cajun has asked for the wisdom of the Perl Monks concerning the following question:

I have a data file that looks like this:

012101 013004 1.44 3.15 9.50 10.83 15.58 12.41
012101 020002 1.38 3.33 528.14 8.70 12.77 20.63
012101 023003 2.53 7.45 3.55 7.28 5.26 17.61
012101 030002 1.59 6.00 9.09 7.26 7.36 36.02
012101 040002 1.44 3.04 2.65 9.30 9.43 30.24

I've done some more looking into this and discovered 'perldoc perllol'. Using the example from there, here's what I've done:

#!/usr/bin/perl -w $datafile='/home/users/cajun/perl/wwwstats/wwwstats.data'; open(INFILE,"$datafile") || die "Cannot open $datafile :$!"; while(<INFILE>){ @tmp=split(0..7); push @AoA, [@tmp]; print $AoA[5][3]; } close INFILE;
When I run this, I get lots of :


Use of uninitialized value in print at ./r-wwwStats.pl line 13, <INFILE> line 1.
Use of uninitialized value in print at ./r-wwwStats.pl line 13, <INFILE> line 2.
Use of uninitialized value in print at ./r-wwwStats.pl line 13, <INFILE> line 3.
Use of uninitialized value in print at ./r-wwwStats.pl line 13, <INFILE> line 4.

So obviously, I'm still missing some piece of the puzzle somewhere. Pointers greatly appreciated !

Replies are listed 'Best First'.
Re: Array of arrays
by chromatic (Archbishop) on Jan 22, 2001 at 09:50 UTC
    I'm not sure what you intend, so I'll try PSI::ESP. On the first five iterations of the loop, there won't be anything in @AoA at index 5. There's your uninitialized value right there.

    Additionally, your split doesn't make a lot of sense. If you want to split on whitespace, you can avoid any regex at all. Otherwise, if you want an array slice, the syntax would be @tmp = (split)[0 .. 7];

    Here's how I would do things:

    while(<DATA>){ push @AoA, [split]; }
    This gives you a list of five lines, with each number in a separate slot.
      Thanks chromatic ! Your PSI::ESP module is working well. I'll have to download that one.
      Problem solved !
Re: Array of arrays
by Fastolfe (Vicar) on Jan 22, 2001 at 09:52 UTC
    You have your print statement in the middle of your loop. The 5th element of that list hasn't been defined yet, so you get the warning that you see above. Perhaps you want to move this outside your loop?
      Duh.... problem one solved. Thanks.
Re: Array of arrays
by chipmunk (Parson) on Jan 22, 2001 at 09:54 UTC
    You're printing a value from $AoA[5] every time through the loop, but @AoA doesn't have anything at index 5 until you've gone through the loop 6 times and pushed 6 array refs onto @AoA.

    By the way, on this line:     @tmp=split(0..7); I think you meant: @tmp = (split)[0..7]; But that may just be a typo from transcribing the code into the textarea here.

Re: Array of arrays
by jeroenes (Priest) on Jan 22, 2001 at 10:56 UTC
    You might wanna check SuperSplit as well. Allows you to save a few keystrokes:
    use SuperSplit; $AoA=supersplit_open($datafile);
    should do the trick for your data. Hope this helps,

    Jeroen
    "We are not alone"(FZ)

Re: Array of arrays
by hotyopa (Scribe) on Jan 22, 2001 at 09:49 UTC
    I might be out of my depth here, but i would say that the problem is with your split statement. The syntax in the camel book doesn't seem to allow for specifying a limit without a pattern and expression. Have you tried it with:

    split " ", $_, 8

    *~-}hotyopa{-~*

Re: Array of arrays
by Beatnik (Parson) on Jan 22, 2001 at 11:40 UTC
    Also worth checking might be perldsc.

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: Array of arrays
by mwp (Hermit) on Jan 22, 2001 at 09:39 UTC
    Try this: print @{$AoA[5]}[3]; or: print $AoA[5]->[3]; $AoA[n] contains a reference to an array. You have to dereference it before you can access the array elements. For more info see perllol.

    Good luck!

    Update: Hmm, only saw one problem and ignored the rest. Guess I was just after first post! ;) I still think dereferencing before accessing is better style (I had no idea it was optional for an array with 2 depth). Apologies, reading perllol now.

      $AoA[5][3] is a perfectly valid way of referencing an element of the array reference in @AoA. Try it. Perhaps you should give perllol a quick once-over yourself :).
      I thought $a[1][2] was equivalent to $a[1]->[2]
      (ie. the -> is implicit between 2 [][]) Am I wrong ?
      Could someone explain me the difference then ?
      (or even point me to the place I could get the info)

        You can find all the info you need at perlref and perllol. Here's a brief description, though.

        When you have a reference to something, there are two ways you can dereference it. By dereference I mean "take the pointer to an object and return the object." You can prefix the reference with the data type indicator or use the dereference operator. For example:

        my @array = ('cow', 'dog', 'cat'); my $ref_to_array = \@array; my ($first, $second) = @{$ref_to_array}[0,1]; # prefix my $third = $ref_to_array->[2]; # deref operator

        This really comes in handy when you have four levels deep of referencing and you need something at the bottom. Typically, the deref operator (->) is optional between multiple levels of dereferencing. For example:

        my @LoL = (); # empty array $LoL[0] = []; # ref to anonymous array $LoL[0]->[0] = []; # another ref to anonymous array # the hard way ${${$LoL[0]}[0]}[0] = 'Look, up in the sky!'; # the easy way $LoL[0]->[0]->[1] = 'It's a bird, it's a plane, it's...'; # the shortcut $LoL[0]->[0][2] = 'Superman!';

        Now that you have more information than you needed or wanted... In this particular example, Perl "Does What You Mean." When you write $LoL[5][3] Perl recognizes that there's no way to rewrite that to mean anything but what you meant to do in the first place. =) So it just assumes the shortcut.

        That's probably the worst example of anything I've ever given. Go read the perldocs! {g}

      Thanks, but both of your corrections failed in exactly the same way as before.
Re: Array of arrays
by cajun (Chaplain) on Jan 22, 2001 at 09:57 UTC
    Thanks for all of the quick replies guys. I'll definitely check them all !