#!/usr/local/bin/perl
use strict; # Always use these two parmas.
use warnings;
#$file = </home/adedayo/Downloads/one27>;
$file = '/home/adedayo/Downloads/one27';
#open( INFO, $file ) or die( print "FILE NOT FOUND" );
open( my $INFO, $file ) or die( print "FILE NOT FOUND" );
#while ( my $line = <INFO> ) {
while ( my $line = <$INFO> ) {
@dayo = split( /\|/, $line );
%hash = ( 'col1' => "$dayo[3] $dayo[5]" );
while ( ( $key, $value ) = each(%hash) ) { # There is only one key, col1.
$timeout == 300;
$coun = 0;
if (
defined $last_time{"$dayo[5]|$dayo[3]"} <= # see note 1 below
$last_timestamp["$dayo[5]|$dayo[3]"] + "$timeout" )
{
foreach ($value) { # Loop can only run once per line. (See Chris's post)
$coun++;
print "$value $coun\n ";
}
}
else {
print "mala";
}
$last_timestamp{"$dayo[5]|$dayo[3]"} = $dayo[1];
}
}
#close(INFO) or die( print "cannot close" );
close( $INFO ) or die( print "cannot close" );
=notes
Note 1:
This statement is valid perl (without use strict), but it is unlikely that it
does what you intend.
Neither of the hashes %last_time or %last_time_stamp are defined at the
first time that this statement is executed. (%last_time is never defined).
What do you expect to happen when you add a number ($timeout) to a string
which contains the pipe character and possibly other non-numeric
characters (depending on the data in your input file)?
Do you expect "defined" to execute before the "<="? If so, what do you
expect it to return? According to "perldoc -f defined", it returns a boolean
value. Its numeric value is not specified. If after, what do you expect
the function "defined" to do? Even if you are certain which way that perl
defaults, give the rest of us a break and use parans to make your intention
clear.
I think you want the if to pass only if all of these conditions are true;
The hash is defined
the key does exist
the value corresponding to that key is defined.
the <= is true.
It takes a little more code, but it is much easier to test them seperately.
Note 2
We stongly recommend that you always use strict. It helps to prevent many of
the errors you have made.
"use strict" will require you to declare all your variables (with my). When you
add this statement, you will get a huge number of error messages. Fix a few at a time
and use perl -c to check your work. Often one fix will resolve
several messages (and occassionaly reveal more errors). Repeat until the
syntax is OK. Ask for help on details you cannot fix. Please provide a small
sample of your input.
=cut
Good Luck,
Bill