in reply to Re: Printing out multiple array lists and more!
in thread Printing out multiple array lists and more!

This worked perfectly!! I am studying the code right now. I understand the majority of it. I have one question though...

Code for reference:

1 #!/usr/local/bin/perl 2 3 use strict; 4 5 my $max = 0; 6 7 sub readfile { 8 my $fn=shift; 9 my @ar; 10 11 open IN,"<$fn" || die "Couldn't open '$fn': $!"; 12 ## snarf the header 13 scalar <IN>; 14 ## read the rest 15 while (<IN>) { 16 chomp; 17 push @ar,$_; 18 $max = $_ if $_ > $max; 19 } 20 close IN; 21 22 @ar; 23 } 24 25 26 ## load the files into arrays 27 my @a=readfile 'f1.lst'; 28 my @b=readfile 'f2.lst'; 29 my @c=readfile 'f3.lst'; 30 31 ## iterate until all arrays are empty 32 for (my $i = 0; $i <= $max; $i++) 33 { 34 next unless $a[0] == $i || $b[0] == $i || $c[0] == $i; 35 36 ## print them out (if they match) 37 printf "%5s", $a[0]==$i ? scalar shift @a : undef; 38 print " | "; 39 printf "%5s", $b[0]==$i ? scalar shift @b : undef; 40 print " | "; 41 printf "%5s", $c[0]==$i ? scalar shift @c : undef; 42 43 print "\n"; 44 }
My only question is: In lines 36-41 you have a snippet  scalar shift @array. What is this doing? :) Thank you very much!!

----------
- Jim

Replies are listed 'Best First'.
Re: Re: Re: Printing out multiple array lists and more!
by mr.nick (Chaplain) on May 17, 2001 at 22:08 UTC
    In my original code (which does not work for the test data :() I detected the end of the looping by testing to see if any of the arrays where still defined (had contents). Since I needed to pull the next number off the list to display it, I just combined a few operations from
    if ($a[0]==$i) { printf "%5s",$a[0]; shift @a; }
    into the code you see. Oh, and I used scalar probably without need, but I wanted to make sure that the shift statement didn't pull off more than one element.