By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,240 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,240 IT Pros & Developers. It's quick & easy.

Problem with print statement

P: 7
Hello Perl Gurus!

I'm quite a perl neophyte so I'm looking for help understanding why print doesn't print correctly. This may be a totally stupid error....


He is my program. What it is doing is parsing a file of comma seperated values and trying to extract the address field every time the clock field transitions from a zero to a one.

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2.  
  3. open(LISTING, "$ARGV[0]");
  4.  
  5. $line_count = 0;
  6. $value_count = 0;
  7. # skip first line of file
  8. $line_read = <LISTING>;
  9.  
  10. $search_val = 1;
  11. $array_index = 0;
  12. # while not at end of file
  13. while ($line_read = <LISTING>) {
  14.     $line_count++;
  15.  
  16.     chop ($line_read);
  17.  
  18.     # get values from line
  19.     ($time, $status, $phase, $addr, $bft, $clk) = split(",", $line_read); 
  20.  
  21.     print "clock: ", $clk, "   search value: ", $search_val, "\n";
  22.  
  23.     #look for current search_val of clk
  24.     if ($clk == $search_val) {
  25.         # on the rising edge of the clock, capture the addr value
  26.         if ($clk == 1) {
  27.             #print "line count: " $line_count "\n" ;
  28.             $lst[$array_index++] = $addr;
  29.             $value_count++;
  30.  
  31.             #toggle the clock value we are searching for
  32.             if ($search_val eq 1) {
  33.                 $search_val = 0;}
  34.             else  {
  35.                 $search_val = 1;}
  36.  
  37.         }
  38.         else {
  39.             #toggle the clock value we are searching for
  40.             if ($search_val eq 1) {
  41.                 $search_val = 0;}
  42.             else  {
  43.                 $search_val = 1;}
  44.         }
  45.  
  46.     }
  47.     else {
  48.         print "In Else, search value: ", $search_val, " clock value: ", $clk, "\n";
  49.     }
  50.  
  51. }
  52. print "line_count: ", $line_count,  "  value_count: ", $value_count, "\n" ;
  53.  
  54. foreach $val (@lst) {
  55.     print $val, ",";
  56. }
  57. print "\n";
  58.  

Here is the data file I am using:
Time,STAT,CMD,ADDR,TRIG,CLK
1.1 ns,0,0,0,1,0
1.667 ns,0,0,0,1,1
1.667 ns,0,0,4,1,0
1.667 ns,0,0,104,1,0
88.333 ns,0,0,104,1,1
1.667 ns,0,0,104,1,1
48.333 ns,0,0,104,1,0
1.667 ns,0,0,26,1,0
85.000 ns,0,0,26,1,1
1.667 ns,0,0,26,1,1


And here is the console output:
% perl extract.pl tst.csv
search value: 1
In Else, search value: 1 clock value: 0
search value: 1
search value: 0
search value: 1
In Else, search value: 1 clock value: 0
search value: 1
search value: 0
In Else, search value: 0 clock value: 1
search value: 0
search value: 1
In Else, search value: 1 clock value: 0
search value: 1
search value: 0
In Else, search value: 0 clock value: 1
line_count: 10 value_count: 3
0,104,26,


What is wrong with this print statement in that "clock: " is never printed?
print "clock: ", $clk, " search value: ", $search_val, "\n";
Feb 13 '08 #1
Share this Question
Share on Google+
12 Replies


KevinADC
Expert 2.5K+
P: 4,059
maybe the file is not opening, do this:

open(LISTING, $ARGV[0]) or die "Can't open $ARGV[0]: $!";
Feb 13 '08 #2

P: 7
jt2
No, the file is opening and being processed just fine, and this program is working in that I am extracting the values that I want.

I'd like to understand why that print statement doesn't work correctly. I was adding print statements all over the place last night to try and debug this program, and I found that I had to debug the print statements! Doh!
Feb 13 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
What are you expecting to get printed?
Feb 13 '08 #4

P: 7
jt2
The print statement is:

print "clock: ", $clk, " search value: ", $search_val, "\n";


so I would expect to see something like:

clock: 1 search value: 0


to be printed out. The console output I get is:

% perl extract.pl tst.csv
search value: 1
In Else, search value: 1 clock value: 0
search value: 1
search value: 0
search value: 1
etc......

What happened to the "clock: $clk portion from the print statement
print "clock: ", $clk, " search value: ", $search_val, "\n";
Feb 13 '08 #5

eWish
Expert 100+
P: 971
I would suggest that you use the strict and warnings pargmas for starters. However, that would not necessarily show you your problem. Also, when you want to make a numerical comparison you would use == in lieu of eq which is for strings. Then it appears that chop should be chomp. Chomp removes a line endings and chop removes the last character. Since the value for clock is only one character long I would say that that is your problem.

Give that a try and see where you get.

