associative array help

M

Mani

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
 
D

David Squire

Mani said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top