-1

I am beginner perl programmer. I have following perl code snippet and can not understand why I can not print the ascii characters ($ascii_chr) to OUTFILE. it seems to work fine when i print to console.

Can not understand why it is not dumping chacracters to OUTFILE in following print statement

 print OUTFILE $ascii_chr;  ## But this print statement does not work.  

Here is a code snippet of this particular loop.

#Now look for start_conversion = 1 and then convert the hex data into ascii data
if (($start_conversion == 1) && ($_ != '5f535452') && ($_ != '5f454e44'))  {
    chomp;
    $_ =~ s/000000/ 0x/g;
    @my_array = split(/ /, $_); 
    foreach $split_word(@my_array) {
        $ascii_chr = chr(hex($split_word));
        print $ascii_chr;       ## This print statement works.  
        print OUTFILE $ascii_chr;  ## But this print statement does not work.  
    }
}

Here is complete code. I tried several things but could never print to OUTFILE.

#!/usr/bin/perl
$num_arg = $#ARGV + 1;
if (($ARGV[0] =~ /help/)) {
 print "post_code_log.pl <inputfile> <outputfile> \n";
 exit;
}
$infile_name = $ARGV[0];
$outfile_name = $ARGV[1];

#Logic to remove non-ascii characters from a text file
$count            = 0;
$start_num        = '5f535452';
$stop_num         = '5f454e44';
$start_conversion = 0;
open (DATA,    "$infile_name");
open (OUTFILE, ">$outfile_name");
while (<DATA>) {
    s/^;.*//g;  # Remove a line starting with ;
    s/^\n//g;   # Remove blank lines
    s/.*?://;   # Remove first column 
    s/.*?Port80Wr//; # Remove the first column look for Port80Wr  
                     # (since the first column contains "Port80Wr")
    s/^\s+//g;  # Remove the space in front

    #Look for start signature
    if ($_ =~ '5f535452') {
        print $_;
        $start_conversion = 1;
    } 
    #Look for stop signature 
    if ($_ =~ '5f454e44') {
        print OUTFILE "\n";  # May need to print newline
        print $_;
        $start_conversion = 0;
    }
    # Now look for conversion start and then convert hex to ascii 
    if (($start_conversion == 1) && ($_ != '5f535452') && ($_ != '5f454e44')) {
        chomp;
        $_ =~ s/000000/ 0x/g;
        @my_array = split(/ /, $_); 
        foreach $split_word(@my_array) {
            $ascii_chr = chr(hex($split_word));
             print $ascii_chr;
             print OUTFILE $ascii_chr;
        }
    }
    # Now look for conversion end and send the data 
    # as is without making any changes
    if (($start_conversion == 0) && ($_ != '5f535452') && ($_ != '5f454e44')) {
        print OUTFILE $_;
    }
}
print "Done\n";
TLP
  • 66,756
  • 10
  • 92
  • 149
Charuben Pandya
  • 41
  • 1
  • 1
  • 3
  • You should use [`use strict; use warnings;`](http://stackoverflow.com/q/8023959/725418). – TLP Jan 08 '14 at 16:30
  • If you add `use warnings` you might get information about what is wrong. I am guessing you will get `print() on unopened filehandle OUTFILE`. – TLP Jan 08 '14 at 16:58
  • Are you checking after the program is done or while it's still running? It may simply not have been written to the file *yet* – ikegami Jan 08 '14 at 17:43
  • I am checking after the program completed running. There are other places where the print to OUTFILE handle works fine. For example Here #Now look for start_conversion = 0 in this case just send the data out as is without making any changes if (($start_conversion == 0) && ($_ != '5f535452') && ($_ != '5f454e44')) { print "In Stop_conversion\n"; print OUTFILE $_; } } – Charuben Pandya Jan 08 '14 at 17:50
  • The script requires a folowing foo.txt file – Charuben Pandya Jan 08 '14 at 17:52
  • 1595695: Port80Wr 00000201 ; flushed; num_trans_seen = 4, total bits (inc headers) = 128(300) 155287839: Port80Wr 0000c00b 155734703: Port80Wr 0000c10b 187741247: Port80Wr 5f535452 187907791: Port80Wr 00000045 187935823: Port80Wr 0000004d 187939343: Port80Wr 0000004e 187942943: Port80Wr 5f454e44 187928815: Port80Wr 00000020 187932319: Port80Wr 00000030 187935823: Port80Wr 0000000a 187939343: Port80Wr 0000000a – Charuben Pandya Jan 08 '14 at 17:53
  • post_code_log.pl foo.txt test.txt – Charuben Pandya Jan 08 '14 at 17:54
  • 2
    @CharubenPandya Don't add code and file content in comments, use the `edit` button and add it to the main body of your question. – TLP Jan 08 '14 at 18:22

1 Answers1

1

Check if your file is really open:

open (OUTFILE, ">", $outfile_name) or die "Cannot open '$outfile_name': $!";

Also add this near the top of your code:

use warnings;
TLP
  • 66,756
  • 10
  • 92
  • 149
toolic
  • 57,801
  • 17
  • 75
  • 117
  • 6
    It's better to use the 3 argument form of open. http://stackoverflow.com/questions/1479741/why-is-three-argument-open-calls-with-autovivified-filehandles-a-perl-best-pract – hmatt1 Jan 08 '14 at 16:41
  • The output file is open. As it is printing from other loops in the perl code above. – Charuben Pandya Jan 08 '14 at 17:03
  • @CharubenPandya There are no other loops in the Perl code above that is printing to the OUTFILE file handle. – TLP Jan 08 '14 at 17:25
  • Following code usese OUTFILE handle – Charuben Pandya Jan 08 '14 at 17:34
  • # Now look for conversion end and send the data # as is without making any changes if (($start_conversion == 0) && ($_ != '5f535452') && ($_ != '5f454e44')) { print OUTFILE $_; } } – Charuben Pandya Jan 08 '14 at 17:36
  • @CharubenPandya That is not a loop, it is a block. I thought you might be referring to code that you had not yet shared. – TLP Jan 08 '14 at 18:23
  • But the print to OUTFILE handle in this block is working. and it is now working in other block – Charuben Pandya Jan 08 '14 at 18:25
  • @CharubenPandya There are several errors in your code, which you need to fix by adding `use warnings`. There is no point in discussing your code any further if you do not fix these errors. By not fixing the errors, you will never have working code. For example, using `!=` with a non-numeric string (including hex) will give the warning `Argument "5f...." isn't numeric in numeric ne (!=)`. If you try to do `if ($_ != '5f454')` it will only be true if `$_ == 5`. – TLP Jan 08 '14 at 18:47
  • That is to say, until you add `use warnings` to your script, anything we say to you is useless, because of things happening on your end that are invisible to you and is preventing your script from working. – TLP Jan 08 '14 at 18:51