Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

display problem

by steph_bow (Pilgrim)
on Mar 04, 2008 at 09:31 UTC ( [id://671830]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

This script runs but there is a problem of dipslay of the results.

Here is the input file

*** *** *** HI COCORICO -song HELLO -chicken 250420 -forest -data data -data 1 *** *** ***

Here is the script

#!/usr/bin/perl use strict; my $inFile = q{EXEMPLE_2.txt}; open my $INFILE, q{<}, $inFile or die; my $outFile = q{results_2.csv}; open my $OUTFILE, q{>>}, $outFile or die; while (<$INFILE>) { #lecture du fichier if (/^HI( |\t)*COC/) { my $song; my $hour_dla; while (<$INFILE>) { if ( /(^-)(song.*)/) { $song = $2; chomp($song); } if ( /^-chicken.*(\d\d)(\d\d)(\d\d)/) { $hour_dla = join(":",$2,$3); print $OUTFILE "$song;$hour_dla\n"; } last if (/^HI.*/) } } } close $INFILE; close $OUTFILE;

Here are the results

song HELLO ;04:20

I would have liked

song HELLO;04:20

The "chomp" commande does not seem to work. Could you check ? Thanks a lot

Replies are listed 'Best First'.
Re: display problem
by moritz (Cardinal) on Mar 04, 2008 at 09:35 UTC
    Just chomp all data that you read immediately. The following works for me:
    #!/usr/bin/perl use strict; while (<DATA>) { #lecture du fichier chomp; if (/^HI( |\t)*COC/) { my $song; my $hour_dla; while (<DATA>) { chomp; if ( /(^-)(song.*)/) { $song = $2; } if ( /^-chicken.*(\d\d)(\d\d)(\d\d)/) { $hour_dla = join(":",$2,$3); print "$song;$hour_dla\n"; } last if (/^HI.*/) } } } __DATA__ HI COCORICO -song HELLO -chicken 250420 -forest -data data -data 1

      Dear Moritz,

      Thanks a lot for your help

      But this is what I get from the display

      ;04:20ELLO

      Instead of

      song HELLO;04:20

      But it works when I do

      chop($song);

      Could you tell me why ? Thanks a lot

        Maybe your line endings are not the same as $/?

        The line endings on linux (LF), windows (CR LF) and Mac OS (CR? not sure...) are different, so if the input file has different line endings than your system, you have to set $/ to that value manually.

        chop just removes the last character, independently of what it is.

        The most generic solution might be $song =~ s/\s+$//, which removes all trailing whitespaces, and all line endings are considered to be whitespaces.

        See perlvar for the meaning of $/.

Re: display problem
by apl (Monsignor) on Mar 04, 2008 at 10:52 UTC
    The following should show you what's giving you grief. (Either you have a non-printing character in your file, or your line terminator isn't what you think it is.) Put it in your code immediately after the chomp.

    print sprintf( "Terminal %x\n", substr($song, -1) );

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://671830]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-24 14:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found