in reply to uninitialized value error

You push $1 onto @array (push (@array, $1);) regardless of getting a match in the previous regex. Unless the last matching line in your file is a well formed $lastPhrase line, your code as it stands will fail as you have noted.

The following test code illustrates the problem:

use strict; use warnings; use List::Util qw{first}; my $file = <<FILE; total rejected recors: 10 FILE my @phrases = ( "total rejected rows:", "total rejected recors:", "rejectb:", "total rejected rows:" ); open my $inFile, '<', \$file; my @newarray = search_phrase ($inFile, @phrases); close $inFile or die qq{close: $!\n}; $newarray[$#newarray] =~ s/\s+//g; if (@newarray && $newarray[$#newarray] > 0) { print "$newarray[$#newarray]\n"; } else { print "-1\n"; } sub search_phrase { my ($inFile, @phrases) = @_; my @array; my $lastPhrase = $phrases[-1]; my @lines = <$inFile>; foreach my $phrase (@phrases) { # Find the first match in the remaining lines for the current +phrase my $lineNo = first { $lines[$_] =~ /\Q$phrase\E/ } 0 .. $#line +s; next unless defined $lineNo; # No line containing current phra +se print "Found: $lines[$lineNo]" if ($lines[$lineNo] =~ m{\Q$lastPhrase\E\s*(\d+)}); push (@array, $1); $lineNo++; splice @lines, 0, $lineNo; # Delete processed lines } return @array; }

Perl is environmentally friendly - it saves trees

Replies are listed 'Best First'.
Re^2: uninitialized value error
by mercuryshipz (Acolyte) on Jan 31, 2008 at 15:40 UTC
    Hi all,
    Thanks for all ur responses
    First I would like to explain what is this script all about.
    The arguments passed are file_name and Search phrases (for eg: reject:, total rejected recors:, total rejected rows:, and so on...)

    The number of arguments passed may vary... But the first argument is always the file_name.

    first it will check for phrase 1 and then from that line number phrase 2 and then phrase 3 and return phrase 3 value .

    that is why the order in which we search is very important...

    lets say we have 4 arguments including file name...

    if (phrase 1 exists) ---------- if (phrase 1 doesnt exists)

    from that line number ----------- search for phrase 2

    search for phrase 2 ---------- if (phrase 2 also doesnt exists)
    from that line number ---------- search for phrase 2 and return phrase 3

    even if last phrase was present before, we shud ignore it and follow the order in which the phrases are passed as arguments.... and return the last phrase's value (ie., 45)


    if the last phrase was not found in that file then it should print -1 and exit


    if the last phrase is given twice, it shud return the last found value in that given order.

    and one more last thing, this script works only for returning the value which is at the end of the line..
    but doesnt return the value which is at the beginning of the line.
    for eg:
    total rejected rows: 90
    it returns 90 or whatever value it is
    but not
    50 rows rejected
    that it doesnt return 50
    thanks,
    Mercury.