basic explanation on code

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default basic explanation on code

    #!/usr/bin/perl


    %machines =(user1, 'booboo',
    user2, 'puter',
    user3, 'synta',
    user4, 'synta');

    for $key (keys %machines)
    {
    $val=$programs{$key}
    if (!defined $supliers{$val})
    {
    $supliers{$val}=1;
    }
    else
    {
    print "duplicate value: $val\n";
    print "duplicate value: $val\n";

    Cannot understand what is the purpose of $key (keys %machines) ,
    $programs{$key} and $supliers{$val} on this script.

    Will be grateful to get some help!

    R o n e n K f i r
    System Administrator
    CIT div. Tel Aviv University
    Israel
    Tel: 972-3-6407416
    Fax: 972-3-6405158
    cellular: 972-55-405910
    E-mail: [email]ronenk@tauex.tau.ac.il[/email]




    Ronen Kfir Guest

  2. Similar Questions and Discussions

    1. code for basic registration to secure page
      hello there I know that this already exists, I just need to find the snippet or formated codes and where they are located. thanks
    2. Basic code structure in controls for events
      Hi, Does anyone know the basic rules you must implement to get the event model to work in a control? Lets say you want to implement a...
    3. Dark basic pro VS blitz basic 3D VS 3d game studio VS shockwave VS jamajic
      What are all your thoughts on the other leading 3D indi game creation packages out there? Are there others that I didn?t mention? here are a few...
    4. Meaning of [<Non-Basic Code>]
      Graham that's very helpful. But specifically, in your example For example, you might click a button which executes your code...
    5. Droping an Oracle database thru code in Visual Basic
      Hi, I need to know if it is possible to drop, backup and restore an Oracle database (specifically 8i & 9i) through visual basic code. Thanks...
  3. #2

    Default Re: basic explanation on code

    Hi,

    Ronen Kfir wrote:
    >#!/usr/bin/perl
    >
    >
    >%machines =(user1, 'booboo',
    > user2, 'puter',
    > user3, 'synta',
    > user4, 'synta');
    >
    > for $key (keys %machines)
    > {
    > $val=$programs{$key}
    > if (!defined $supliers{$val})
    > {
    > $supliers{$val}=1;
    > }
    > else
    > {
    > print "duplicate value: $val\n";
    > print "duplicate value: $val\n";
    >
    >Cannot understand what is the purpose of $key (keys %machines) ,
    >$programs{$key} and $supliers{$val} on this script.
    łkeys %machines" returns a list of the keys of the %machines hash, in this case

    (user1, user 2, user3, user4)

    There are two other hashes, %programs and %supliers. Obviously, %pograms should have the same set of keys as %machines.

    Now, the for loop assigns the value of $programs{$key} to $val and checks of there is a value defined for $supliers{$val}. If that is the case, it prints the message ""duplicate value..." twice (funny idea), otherwise it assigns 1 to $supliers{$val}.

    Now if you run this two or more times, you should always get the duplicate value message since you defined each $suplier{$val} on the first run.

    - Jan
    --
    There's no place like ~/
    Jan Eden Guest

  4. #3

    Default RE: basic explanation on code

    Hi,

    Ronen Kfir wrote:
    >Hi Jan,
    >I am sorry...
    >
    >This is the right code:
    >
    >
    >#!/usr/bin/perl
    >
    >%machines =(user1, 'booboo',
    > user2, 'puter',
    > user3, 'synta',
    > user4, 'synta');
    >
    > for (keys %machines)
    > {
    > $val=$machines{$_};
    > if (!defined $supliers{$val})
    > {
    > $supliers{$val}=1;
    > # print "hi\n";
    > }
    > else
    > {
    > print "duplicate value: $val\n";
    >
    > }
    > }
    >
    >Seems more reasonable now... What I cant comprehend now is the
    >$supliers{$val part. What's inside it , & all the mechanism around
    >its action.
    >
    The %supliers hash must have keys which equal the values of the %machines hash. So the hashes are "tied together" in a way.

    The way this is written, the %supliers keys will be booboo, puter and synta. For each of these suppliers, you check if it's value is defined. The idea is to mark the supplier synta as duplicate: when you use the value of $machines{user3}, you assign 1 to $supliers{synta}. On a subsequent looping,$supliers{synta} will be defined, so you get the duplicate message.

    Best,

    Jan
    --
    If all else fails read the instructions. - Donald Knuth
    Jan Eden Guest

  5. #4

    Default RE: basic explanation on code

    If I get you correctly the %supliers hash will look like this in this
    script:
    %supliers =(booboo =>'1',
    Puter =>'1',
    Synta => 'defined')
    .... or am I wrong. I need to see the real print of this hash in order to
    understand it...

    Thanx a lot!



    R o n e n K f i r
    System Administrator
    CIT div. Tel Aviv University
    Israel
    Tel: 972-3-6407416
    Fax: 972-3-6405158
    cellular: 972-55-405910
    E-mail: [email]ronenk@tauex.tau.ac.il[/email]



    -----Original Message-----
    From: Jan Eden [mailto:lists@jan-eden.de]
    Sent: Friday, February 13, 2004 4:39 PM
    To: Ronen Kfir; Perl Lists
    Subject: RE: basic explanation on code

    Hi,

    Ronen Kfir wrote:
    >Hi Jan,
    >I am sorry...
    >
    >This is the right code:
    >
    >
    >#!/usr/bin/perl
    >
    >%machines =(user1, 'booboo',
    > user2, 'puter',
    > user3, 'synta',
    > user4, 'synta');
    >
    > for (keys %machines)
    > {
    > $val=$machines{$_};
    > if (!defined $supliers{$val})
    > {
    > $supliers{$val}=1;
    > # print "hi\n";
    > }
    > else
    > {
    > print "duplicate value: $val\n";
    >
    > }
    > }
    >
    >Seems more reasonable now... What I cant comprehend now is the
    >$supliers{$val part. What's inside it , & all the mechanism around
    >its action.
    >
    The %supliers hash must have keys which equal the values of the
    %machines hash. So the hashes are "tied together" in a way.

    The way this is written, the %supliers keys will be booboo, puter and
    synta. For each of these suppliers, you check if it's value is defined.
    The idea is to mark the supplier synta as duplicate: when you use the
    value of $machines{user3}, you assign 1 to $supliers{synta}. On a
    subsequent looping, $supliers{synta} will be defined, so you get the
    duplicate message.

    Best,

    Jan
    --
    If all else fails read the instructions. - Donald Knuth


    Ronen Kfir Guest

  6. #5

    Default RE: basic explanation on code


    Ronen Kfir wrote:
    >If I get you correctly the %supliers hash will look like this in this
    >script:
    >%supliers =(booboo =>'1',
    > Puter =>'1',
    > Synta => 'defined')
    >.... or am I wrong. I need to see the real print of this hash in order to
    >understand it...
    >
    Not exactly, all keys in %supliers are assigned the value '1', and are thereby defined. Being defined is the same as having any value other than undef. So %supliers{synta} gets '1' if the loop encounters the first user with the value "synta" (in the %machines hash) and is thereby defined if the loop encounters the second user with the value "synta". Then you get the duplicate message.

    Note that the %supliers hash will start out empty, i.e. without key/value pairs. This is a prerequisite for the duplicate check.

    Apart from your actual problem, you should always use strict in your scriptto make Perl enforce good programming style and use warnings (if you have a recent Perl distribution) to make Perl tell you about hazardous operations.

    HTH,

    Jan
    --
    How many Microsoft engineers does it take to screw in a lightbulb? None. They just redefine "dark" as the new standard.
    Jan Eden Guest

  7. #6

    Default RE: basic explanation on code

    <answer>
    i agree with you,
    but for the sake of read - a - bility I would have defined
    the vars here (<---) as they are valid for whole package anyway and not
    just for test1();

    use strict;

    local $a = 1; <---
    local $aa = 2; <---

    test1();

    sub test1
    {
    test2();
    }

    sub test2
    {
    print "a = $a\n";
    print "aa = $aa\n";
    }
    </answer>
    Wiggins d'Anconia wrote:
    > Eternius wrote:
    >
    >> Joshua A. Gage wrote:
    [snip]
    >>
    >> use my
    >> extract from perldoc -f my :
    >> A "my" declares the listed variables to be local
    >> (lexically) to the enclosing block, file, or
    >> "eval".
    >>
    >
    > In most cases you would be right, but in this case it appears that the
    > OP understands what 'local' does, as he is using it inside of an
    > embedded function. Do I like that, no, but it is a "correct" usage of
    > 'local'. Your suggestion of using 'my' changes how the variables must
    > be used so is not equivalent without suggesting the interface to 'test2'
    > changes...
    >
    > sub test1 {
    > my $aa = 1;
    > my $aaa = 2;
    > test2($aa, $aaa);
    > }
    >
    > sub test2 {
    > my ($bb, $bbb) = @_;
    > print "aa = $bb\n";
    > print "aaa = $bbb\n";
    > }
    >
    > Is the above better, in the court of readability and encapsulation, yes,
    > in the court of elegance, maybe not...
    >
    > [url]http://danconia.org[/url]
    >
    >
    >
    Eternius Guest

  8. #7

    Default Re: basic explanation on code

    Please bottom post....

    Eternius wrote:
    > <answer>
    > i agree with you,
    > but for the sake of read - a - bility I would have defined
    > the vars here (<---) as they are valid for whole package anyway and not
    > just for test1();
    >
    But again you have changed the meaning, if you are going to declare them
    as package variables at the top then you don't need a 'local' at all and
    that should be an 'our', this is even worse for encapsulation and
    definitely shows a lack of understanding of 'local'...
    > use strict;
    >
    > local $a = 1; <---
    > local $aa = 2; <---
    >
    > test1();
    >
    > sub test1
    > {
    > test2();
    > }
    >
    > sub test2
    > {
    > print "a = $a\n";
    > print "aa = $aa\n";
    > }
    As an example, try:

    use strict;
    use warnings;


    our $aa = 1;
    our $aaa = 2;


    print "aa 1st = $aa\n";
    print "aaa 1st = $aaa\n";


    test1();


    print "aa 3rd = $aa\n";
    print "aaa 3rd = $aaa\n";


    sub test1 {
    local $aa = 3;
    local $aaa = 4;
    test2();
    }


    sub test2 {
    print "aa 2nd = $aa\n";
    print "aaa 2nd = $aaa\n";
    }

    Suggested reading: [url]http://perl.plover.com/FAQs/Namespaces.html[/url]

    [url]http://danconia.org[/url]

    > Wiggins d'Anconia wrote:
    >
    > > Eternius wrote:
    > >
    > >> Joshua A. Gage wrote:
    > [snip]
    > >>
    > >> use my
    > >> extract from perldoc -f my :
    > >> A "my" declares the listed variables to be local
    > >> (lexically) to the enclosing block, file, or
    > >> "eval".
    > >>
    > >
    > > In most cases you would be right, but in this case it appears that the
    > > OP understands what 'local' does, as he is using it inside of an
    > > embedded function. Do I like that, no, but it is a "correct" usage of
    > > 'local'. Your suggestion of using 'my' changes how the variables must
    > > be used so is not equivalent without suggesting the interface to 'test2'
    > > changes...
    > >
    > > sub test1 {
    > > my $aa = 1;
    > > my $aaa = 2;
    > > test2($aa, $aaa);
    > > }
    > >
    > > sub test2 {
    > > my ($bb, $bbb) = @_;
    > > print "aa = $bb\n";
    > > print "aaa = $bbb\n";
    > > }
    > >
    > > Is the above better, in the court of readability and encapsulation, yes,
    > > in the court of elegance, maybe not...
    > >
    > > [url]http://danconia.org[/url]
    > >
    > >
    > >
    >
    Wiggins D'Anconia Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139