in reply to Re^4: Combining 3 files
in thread Combining 3 files
Hi anonymous monk, yes I'm still finding this confusing.
Well hold onto your pants :)
You mentioned it is confusing $dataset for a hashref.
Look at the @beef equivalent example, it is this line that dies with Can't use an undefined value as an ARRAY reference
This line is reallymy @beef = @{ $dataset->{ $col[0] } };
I misspoke when I said it is confusing $dataset for a hashref , in actuality, $dataset is a hashref.my $key = $col[0] ; my $meat = $dataset->{ $key }; my @beef = @{ $meat };
The problem is, $key is not in $dataset, so $meat is undef
Since meat is undef, trying to treat $meat as an array by de-referencing it, triggers a warning if you have warnings on, and triggers an error, if you have strict on.
See References quick reference## no warnings or errors $ perl -e "print @{ undef() }; print 6" 6 ## a warning is issued, but 6 still gets printed $ perl -we "print @{ undef() } Use of uninitialized value in array dereference at -e line 1. 6 ## with strict the warning is fatal, 6 not printed cause program died $ perl -Mstrict -we "print @{ undef() } Can't use an undefined value as an ARRAY reference at -e line 1.
To avoid this error, you might add
You don't want to disable strict :)next unless $meat;
Lets take apart a simpler example, one that builds @data
akapush @{$data[$nr - 1]->{shift(@cols)}},\@cols;
akapush @{ $data[ $nr - 1 ]->{ shift(@cols) } }, \@cols;
is reallypush @{ $data[ $fileCount ]->{ shift @col } }, \@col;
or evenmy $Hashref = $data[ $fileCount ]; if( not $Hashref){ $Hashref = $data[ $fileCount ] = {}; } my $key = shift @col; my $Arrayref = $Hashref->{ $key }; if( not $Arrayref ){ $Arrayref = $Hashref->{ $key } = []; } push @{ $Arrayref }, \@col;
my $Hashref = $data[ $fileCount ]; if( not $Hashref){ my %newHashThatIsOnlyNamedHere; $Hashref = $data[ $fileCount ] = \%newHashThatIsOnlyNamedHere; } my $key = shift @col; my $Arrayref = $Hashref->{ $key }; if( not $Arrayref ){ my @newArrayThatIsOnlyNamedHere; $Arrayref = $Hashref->{ $key } = \@newArrayThatIsOnlyNamedHere; } push @{ $Arrayref }, \@col;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Combining 3 files
by garyboyd (Acolyte) on Jun 27, 2011 at 14:47 UTC | |
by Anonymous Monk on Jun 27, 2011 at 14:58 UTC | |
by garyboyd (Acolyte) on Jun 27, 2011 at 15:11 UTC | |
by Anonymous Monk on Jun 27, 2011 at 15:43 UTC | |
by garyboyd (Acolyte) on Jun 27, 2011 at 15:58 UTC | |
|