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

Hi Monks, I basically have a big file of numbers (over 100 lines) and each line then has numerous numbers again. Obviously the only way to process this is within a loop.
However, my question is, how can I alter my code so that the @good_nums array prints only the values for each particular line, not the accumulated values for the entire file?
foreach my $line (@file) { # @array just contains certain values computed so far + for (my $i = 1; $i < @array; $i++) { if ($array[$i - 1] =~ m/(\d+)\s{1}(\d*)/)/ { $x1 = $1; $y1 = $2; } if ($array[$i] =~ m/(\d+)\s{1}(\d*)/)/ { $x2 = $1; $y2 = $2; } $equ1 = ($y2 * $y1); $equ2 = ($x2 * $x1); $equ3 = ($equ1 * $equ2); push @good_nums, $equ3; } print "@good_nums"; }

Replies are listed 'Best First'.
Re: value accumulation
by bigj (Monk) on May 15, 2003 at 09:48 UTC
    Sorry, I don't understand really what your input and desired output is. Thus I can't also not follow what your code wants to do. Can you give us an example of your input and a description what good numbers are.
    However, of course, I can rewrite your code to shorten it and make it more Perlish :-) I assume, that the data comes from a file.
    use Tie::CSV_File; tie my @array, "Tie::CSV_File", $filename, WHITESPACE_SEPARATED; for my $i (1 .. $#array) { push @good_nums, $array[$i-1]->[0] * $array[$i]->[1] * $array[$i-1]->[1] * $array[$i]->[0]; # Did you really want to compute 4x a multiplication ?! } print @good_nums;
    Well, I still don't know what's going on :-(
    If the data doesn't come from a file, you could also achieve quite the same, all you have to do is to nest the data:
    @array = map {[split]} @array; # instead of the first two lines of my snippet

    Greetings,
    Janek

Re: value accumulation
by Bilbo (Pilgrim) on May 15, 2003 at 11:14 UTC

    Do you just mean that you want to clear @goodnums before processing each line? If so then you probably just want to declare the array within the loop so that it is reinitialised each time

    foreach my $line (@file) { my @good_nums; ... ... }

      When I saw your post I thought "Yes, that's true. But it would be more efficient to declare the @good_nums array outside the look and just empty it". Luckily I did not go ahead and post that, but run a benchmark (In Perl 5.8, ActivePerl build 804, Win2kServer):

      use Benchmark; use strict; sub arrayInit { foreach (1..1000) { my @list; foreach (1..10) { push @list, $_; } } } sub arrayUndef { my @list; foreach (1..1000) { foreach (1..10) { push @list, $_; } undef @list; } } sub arrayEmpty { my @list; foreach (1..1000) { foreach (1..10) { push @list, $_; } @list = (); } } sub scalarInit { foreach (1..1000) { my $sum; foreach (1..10) { $sum += $_; } } } sub scalarUndef { my $sum; foreach (1..1000) { foreach (1..10) { $sum += $_; } undef $sum; } } sub scalarEmpty { my $sum; foreach (1..1000) { foreach (1..10) { $sum += $_; } $sum = 0; } } timethese 1000, { arrayInit => \&arrayInit, arrayUndef => \&arrayUndef, arrayEmpty => \&arrayEmpty, scalarInit => \&scalarInit, scalarUndef => \&scalarUndef, scalarEmpty => \&scalarEmpty, } __END__ Benchmark: timing 1000 iterations of arrayEmpty, arrayInit, arrayUndef +, scalarEmpty, scalarInit, sca larUndef... arrayEmpty: 15 wallclock secs (13.97 usr + 0.00 sys = 13.97 CPU) @ 71 +.58/s (n=1000) arrayInit: 13 wallclock secs (13.45 usr + 0.00 sys = 13.45 CPU) @ 74 +.35/s (n=1000) arrayUndef: 16 wallclock secs (15.70 usr + 0.00 sys = 15.70 CPU) @ 63 +.68/s (n=1000) scalarEmpty: 9 wallclock secs ( 9.30 usr + 0.02 sys = 9.32 CPU) @ 1 +07.26/s (n=1000) scalarInit: 10 wallclock secs ( 9.36 usr + 0.01 sys = 9.37 CPU) @ 10 +6.68/s (n=1000) scalarUndef: 10 wallclock secs ( 9.43 usr + 0.00 sys = 9.43 CPU) @ 1 +06.01/s (n=1000)
      So once again I was wrong :-|

      Thanks for making me try.

      Jenda
      Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
         -- Rick Osborne

      Edit by castaway: Closed small tag in signature