Problem with print statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jt2
    New Member
    • Feb 2008
    • 7

    Problem with print statement

    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.

    [code=perl]
    #! /usr/bin/perl

    open(LISTING, "$ARGV[0]");

    $line_count = 0;
    $value_count = 0;
    # skip first line of file
    $line_read = <LISTING>;

    $search_val = 1;
    $array_index = 0;
    # while not at end of file
    while ($line_read = <LISTING>) {
    $line_count++;

    chop ($line_read);

    # get values from line
    ($time, $status, $phase, $addr, $bft, $clk) = split(",", $line_read);

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

    #look for current search_val of clk
    if ($clk == $search_val) {
    # on the rising edge of the clock, capture the addr value
    if ($clk == 1) {
    #print "line count: " $line_count "\n" ;
    $lst[$array_index++] = $addr;
    $value_count++;

    #toggle the clock value we are searching for
    if ($search_val eq 1) {
    $search_val = 0;}
    else {
    $search_val = 1;}

    }
    else {
    #toggle the clock value we are searching for
    if ($search_val eq 1) {
    $search_val = 0;}
    else {
    $search_val = 1;}
    }

    }
    else {
    print "In Else, search value: ", $search_val, " clock value: ", $clk, "\n";
    }

    }
    print "line_count : ", $line_count, " value_count: ", $value_count, "\n" ;

    foreach $val (@lst) {
    print $val, ",";
    }
    print "\n";
    [/code]


    Here is the data file I am using:
    Time,STAT,CMD,A DDR,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";
    Last edited by numberwhun; Feb 13 '08, 02:55 PM. Reason: add code tags
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    maybe the file is not opening, do this:

    open(LISTING, $ARGV[0]) or die "Can't open $ARGV[0]: $!";

    Comment

    • jt2
      New Member
      • Feb 2008
      • 7

      #3
      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!

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        What are you expecting to get printed?

        Comment

        • jt2
          New Member
          • Feb 2008
          • 7

          #5
          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";

          Comment

          • eWish
            Recognized Expert Contributor
            • Jul 2007
            • 973

            #6
            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

            Comment

            • jt2
              New Member
              • Feb 2008
              • 7

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

              So, how about a smaller test program?
              Code:
              #! /usr/bin/perl
              
              
              #open input file
              #open(LISTING, "$ARGV[0]") or die "Can't open $ARGV[0]: $!";
              open(LISTING, "$ARGV[0]");
              
              #skip first line of file
              $line_read = <LISTING>;
              
              $line_read = <LISTING>;
              print $line_read;
              
              $search_val = 1;
              
              chomp ($line_read);
              print "$line_read\n";
              
              # get values from line
              ($time, $status, $phase, $addr, $bft, $clk) = split(",", $line_read); 
              
              print $line_read;
              
              print "clock: ", $clk, "   search value: ", $search_val, "\n";
              print "clock:  $clk   search value:  $search_val\n";
              print "search value:  $search_val clock:  $clk   \n";
                  
              print "$time\n";
              print "$status\n";
              print "$phase\n";
              print "$addr\n";
              print "$bft\n";
              print "$clk\n";
              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??
              Code:
              print "clock: ", $clk, "   search value: ", $search_val, "\n";
              print "clock:  $clk   search value:  $search_val\n";
              print "search value:  $search_val clock:  $clk   \n";

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                When I run your test script I get this:

                Code:
                1.1 ns,0,0,0,1,0
                1.1 ns,0,0,0,1,0
                1.1 ns,0,0,0,1,0clock: 0   search value: 1
                clock:  0   search value:  1
                search value:  1 clock:  0   
                1.1 ns
                0
                0
                0
                1
                0
                1.1 ns,0,0,0,1,0
                1.1 ns,0,0,0,1,0
                1.1 ns,0,0,0,1,0clock: 0   search value: 1
                clock:  0   search value:  1
                search value:  1 clock:  0   
                1.1 ns
                0
                0
                0
                1
                0
                1.1 ns,0,0,0,1,0
                1.1 ns,0,0,0,1,0
                1.1 ns,0,0,0,1,0clock: 0   search value: 1
                clock:  0   search value:  1
                search value:  1 clock:  0   
                1.1 ns
                0
                0
                0
                1
                0
                
                
                clock:    search value: 1
                clock:     search value:  1
                search value:  1 clock:
                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.

                Comment

                • jt2
                  New Member
                  • Feb 2008
                  • 7

                  #9
                  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.

                  Comment

                  • jt2
                    New Member
                    • Feb 2008
                    • 7

                    #10
                    KevinADC, why did your output have three copies of the expected output?

                    Comment

                    • KevinADC
                      Recognized Expert Specialist
                      • Jan 2007
                      • 4092

                      #11
                      because the input was three lines

                      Comment

                      • jt2
                        New Member
                        • Feb 2008
                        • 7

                        #12
                        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?

                        Comment

                        • KevinADC
                          Recognized Expert Specialist
                          • Jan 2007
                          • 4092

                          #13
                          Originally posted by 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?
                          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.

                          Comment

                          Working...