1

So this is what should be of interest for you to be able to help me, the program is written in perl and I'm using both warnings and strict in perl but I get to the end of the text file and then I get stuck with the last output.

my $filename = 'trace-804-2.txt';
open(my $input, '<:encoding(UTF-8)', $filename)
or die "Couldn't open file '$filename' $!";

while ( <$input> )
{
    my ($row) = $_;
    chomp $row;
    ( $pkt_number[$i], $pkt_arrival[$i], $pkt_length[$i] ) = ( split(':',$row, 8)) [0,3,5];
    $pkt_number[$i] =~ s/\D//g;
    $pkt_length[$i] =~ s/\D//g;
    printf "Packet number %d ",$pkt_number[$i];
    printf "Arrival time %s ",$pkt_arrival[$i];
    printf "Length %d \n",$pkt_length[$i];
    $i++;
    $nr_of_pkts++;
}

and this is the output I get

 Packet number 3 Arrival time 2662.050878565250 Length 256
 Packet number 4 Arrival time 2662.050981740750 Length 256
 Packet number 5 Arrival time 2662.051084976000 Length 256
 Packet number 6 Arrival time 2662.051188151750 Length 256
 Packet number 7 Arrival time 2662.051291327500 Length 256
 Packet number 8 Arrival time 2662.051394562750 Length 256
 Packet number 9 Arrival time 2662.051469485750 Length 256
 Packet number 10 Arrival time 2662.051497738250 Length 256
 Packet number 11 Arrival time 2662.051573317000 Length 256
 Packet number 12 Arrival time 2662.051600914000 Length 256
 Packet number 13 Arrival time 2662.051677088750 Length 256
 Packet number 15 Arrival time 2662.051704149250 Length 256
 Packet number 17 Arrival time 2662.051780920000 Length 256
 Packet number 18 Arrival time 2662.051807324750 Length 256
 Packet number 20 Arrival time 2662.051890115750 Length 256
 Packet number 21 Arrival time 2662.051910560000 Length 256
 Packet number 23 Arrival time 2662.051993887250 Length 256
 Packet number 24 Arrival time 2662.052013735750 Length 256
 Packet number 27 Arrival time 2662.052097718750 Length 256
 Packet number 29 Arrival time 2662.052116911250 Length 256

I know it doesn't get further since I have a simple printf after the loop to mark that the program have gone past that stage.

And this is the content of the text file:

[   3]:d10:mp10165:2662.050878565250:LINK(1266):CAPLEN( 256)
[   4]:d10:mp10165:2662.050981740750:LINK(1266):CAPLEN( 256)
[   5]:d10:mp10165:2662.051084976000:LINK(1266):CAPLEN( 256)
[   6]:d10:mp10165:2662.051188151750:LINK(1266):CAPLEN( 256)
[   7]:d10:mp10165:2662.051291327500:LINK(1266):CAPLEN( 256)
[   8]:d10:mp10165:2662.051394562750:LINK(1266):CAPLEN( 256)
[   9]:d00:mp10165:2662.051469485750:LINK(1266):CAPLEN( 256)
[  10]:d10:mp10165:2662.051497738250:LINK(1266):CAPLEN( 256)
[  11]:d00:mp10165:2662.051573317000:LINK(1266):CAPLEN( 256)
[  12]:d10:mp10165:2662.051600914000:LINK(1266):CAPLEN( 256)
[  13]:d00:mp10165:2662.051677088750:LINK(1266):CAPLEN( 256)
[  15]:d10:mp10165:2662.051704149250:LINK(1266):CAPLEN( 256)
[  17]:d00:mp10165:2662.051780920000:LINK(1266):CAPLEN( 256)
[  18]:d10:mp10165:2662.051807324750:LINK(1266):CAPLEN( 256)
[  20]:d00:mp10165:2662.051890115750:LINK(1266):CAPLEN( 256)
[  21]:d10:mp10165:2662.051910560000:LINK(1266):CAPLEN( 256)
[  23]:d00:mp10165:2662.051993887250:LINK(1266):CAPLEN( 256)
[  24]:d10:mp10165:2662.052013735750:LINK(1266):CAPLEN( 256)
[  27]:d00:mp10165:2662.052097718750:LINK(1266):CAPLEN( 256)
[  29]:d10:mp10165:2662.052116911250:LINK(1266):CAPLEN( 256)
9
  • I am not sure if I understand what you mean by getting stuck with the last output. May 11, 2015 at 8:59
  • I have more code after, for example just for test purposes a printf "step one" so I can continue with my real purpose of the program. When I say stuck I mean that it doesn't continue with the program I. E. I see the last part of the included output but the program is supposed to continue
    – ricksson
    May 11, 2015 at 9:04
  • 1
    tunr on use strict; use warnings;. That may already help, e.g. $i is uninitialized in first round of while. I would prefer while(my $row = <$input>){ Your explanation of what happens after cannot be understood really well without code. I have not yet seen a while(<$input>) getting stuck on a file, since they tend to end, so I am guessing the problem is not a never-ending while here, but I may be wrong.
    – bytepusher
    May 11, 2015 at 9:21
  • Does the printf call after the loop include a newline? If not it may get stuck in stdout's line buffer. May 11, 2015 at 9:22
  • I had use strict and use warning included and I actually just changed to while(my $row = <$input>){ Yes, now with the result in my hand I should have added the code after too. @ChrisS was right, a simple addition of "/n" made it run like I wanted it to! Thanks a bunch
    – ricksson
    May 11, 2015 at 9:27

1 Answer 1

1

The error was outside of the while loop, in the printf a /n (newline) was missing:

}
close($filename);
sleep(1);
printf "step one";

and I changed it to:

}
close($filename);
sleep(1);
printf "step one \n";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.