--Kevin
Feb 13 '08 #6

P: 7
jt2
Ok, so its obvious that no one would even try running the program to verify the error.

So, how about a smaller test program?
Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2.  
  3.  
  4. #open input file
  5. #open(LISTING, "$ARGV[0]") or die "Can't open $ARGV[0]: $!";
  6. open(LISTING, "$ARGV[0]");
  7.  
  8. #skip first line of file
  9. $line_read = <LISTING>;
  10.  
  11. $line_read = <LISTING>;
  12. print $line_read;
  13.  
  14. $search_val = 1;
  15.  
  16. chomp ($line_read);
  17. print "$line_read\n";
  18.  
  19. # get values from line
  20. ($time, $status, $phase, $addr, $bft, $clk) = split(",", $line_read); 
  21.  
  22. print $line_read;
  23.  
  24. print "clock: ", $clk, "   search value: ", $search_val, "\n";
  25. print "clock:  $clk   search value:  $search_val\n";
  26. print "search value:  $search_val clock:  $clk   \n";
  27.  
  28. print "$time\n";
  29. print "$status\n";
  30. print "$phase\n";
  31. print "$addr\n";
  32. print "$bft\n";
  33. print "$clk\n";
  34.  
Here is the file read by the program:

junk line
1.1 ns,0,0,0,1,0
1.1 ns,0,0,0,1,0
1.1 ns,0,0,0,1,0


And here is the output from the program execution:

% perl test.pl tst2.csv
1.1 ns,0,0,0,1,0
1.1 ns,0,0,0,1,0
search value: 1
search value: 1
rch value: 1 clock: 0
1.1 ns
0
0
0
1
0


Why do these print statements not work correctly??
Expand|Select|Wrap|Line Numbers
  1. print "clock: ", $clk, "   search value: ", $search_val, "\n";
  2. print "clock:  $clk   search value:  $search_val\n";
  3. print "search value:  $search_val clock:  $clk   \n";
  4.  
Feb 14 '08 #7

KevinADC
Expert 2.5K+
P: 4,059
When I run your test script I get this:

Expand|Select|Wrap|Line Numbers
  1. 1.1 ns,0,0,0,1,0
  2. 1.1 ns,0,0,0,1,0
  3. 1.1 ns,0,0,0,1,0clock: 0   search value: 1
  4. clock:  0   search value:  1
  5. search value:  1 clock:  0   
  6. 1.1 ns
  7.  
  8.  
  9.  
  10. 1
  11.  
  12. 1.1 ns,0,0,0,1,0
  13. 1.1 ns,0,0,0,1,0
  14. 1.1 ns,0,0,0,1,0clock: 0   search value: 1
  15. clock:  0   search value:  1
  16. search value:  1 clock:  0   
  17. 1.1 ns
  18.  
  19.  
  20.  
  21. 1
  22.  
  23. 1.1 ns,0,0,0,1,0
  24. 1.1 ns,0,0,0,1,0
  25. 1.1 ns,0,0,0,1,0clock: 0   search value: 1
  26. clock:  0   search value:  1
  27. search value:  1 clock:  0   
  28. 1.1 ns
  29.  
  30.  
  31.  
  32. 1
  33.  
  34.  
  35.  
  36. clock:    search value: 1
  37. clock:     search value:  1
  38. search value:  1 clock:   
  39.  
the input was:

1.1 ns,0,0,0,1,0
1.1 ns,0,0,0,1,0
1.1 ns,0,0,0,1,0


I am stumped why your code prints out something different.
Feb 14 '08 #8

P: 7
jt2
Funny stuff huh?

I've tried running this script on two different machines, one Unix box running Solarix9 and perl version 5.6.1, and a Linux box running (unknown) version of Red Hat linux with (unknown) version of perl.

They both responded the same odd way...

Hmm, I AM accessing these machines by running EXCEED on my PC, which is an X windows utility.
Feb 14 '08 #9

P: 7
jt2
KevinADC, why did your output have three copies of the expected output?
Feb 14 '08 #10

KevinADC
Expert 2.5K+
P: 4,059
because the input was three lines
Feb 14 '08 #11

P: 7
jt2
Hmm, yeah, but I didn't have a loop in my smaller test program.

Did you add a loop to test all three lines?

Also, did you notice in your output that the third line didn't print out the clock value correctly?
Feb 14 '08 #12

KevinADC
Expert 2.5K+
P: 4,059
Hmm, yeah, but I didn't have a loop in my smaller test program.

Did you add a loop to test all three lines?

Also, did you notice in your output that the third line didn't print out the clock value correctly?
Yes, I looped through a three line "file". You can ignore that last part of the output, I should have left that out. Lines 1 thru 33 are the valid output, after that is a side-affect of running the code in my IDE and using __DATA__ instead of an external file.
Feb 14 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.