If you use @colors{greys}perl thinks that you have an array named @colors, and if it is followed by {...} it understands that you want to get the array of values for the specified keys.
Octavian----- Original Message ----- From: "Sina Bahram" <sbahram@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx> Sent: Tuesday, October 14, 2008 5:04 PM Subject: RE: Perl arrays and hashes
But I wasn't assigning it to a key, only to the value stored at that key's index. For example. If I have %colors I would like to have @colors{darks} @colors{lights} @colors{greys}Where those are simply arrays, as I tried to symbolize by the at sign, whichcontain a list of colors of the given description. Instead, I'm lead to believe that I would have to do. @{$colors{"darks"}}Which is fine, but I'm just saying I think the previous syntax looks nicer.Take care, Sina -----Original Message----- From: programmingblind-bounce@xxxxxxxxxxxxx [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of james.homme@xxxxxxxxxxxx Sent: Tuesday, October 14, 2008 9:29 AM To: programmingblind@xxxxxxxxxxxxx Subject: RE: Perl arrays and hashes Hi, Perhaps I didn't read correctly, but I thought he said that you couldn't assign an array to a key of a hash, but you can assign an array to a value of a hash. I think this is like a Python or VbScript or JavaScript dictionary. Thanks. 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 RE: Perl arrays and hashes 10/14/2008 08:50 AM Please respond to programmingblind@ freelists.orgThanks much. Is there a reason why you can not assign an array, rather thanan array reference, to an element of a hash? Take care, Sina -----Original Message----- From: programmingblind-bounce@xxxxxxxxxxxxx [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Octavian Rasnita Sent: Tuesday, October 14, 2008 7:13 AM To: programmingblind@xxxxxxxxxxxxx Subject: Re: Perl arrays and hashes This is because for any hash key you can have an array reference as a value. You can't have an array as a value for a hash key. For example, you can't use something like: %hash = ( key => (1, 2, 3), ); but you need to use: %hash = ( key => [1, 2, 3], ); And when you use $hash{key} you will have the value of this key as a result. And that value is an array ref, not an array. If you want to have the array for that ref, you need to dereference it, putting the "@" sign before it like: my $arrayref = [1, 2, 3]; my @array = @$arrayref;So you need to use @$arrayref and not only @arrayref because there is no anarray with this name.$arrayref is a scalar not an array. That scalar is a reference to an array.Octavian ----- Original Message ----- From: "Sina Bahram" <sbahram@xxxxxxxxx> To: <programmingblind@xxxxxxxxxxxxx> Sent: Tuesday, October 14, 2008 9:00 AM Subject: RE: Perl arrays and hashesI did understand all of that, but I suppose I was asking about why perlwastreating it as a reference rather than doing the conversion itself. Regardless though, it all works and thanks. Take care, Sina -----Original Message----- From: programmingblind-bounce@xxxxxxxxxxxxx [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of OctavianRasnitaSent: Tuesday, October 14, 2008 1:35 AM To: programmingblind@xxxxxxxxxxxxx Subject: Re: Perl arrays and hashes Hi, You need to write @{$macs{$key}} and not @macs{$key} because %macs is a hash and not an array to be noted with @ at the beginning. The %macs hash has a key named $key which happends to have an array as a value. So you first need to specify that you want to address the key $key ofthishash, using $macs{$key} and derefference it to get its value... the array, using @{$macs{$key}} It is just like when you use my $hash_key = $macs{$key}; my @array = @$macs_key; Of course, you need to add more braces like when you use my @array = @{$hash_key}; because $macs{$key} already contains braces and you need to specify thattheentire $macs{$key} is an array reference, not only $hash. Octavian ----- Original Message ----- From: "Sina Bahram" <sbahram@xxxxxxxxx> To: <programmingblind@xxxxxxxxxxxxx> Sent: Monday, October 13, 2008 11:38 PM Subject: RE: Perl arrays and hashesI'm off to try this out, but can you please explain why that is? I do understand why you're doing what you're doing, but I'm unclear astowhy perl isn't doing this automatically? Take care, Sina -----Original Message----- From: programmingblind-bounce@xxxxxxxxxxxxx [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of OctavianRasnitaSent: Monday, October 13, 2008 4:24 PM To: programmingblind@xxxxxxxxxxxxx Subject: Re: Perl arrays and hashes Hi, You need to use: for my $key (sort(keys(%macs))) { print "$key\n"; for my $val (@{$macs{$key}}) { print "$val\n"; } print "\n"; } Octavian ----- Original Message ----- From: "Sina Bahram" <sbahram@xxxxxxxxx> To: <programmingblind@xxxxxxxxxxxxx> Sent: Monday, October 13, 2008 10:50 PM Subject: Perl arrays and hashesHi all, I have a very simple program that loops through a file, whose structureislike 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 thekeysof that hash, I get all the ip addresses I was looking for, but heavenforbidI try to get the values. That's an insane nightmare. How can I loop through that hash, with each key, looping through eachofthe arrays stored at each key's index. After all, each key is an IPaddress,and each IP address has a series of mac addresses associated with it inthisfile, 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 justit 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 thecontentsof that array. Why does it do this? I am using a for each construct to itterate through an array, and I usethe @ 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 toloopthrough the array stored at @macs{$key}? Maybe I'm not doing this right up top? That's what I think the problemis.Somehow I've given my hash a reference to an array, rather than thearrayitself. 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__________ 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 __________ 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
__________View the list's information and change your settings at //www.freelists.org/list/programmingblind