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

my @beef = @{ $dataset->{ $col[0] } };
This line is really
my $key = $col[0] ; my $meat = $dataset->{ $key }; my @beef = @{ $meat };
I misspoke when I said it is confusing $dataset for a hashref , in actuality, $dataset is a hashref.

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.

## 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.
See References quick reference

To avoid this error, you might add

next unless $meat;
You don't want to disable strict :)

Lets take apart a simpler example, one that builds @data

push @{$data[$nr - 1]->{shift(@cols)}},\@cols;
aka
push @{ $data[ $nr - 1 ]->{ shift(@cols) } }, \@cols;
aka
push @{ $data[ $fileCount ]->{ shift @col } }, \@col;
is really
my $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;
or even
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;

In reply to Re^5: Combining 3 files by Anonymous Monk
in thread Combining 3 files by garyboyd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.