associative array help

Discussion in 'Perl Misc' started by Mani, Jun 2, 2006.

  1. Mani

    Mani Guest

    I want to extract some info from a file which contains,

    name1 account1 email1 email2
    name1 account2 email1 email2

    name2 account1 email1 email2
    name2 account2 email1 email2

    then, i came across a code,
    $names{$user} {$account} =0;

    given "names" is an associative array,
    and $user and $account are elements of separate strings.

    I guess all values for the key are set to zero.

    if it is written as,

    $names{$user} =0
    all the strings in the associative array "names" are set key values of
    zero.

    but what does $names{$user} {$account} =0 mean?

    -Mani
    Mani, Jun 2, 2006
    #1
    1. Advertising

  2. Mani

    David Squire Guest

    Mani wrote:
    > I want to extract some info from a file which contains,
    >
    > name1 account1 email1 email2
    > name1 account2 email1 email2
    >
    > name2 account1 email1 email2
    > name2 account2 email1 email2
    >
    > then, i came across a code,
    > $names{$user} {$account} =0;
    >
    > given "names" is an associative array,


    .... more commonly called a hash. It is %names that is the hash. The
    sigil (e.g. $, @, or %) is very important in Perl.

    > and $user and $account are elements of separate strings.
    >
    > I guess all values for the key are set to zero.


    ??? There is one value per key.

    > if it is written as,
    >
    > $names{$user} =0
    > all the strings in the associative array "names" are set key values of
    > zero.


    What makes you think that there are strings and that the keys are being
    modified? $names{$user} = 0; results in the value of the member of the
    hash %names with key $user is set to the scalar 0. This is a number, not
    a string. The hash member $names{$user} is created by this statement if
    it does not already exist.

    >
    > but what does $names{$user} {$account} =0 mean?


    It means that you have a hash of hashes. The value of $names{$user} is a
    reference to a hash. $names{$user}{$account} = 0; sets the value of the
    element of the hash %{$names{user}} with key $account to the value 0.

    You needed to read perldoc perldata and perldoc perldsc.

    DS
    David Squire, Jun 2, 2006
    #2
    1. Advertising

  3. Mani

    Mani Guest

    Thanks, i understood its functionality that is hash of hashes.

    David Squire wrote:
    > Mani wrote:
    > > I want to extract some info from a file which contains,
    > >
    > > name1 account1 email1 email2
    > > name1 account2 email1 email2
    > >
    > > name2 account1 email1 email2
    > > name2 account2 email1 email2
    > >
    > > then, i came across a code,
    > > $names{$user} {$account} =0;
    > >
    > > given "names" is an associative array,

    >
    > ... more commonly called a hash. It is %names that is the hash. The
    > sigil (e.g. $, @, or %) is very important in Perl.
    >
    > > and $user and $account are elements of separate strings.
    > >
    > > I guess all values for the key are set to zero.

    >
    > ??? There is one value per key.
    >
    > > if it is written as,
    > >
    > > $names{$user} =0
    > > all the strings in the associative array "names" are set key values of
    > > zero.

    >
    > What makes you think that there are strings and that the keys are being
    > modified? $names{$user} = 0; results in the value of the member of the
    > hash %names with key $user is set to the scalar 0. This is a number, not
    > a string. The hash member $names{$user} is created by this statement if
    > it does not already exist.
    >
    > >
    > > but what does $names{$user} {$account} =0 mean?

    >
    > It means that you have a hash of hashes. The value of $names{$user} is a
    > reference to a hash. $names{$user}{$account} = 0; sets the value of the
    > element of the hash %{$names{user}} with key $account to the value 0.
    >
    > You needed to read perldoc perldata and perldoc perldsc.
    >
    > DS
    Mani, Jun 2, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. desktop
    Replies:
    5
    Views:
    367
    James Kanze
    Jun 26, 2007
  2. Yvon Thoraval
    Replies:
    5
    Views:
    191
    Jason Creighton
    Sep 17, 2003
  3. Ant

    Associative array help needed.

    Ant, Feb 20, 2005, in forum: Javascript
    Replies:
    10
    Views:
    156
  4. VK
    Replies:
    47
    Views:
    500
    Thomas 'PointedEars' Lahn
    Jul 13, 2005
  5. VK
    Replies:
    36
    Views:
    605
    Martin Honnen
    Aug 3, 2005
Loading...

Share This Page