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

Dear monks, Warning I am very new to perl! I am basically writing a CGI application where a file is submitted contianing 96 lines of numbers (plus a bit of junk) - i am filtering out the junk and then finding the single highest number on each line - i am currently using a foreach loop to do this but am not getting very far. I would be very grateful if someone could pick out where i'm going wrong. ;-) I am having no problems displaying the actual file, it is within the foreach loop where i am struggling, especaially picking out the highest number (also the CGI is very slow at processing so many numbers in the foreach loop). Thanx.
$data =~ s/([^\n]) (\n) ([^\n]) /$1$3/g; # data contains the input f +ile $data =~ s/\n+/\n/g; my @data = split /\n/, $data; my $in_derivative = 0; my $info; my $element; my @numbers; foreach my $line (@data) { if ($line =~ m/^\S{4}(\s+)Derivative(.*)/) { $in_derivative = 1; } if ($in_derivative) { $info .= "$line\n"; } } my @derivative = split (/\n/, $info); my $highest; my $i; foreach my $well (@derivative) { if ($well =~ s/^\S{4}(\s+)Derivative(.*)//) { next; } $well =~ s/^(\d+){1,2}//; my $well_no = $1; my @well = split ('', $well); $highest = $well[0]; # definately problems here, cant pick out $ +well[0] for ($i = 0; $i < @well; $i++) { if ($well[$i] > $highest) { $highest = $numbers[$i]; } } print $highest; }

Replies are listed 'Best First'.
Re: foreach nightmare!
by Skeeve (Parson) on May 08, 2003 at 11:30 UTC
    my @well = split ('', $well); $highest = $well[0]; # definately problems here, cant pick out $ +well[0]
    that should be okay. Why do you think there is a problem? Your problems are here:
    for ($i = 0; $i < @well; $i++) { # scalar(@well) here if ($well[$i] > $highest) { $highest = $numbers[$i]; # $well[$i] here } }
    You could also write your loop with:
    foreach $number (@well) { $highest=$number if $highest<$number; }
      It's not necessary to change @well to scalar(@well), because the less-than operator imposes scalar contect on its operands already.

      But in any case, your foreach rewrite is much better. :-)

          -- Chip Salzenberg, Free-Floating Agent of Chaos

Re: foreach nightmare!
by Jaap (Curate) on May 08, 2003 at 10:42 UTC
    Please post 5 lines of your data file. I can point out a couple of problems in your code, but it'd be far easier to give you a small piece of correct code ;-)
      Hi, It looks something like this:
      Well Derivative Data (Reading 1...N) + + + + + + + + 1 -0.0534 -0.0423 -0.0148 -0.0056 0.0074 0.0235 0 +.0296 0.0225 0.0431 0.0377 0.0495 0.0496 0.0477 +0.0545 0.0594 0.055 0.0696 0.0751 0.0597 0.0452 +0.0419 0.0449 0.0578 0.0658 0.0777 0.0926 0.0784 + 0.0693 0.0642 0.0685 0.0697 0.0736 0.0691 0.0667 + 0.058 0.0508 0.0538 0.0621 0.0737 0.0785 0.0783 + 0.0687 0.0581 0.0546 0.0559 0.0607 0.0718 0.0765 + 0.0688 0.067 0.0644 0.0707 0.0776 0.085 0.0864 + 0.1129 0.1922 0.3194 0.4269 0.4617 0.4155 0.3037 + 0.1595 0.0428 -0.0078 0.0033 0.0275 0.0299 0.030 +4 0.0315 0.0301 0.0278 0.0302 0.0283 0.0354 0.03 +63 0.0373 0.04 0.0406 0.0396 0.0394 0.0412 0.044 +4 0.0587 0.0842 0.1232 0.1616 0.1877 0.1871 0.15 +79 0.1125 0.0627 0.025 0.0021 -0.0055 -0.0047 -0 +.001 -0.0015 -0.0006 0.0019 0.0012 0.0002 -0.0003 + 0.0001 0.0009 0.0008 -0.0003 0.0006 0.0009 -0.000 +3 0.0011 0.0008 -0.0001 0.0007 -0.0008 -0.0004 0 + -0.0004 -0.0007 0.001 0.0002 0.0014 0.0013 0.00 +03 0.001 -0.0001 -0.0004 -0.0006 -0.0013 -0.0011 2 -0.0524 -0.0357 -0.0081 0.0061 0.0159 0.0226 0. +024 0.03 0.0339 0.0376 0.041 0.0477 0.0521 0.054 +4 0.0562 0.0568 0.065 0.0642 0.0621 0.0584 0.058 +9 0.064 0.0625 0.0649 0.0637 0.0703 0.068 0.0659 + 0.068 0.0754 0.0763 0.0722 0.0656 0.0655 0.0645 + 0.0646 0.0649 0.0702 0.0667 0.0663 0.0648 0.063 + 0.0641 0.0649 0.0685 0.0668 0.0686 0.0695 0.068 +1 0.0675 0.0695 0.0737 0.0747 0.0784 0.0826 0.10 +77 0.1839 0.3124 0.4273 0.4713 0.4307 0.321 0.17 +82 0.055 -0.0061 0.0001 0.0256 0.0312 0.0308 0.0 +305 0.03 0.0274 0.03 0.0297 0.0328 0.0345 0.0365 + 0.0376 0.04 0.041 0.04 0.0429 0.0471 0.0616 +0.0889 0.1262 0.1636 0.1871 0.1837 0.1518 0.1051 + 0.0562 0.0188 -0.0013 -0.0067 -0.0044 -0.0007 -0.0 +001 -0.0015 0.0005 0.0009 -0.0005 -0.0018 -0.0007 + 0.0007 0.0016 -0.0001 0.0006 0.0015 -0.0006 -0.00 +05 -0.0008 0.0003 0.0004 0.0006 0.0004 0.0011 0. +0004 0.0004 0.0007 -0.0005 0.0002 -0.0002 -0.0007 + -0.0007 -0.0007 0.0006 0.0005 -0.0004 -0.0008
        Untested code:
        ### Open the file if (open (FH, "file.txt")) { ### Run through each line of the file (line will be put in $_) while (<FH>) { ### Set the maximum to -1 my $max = -1; ### Run through all the numbers on this line while (m/([0-9\.\-]+)/g) { ### number is put in $1 by regexp if ($1 > $max) { $max = $1; } } print "$max\n"; } close (FH); }
        This could be a base for the program you want. I don't really get the stuff about 'derivative' so i ignored it.
Re: foreach nightmare!
by perlguy (Deacon) on May 08, 2003 at 16:22 UTC

    Surround this with your loop and you may be golden (there are better regexes to match a floating number, but this should serve your purpose with the above dataset):

    my ($highest) = sort { $b <=> $a } $string =~ /([\d\.-]+)/g; print "$highest\n";