RE: Perl arrays and hashes

  • From: "Sina Bahram" <sbahram@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Mon, 13 Oct 2008 16:07:46 -0400

The construct exists yes. There is also an each function which facilitates
such idioms.

Like this

for my $val (@vals)
{
print "$val was in vals\n";
}

Take care,
Sina

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of
james.homme@xxxxxxxxxxxx
Sent: Monday, October 13, 2008 4:01 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: Perl arrays and hashes

Hi,
Dumb question, but does Peral have ForEach?

Jim

James D Homme, Usability Engineering, Highmark Inc.,
james.homme@xxxxxxxxxxxx, 412-544-1810

"The difference between those who get what they wish for and those who
don't is action. Therefore, every action you take is a complete
success,regardless of the results." -- Jerrold Mundis
Highmark internal only: For usability and accessibility:
http://highwire.highmark.com/sites/iwov/hwt093/


                                                                           
             "Sina Bahram"                                                 
             <sbahram@xxxxxxxx                                             
             m>                                                         To 
             Sent by:                  programmingblind@xxxxxxxxxxxxx      
             programmingblind-                                          cc 
             bounce@freelists.                                             
             org                                                   Subject 
                                       Perl arrays and hashes              
                                                                           
             10/13/2008 03:50                                              
             PM                                                            
                                                                           
                                                                           
             Please respond to                                             
             programmingblind@                                             
               freelists.org                                               
                                                                           
                                                                           




Hi all,

I have a very simple program that loops through a file, whose structure is
like this.

Key name:
Value1
Value2
Value3
...

Key name:
Value1
Value2
.....

Key name:
Value1
Value2
Value3
Value4
...

And so on

Not hard, right?

I use the following snippet of code to parse that file.

***

open(MACS, "macs.txt");

my @macsFromFile = <MACS>;
chomp @macsFromFile;

my %macs;
my $i = 0;
for my $mac (@macsFromFile)
{
if($mac =~ /10.110.0.*/)
{
$key = $mac;
$i = 0;
@macs{$key} = ();
}

$macs{$key}[$i++] = $mac if($mac =~ /05:.*/);
}

***

Anyways, as far as I can tell, that works fine. When I print out the keys
of
that hash, I get all the ip addresses I was looking for, but heaven forbid
I
try to get the values. That's an insane nightmare.

How can I loop through that hash, with each key, looping through each of
the
arrays stored at each key's index. After all, each key is an IP address,
and
each IP address has a series of mac addresses associated with it in this
file, in the form of them being in an array assigned to that key in the
hash.

So I wanted to make sure I parsed the file write. Thus, why not just print
it out again and compare against the original. I tried the following.

***

for my $key (sort(keys(%macs)))
{
print "$key\n";
for my $val (@macs{$key})
{
print "$val\n";
}
print "\n";
}

***

It prints out a single memory address rather than the list of the contents
of that array.

Why does it do this?

I am using a for each construct to itterate through an array, and I use the
@ to indicate that I want array context to be used when I parse @macs{$key}
... What the heck else should I do to make perl understand I want to loop
through the array stored at @macs{$key}?

Maybe I'm not doing this right up top? That's what I think the problem is.
Somehow I've given my hash a reference to an array, rather than the array
itself.

Help!

Take care,
Sina

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind



__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: