First, let's make your code readable by running it through perltidy:

#!/usr/bin/perl # use strict; use warnings; use List::Util q{first}; sub search_phrase { my @array; my ($inFile, @phrases) = @_; my $lastPhrase = $phrases[-1]; open my $inFH, q{<}, $inFile or die qq{open: $inFile: $!\n}; my @lines = <$inFH>; close $inFH or die qq{close: $!\n}; foreach my $phrase (@phrases) { my $rxPhrase = qr{\Q$phrase\E}; my $lineNo = first {$lines[$_] =~ $rxPhrase} 0 .. $#lines; unless (defined $lineNo) { #print "-1"; # print qq{$phrase: not found in sequence\n}; next; } print "" if ($lines[$lineNo] =~ m{\Q$lastPhrase\E\s*(\d*)}); push (@array, $1); $lineNo++; splice @lines, 0, $lineNo; } return (@array); } my $file_n = "665172.data"; my $phrase1 = "total rejected rows:"; my $phrase2 = "total rejected recors:"; my $phrase3 = "rejectb:"; my $phrase4 = "total rejected rows:"; my @newarray = search_phrase( $file_n, $phrase1, $phrase2, $phrase3, $phrase4 ); my $count = ($#newarray); $newarray[$count] =~ s/\s+//g; if ((($#newarray + 1) >= 1) && ($newarray[$count] gt 0)) { print "$newarray[$count]\n"; } else { print "-1\n"; }

Next, when I try running your code, it works fine, and you say it works fine for you, except when you ru it on a server.

So the obvious question (but one that I'll ask anyway) is, "What's the difference from your workstation and the server?" You're only using List::Util -- I imagine it's installed on the server, and the same version number. Is the data file the same?

Now onto the fun part: picking out stylistic miscues.

Those are my thoughts. What do you think?

Update: As both Narveson and wfsp have pointed out, for reads the entire file in, so doesn't achieve the goal I was reaching. Instead, use while. Oops.

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds


In reply to Re: uninitialized value error by talexb
in thread uninitialized value error by mercuryshipz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.