monkjeff has asked for the wisdom of the Perl Monks concerning the following question:

When I read in a line from a file, I check for the octal value of 14 to show the 'SHIFT OUT' for a new page. However when I go and process the lines to create a new output document, it will not print the character for a 'SHIFT OUT'. I have even tried to past it in as hardcode and yet it still will not print.
#!/usr/bin/perl use CGI qw(:standard); open(INFO, "testdoc.txt"); print header(); print start_html(); my @record; my %hash; my $true = 0; $i =0; my $FEI; ($sec,$min,$hour,$mday,$mon,$year,$wday, $yday,$isdst)=localtime(time); #printf "%4d-%02d-%02d %02d:%02d:%02d\n", $year+1900,$mon+1,$mday,$hou +r,$min,$sec; open (OUT, '>OUTPUT.txt'); while ($line = <INFO>){ print $line . "<BR>"; #check each character of the line for a page break for (split//,$line) { $test = sprintf "\%3o\n", ord $_; if ($test == 14) { # WE HAVE FOUND A NEW PAGE $true = 1; $hash->{$i} = $line . $test; $i++; # store next 8 lines in a hash } } } }
This isn't the full code but a sample of the section I am talking about.

Replies are listed 'Best First'.
Re: ASCII insert after read
by moritz (Cardinal) on Jul 29, 2008 at 19:46 UTC
    open(INFO, "testdoc.txt");

    Always check for the success of your open:

    open INFO, '<', 'testdoc.txt' or die "Can't open testdoc.txt: $!";

    That way you won't get bad surprises if the file is not available.

    for (split//,$line) { $test = sprintf "\%3o\n", ord $_; if ($test == 14) {

    That's a really odd way to test if a certain character appears in a line. What about this one?

    my $char = chr oct 14; while (my $line = <INFO>){ if ($line =~ m/\Q$char\E/){ # do whatever you want } }

    You might also take a look at index.

      so how do I get that to print out. For some reason I cannot get it to show up on the output.
        Could you please be more specific? What do you want to show up in the output, and what do you expect?

        It would help if you could provide a small script along with some sample input data and your desired output. Then we can help you better.

Re: ASCII insert after read
by pc88mxer (Vicar) on Jul 29, 2008 at 19:40 UTC
    An easier way to test for the ASCII character 12 is to use a regex:
    if ($line =~ m/\014/) { # $line contains a Control-L }
    Update: I guess your approach can work, but it's a little awkward. You are comparing the octal representation of a number (which is a string) to a decimal value. Doing it your way I would have written it:
    $test = sprintf("%o", ord $_); if ($test eq "14") { ... }