RE: hash assign not working
Smith, Derek wrote: My hash creations are not working as I expected: %hash = ( @mir, @mir2 ); Why? To populate a hash requires two fields: Key and data. What you are assuming is that it will take one from @mir and one from @mir2 which is a wrong assumption. Yes the second works because you do take the key and data, but one from each array. Could do something like: while ( @mir ) { $hash{shift(@mir)} = shift(@mir2); } Did a quick test and does work. Wags ;) But do work at %hash = ( $mir[0], $mir2[0] ); Obviously I need the entire arrays and their associated key/value pairs, and have tried various methods unsuccessfully from Programming Perl CD such as array of hashes. The end result for my key/value pair should look like Mirror copies 1 : /dev/vg00/lvol3 Which is the printed output of the individual element assignment as %hash = ($mir[0],$mir2[0]); But cannot get %hash = (@mir,@mir2); to work. Plz help. my (%hash,@mir,@mir2)= (); my ($key2,$value2) = 0; foreach my $lv (@lvs) { push @mir = (grep /mirror/i, `lvdisplay $lv`); push @mir2 = (grep s/^LV Name\s*//, `lvdisplay $lv`); chomp (@mir,@mir2); } #%hash = (@mir,@mir2); ## This does not work the way I expect, so I tried the foreach in the next line. foreach (@mir) { foreach (@mir2) { ($key2,$value2) = [ split ]; $hash{$key2}= $value2; } } while (($key2,$value2) = each %hash) { #($key2,$value2) = [ split ]; print $key2\t:$value2; } Derek Bellner Smith Unix Systems Engineer Cardinal Health Dublin, Ohio 614-757-5000 Main 614-757-8075 Direct 614-757-8120 Fax [EMAIL PROTECTED] Cardinal Health -- Working together. For life. (sm) _ This message is for the designated recipient only and may contain privileged, proprietary, or otherwise private information. If you have received it in error, please notify the sender immediately and delete the original. Any other use of the email by you is prohibited. Dansk - Deutsch - Espanol - Francais - Italiano - Japanese - Nederlands - Norsk - Portuguese - Svenska: www.cardinalhealth.com/legal/email ** This message contains information that is confidential and proprietary to FedEx Freight or its affiliates. It is intended only for the recipient named and for the express purpose(s) described therein. Any other use is prohibited. ** -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: hash assign not working
On 5/16/06, Smith, Derek [EMAIL PROTECTED] wrote: My hash creations are not working as I expected: %hash = ( @mir, @mir2 ); Why? Probably because that expression isn't a list of key-value pairs. If you're collecting @mir (a list of keys?) and @mir2 (a list of corresponding values?), no wonder you're having troubles. You're hammering with the wrong kind of violin, and you're not even holding it right. foreach my $lv (@lvs) { push @mir = (grep /mirror/i, `lvdisplay $lv`); push @mir2 = (grep s/^LV Name\s*//, `lvdisplay $lv`); Don't run the same command twice! Store its output into an array. Then you can process that array to find $some_key and $some_value that goes with it, and store those into your hash (not into arrays) as you go along: $hash{$some_key} = $some_value; And did you really mean to use s/// on $_ within grep()? That's bad form, even if Perl lets you get away with it. Cheers! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: hash assign not working
Smith, Derek wrote: : My hash creations are not working as I expected: %hash = ( @mir, : @mir2 ); : : Why? Because nothing magical happens on the right hand side of the assignment. If I have a series of arrays over there perl flattens them into one list. It does not handle them special just because there's a hash on the right hand side. If @mir = (1, 2 ,3) and @mir2 = (4, 5, 6), then ( @mir, @mir2 ) = (1, 2, 3, 4, 5, 6) So using a little algebra, my %hash = ( @mir, @mir2 ); Is the same as this: my %hash = (1, 2, 3, 4, 5, 6); Or similar to this: my %hash = ( 1 = 2, 3 = 4, 5 = 6, ); HTH, Charles K. Clarkson -- Mobile Homes Specialist Free Market Advocate Web Programmer 254 968-8328 If it looks wrong convert it to plain text. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: hash assign not working
Wagner, David --- Senior Programmer Analyst --- WGO wrote: : while ( @mir ) { : $hash{shift(@mir)} = shift(@mir2); :} We can also use a hash slice. my %hash; @[EMAIL PROTECTED] = @mir2; HTH, Charles K. Clarkson -- Mobile Homes Specialist Free Market Advocate Web Programmer 254 968-8328 If it looks wrong convert it to plain text. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: hash assign not working
On Tue, May 16, 2006 at 11:06:02AM -0700, Wagner, David --- Senior Programmer Analyst --- WGO wrote: while ( @mir ) { $hash{shift(@mir)} = shift(@mir2); } @[EMAIL PROTECTED] = @mir2; -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: hash assign not working
Charles K. Clarkson wrote: : It does not handle them special just because : there's a hash on the right hand side. Whoopsie! Should be left hand side not right. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: hash assign not working
From: Wagner, David --- Senior Programmer Analyst --- WGO [mailto:[EMAIL PROTECTED] Sent: Tuesday, May 16, 2006 2:06 PM To: Smith, Derek; beginners@perl.org Subject: RE: hash assign not working Smith, Derek wrote: My hash creations are not working as I expected: %hash = ( @mir, @mir2 ); Why? To populate a hash requires two fields: Key and data. What you are assuming is that it will take one from @mir and one from @mir2 which is a wrong assumption. Yes the second works because you do take the key and data, but one from each array. Could do something like: while ( @mir ) { $hash{shift(@mir)} = shift(@mir2); } Did a quick test and does work. Wags ;) But do work at %hash = ( $mir[0], $mir2[0] ); Obviously I need the entire arrays and their associated key/value pairs, and have tried various methods unsuccessfully from Programming Perl CD such as array of hashes. The end result for my key/value pair should look like Mirror copies 1 : /dev/vg00/lvol3 Which is the printed output of the individual element assignment as %hash = ($mir[0],$mir2[0]); But cannot get %hash = (@mir,@mir2); to work. Plz help. my (%hash,@mir,@mir2)= (); my ($key2,$value2) = 0; foreach my $lv (@lvs) { push @mir = (grep /mirror/i, `lvdisplay $lv`); push @mir2 = (grep s/^LV Name\s*//, `lvdisplay $lv`); chomp (@mir,@mir2); Kudos to all...I was surprised to see all the responses. Tom: I had no good reason why I was running that system commands twice other than I wanted to isolate the needed data into 2 different arrays. It is better to get all the data in 1 full swoop, thanks for catching that! I thought Perl took cars of the key/value matches from both arrays via statements: %hash = (@mir, @mir2); %hash{$key} = $value Charles thanks for the visual... that helped. Cardinal Health -- Working together. For life. (sm) _ This message is for the designated recipient only and may contain privileged, proprietary, or otherwise private information. If you have received it in error, please notify the sender immediately and delete the original. Any other use of the email by you is prohibited. Dansk - Deutsch - Espanol - Francais - Italiano - Japanese - Nederlands - Norsk - Portuguese - Svenska: www.cardinalhealth.com/legal/email -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response