Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

skip blank lines that may contain from time to time some ";"

by steph_bow (Pilgrim)
on Jun 01, 2009 at 16:52 UTC ( [id://767318]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks, I am working with my Perl program on csv files. These csv files contained some information lines, gathered in blocks.

RWY_protection_1;80 -0.701399;44.840355 -0.698625;44.838375 -0.728149;44.817631 -0.730969;44.819683 -0.701399;44.840355 RWY_protection_2;80 -0.696316;44.826075 -0.697434;44.823624 -0.731108;44.830796 -0.730151;44.833011 -0.696316;44.826075 Restricted_area_protection_localizer_1;80 -0.734401;44.816223 -0.732972;44.815214 -0.734348;44.814279 -0.735763;44.815288 -0.734401;44.816223

Everything was OK until I made some modifications in my files and then there was some ";" instead of the blank lines. Thus my code did not work.

I don't know how to modify it so that even if some ";" appear (several per line), it will still work.

Here is a part of my code

while(my $line = <$FILE>){ $line =~ s/\s+$//; if ($line =~ /(^\w+)/){ ... } }

Replies are listed 'Best First'.
Re: skip blank lines that may contain from time to time some ";"
by ikegami (Patriarch) on Jun 01, 2009 at 17:04 UTC
    while(my $line = <$FILE>){ next if $line =~ /^\s*(?:;\s*)*$/; ... }
Re: skip blank lines that may contain from time to time some ";"
by almut (Canon) on Jun 01, 2009 at 17:04 UTC
    ...and then there was some ";" instead of the blank lines.

    You mean like this?

    ... -0.701399;44.840355 ; RWY_protection_2;80 ...

    Then why not simply remove it before doing the other processing?

    $line =~ s/^\s*;\s*$//;
Re: skip blank lines that may contain from time to time some ";"
by John M. Dlugosz (Monsignor) on Jun 01, 2009 at 19:44 UTC
    I think there is a CSV module in CPAN that handles everything, and has been updated through hard battles against files you have yet to dream about.
Re: skip blank lines that may contain from time to time some ";"
by perliff (Monk) on Jun 02, 2009 at 09:55 UTC
    perhaps another way could be to only look at those lines that contain a number and ignore all other lines?
    ----------------------

    -with perl on my side

    "If you look at the code too long, the code also looks back at you"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (8)
As of 2024-04-24 12:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found