CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
counter problem


New User

Aug 12, 2010, 12:42 PM

Post #1 of 3 (1521 views)
counter problem Can't Post

I am trying to count the number of occurrences of each line in a file but the code only counts once , I would appreciate any help.

$file = </home/adedayo/Downloads/one27>;
open(INFO,$file)or die(print "FILE NOT FOUND" );
while (my $line = <INFO>){
@dayo =split(/\|/,$line);
%hash = ('col1' => "$dayo[3] $dayo[5]");
while(($key,$value) = each(%hash)){
$coun=0 ;
if( defined $last_time{"$dayo[5]|$dayo[3]"} <= $last_timestamp["$dayo[5]|$dayo[3]"]+"$timeout") {
foreach ($value){
$coun ++;
print "$value $coun\n ";}}
else{ print "mala";
$last_timestamp{ "$dayo[5]|$dayo[3]" } = $dayo[1];
close (INFO)or die (print "cannot close");


Aug 12, 2010, 7:36 PM

Post #2 of 3 (1499 views)
Re: [adebaki] counter problem [In reply to] Can't Post


There are quite a few "poor practices" in your code / possible mistakes. Also without seeing the input data / expected output, its difficult to comprehend whats going on at certain points.

However, the following line (the opening of the loop where you increment your counter):

   foreach ($value) {

is looping through a scalar variable, which only contains itself (1 element), therefore the foreach statement will only iterate once, therefore only counting once. Foreach statements typically require an array or a hash.


(This post was edited by Zhris on Aug 12, 2010, 7:43 PM)


Aug 13, 2010, 5:37 PM

Post #3 of 3 (1472 views)
Re: [adebaki] counter problem [In reply to] Can't Post

Please post in only one forum.

This script needs work from you before we can understand it.

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)
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" );

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
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.
Good Luck,


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives