awohld has asked for the wisdom of the Perl Monks concerning the following question:
I want to take the second element in each array and make it a hash key. If the second element is undef, then I want to change it to '0'. Also I want to take the first element in every array and push it into the array referenced by the hash ( which is the second element in the array).$VAR1 = [ [ '95', undef ], [ '100', '0' ] [ '105', '1' ], [ '110', '1' ] ];
!/usr/bin/perl -w use strict; use Data::Dumper; my @one = qw(95 undef); my @two = qw(100 2); my @three = qw(105 undef); my @four = qw(100 2); my @array = \@one; push @array, \@two; push @array, \@three; push @array, \@four; print Dumper @array; my %hash; foreach my $ary ( @array ) { if ( @$ary[1] eq undef ) { @$ary[1] = '0' } $hash{@$ary[1]} = push @$ary[0]; } print Dumper %hash;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Transform Array Ref to Hash Ref - Where's my error?
by Errto (Vicar) on Feb 14, 2006 at 01:31 UTC | |
by davidrw (Prior) on Feb 14, 2006 at 02:12 UTC | |
by ayrnieu (Beadle) on Feb 14, 2006 at 07:20 UTC | |
|
Re: Transform Array Ref to Hash Ref - Where's my error?
by McDarren (Abbot) on Feb 14, 2006 at 01:12 UTC |