in reply to Re: array processing
in thread array processing

OK having taken this on board , I decide to add a line number count so I don't go processing the same bit of the file every time this utility is run. e.g.

my $linenum = 1; my $lastline = 200; my %user_login; while (my $line = <errlog>) { chomp $line; next unless ($linenum = $lastline); next unless ($line =~ /Login succeeded/); my ($date,$time,$username)=(split(' ',$line))[0,1,6]; $user_login{$username}="$date $time"; ++linenum; } for my $record(sort keys %user_login) { print "$record logged in at $user_login{$record}\n"; } print "line number is $linenum\n";
This doesn't work though since $linenum is still set to 1 at the print above. How can I use the linenum variable outside the while loop to achieve this ?

Replies are listed 'Best First'.
Re^3: array processing
by tirwhan (Abbot) on Dec 06, 2005 at 12:58 UTC

    You've got an error in testing for your $linenum. Also, you're incrementing your variable after you test and loop back, so it never gets incremented. Change

    next unless ($linenum = $lastline);
    to
    next unless ($linenum++ >= $lastline);

    Update: sorry, I didn't do this carefully enough, changed to work


    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
      I must be doing something else wrong because it still doesn't work.
      I changed the line to
      next unless ($linenum < $lastline);
      because I want to process everything after the lastline. I still end up with linenum being set to 1 after the while loop has processed ?

      Many thanks