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>


Reply via email to