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

Hi, I have a simple 30 line perl script which basically just compares a bunch of numbers in a list. I'm a little rusty with perl as I haven't used it in a few years now, but no matter what I try, I can't get past the syntax error which is complaining about the line where I compare two numbers out of my array. I've given up and just can't see what is wrong. Is there a monk out there who could guide me please? I hate to post up on such a simple thing, but I'm totally stuck. Running the script complains of a syntax error:
$ ./perlAnalyzeDailyStats.pl syntax error at ./perlAnalyzeDailyStats.pl line 29, near "@array(" Execution of ./perlAnalyzeDailyStats.pl aborted due to compilation err +ors.
1 #!/usr/bin/perl 2 3 @files = `ls -1trh s3_dailystats.*`; 4 foreach (@files) 5 { 6 $filename = $_; 7 #print $filename; 8 open(FH,"<$filename"); 9 while(<FH>) 10 { 11 $line = $_; 12 if($line =~ /([0-9]{17,18})/) 13 { 14 print "$1\n"; 15 push(@array,$1); 16 last; 17 } 18 } 19 } 20 $arrayCount = @array; 21 print "array count is $arrayCount"; 22 foreach(@array) 23 { 24 if ($counter eq $arrayCount) 25 { 26 last; 27 } 28 $counter++; 29 print "difference is " . @array($counter) - @array($counte +r - 1); 30 } 31

Replies are listed 'Best First'.
Re: simplish script (I think)
by LanX (Saint) on Sep 25, 2020 at 00:30 UTC

    Please replace all @array($counter) with $array[$counter]

    There are more issues but it'll help you past the syntax errors

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: simplish script (I think)
by NetWallah (Canon) on Sep 25, 2020 at 00:33 UTC
    An array element is referenced using SQUARE brackets and a Dollar sigil.

    You should be using

    print "difference is " . $array[$counter] - $array[$counter - 1];
    Your second "foreach" is not necessary.
    It can be replaced by:
    print "difference is " . $array[$#array] - $array[-2] , "\n";

    See correction below

                    "Imaginary friends are a sign of a mental disorder if they cause distress, including antisocial behavior. Religion frequently meets that description"

      Your second "foreach" is not necessary.

      I think the OP is trying to do this with the second foreach:

      for(my $i=1; $i < @array; $i++){ print "The difference is ", $array[$i] - $array[$i-1], "\n"; }
        You are correct(++) - I misread the curly brackets.

        Another way he can achieve this - arguably more readable is:

        my @a=@array; # make a copy, since we will destroy @a my $prev=pop @a; for (reverse @a){ print $prev - $_, "\n"; $prev=$_; }

                        "Imaginary friends are a sign of a mental disorder if they cause distress, including antisocial behavior. Religion frequently meets that description"

Re: simplish script (I think)
by jdporter (Paladin) on Sep 25, 2020 at 20:11 UTC

    Sometimes I don't like using array indices. ;-)

    use List::Util qw( head tail ); use List::MoreUtils qw( pairwise ); print map "difference is $_ \n", pairwise { $b - $a } @{[head(-1,@array)]}, @{[tail(-1,@array)]};
    relevant :-)
Re: simplish script (I think)
by notoriousrab430 (Novice) on Sep 25, 2020 at 21:25 UTC
    Thank you to those who helped me. The way of doing the if statement was better than using "last;" like I was doing. Thank you for making the internet a better place.