Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^4: skip junk lines in csv before header

by AnomalousMonk (Archbishop)
on Jul 28, 2022 at 15:28 UTC ( [id://11145815]=note: print w/replies, xml ) Need Help??


in reply to Re^3: skip junk lines in csv before header
in thread skip junk lines in csv before header

It occurs to me that there might be a use for a do-block. If no 'fieldname' string is present in the file, <$fh> will read to the end of the file. This will be a valid read. If the data of the read is returned as from a do-block, it can be tested to determine if a header was actually present. (The data could be returned from a "naked" block, but using a do-block is neater IMHO.)

Win8 Strawberry 5.8.9.5 (32) Thu 07/28/2022 11:22:44 C:\@Work\Perl\monks >perl use strict; use warnings; use Data::Dump qw(dd); use constant FIELDNAME => 'fieldname'; open my $fh, '<', \<<END or die; no valid header present value1,value2 END my $got_header = do { local $/ = FIELDNAME; <$fh>; }; dd $got_header; # for debug $got_header =~ m{ \Q${ \FIELDNAME }\E \z }xms or die "no header"; my $header_line = FIELDNAME . <$fh>; # complete the line # and so on... ^Z "no valid header present\nvalue1,value2\n" no header at - line 16, <$fh> line 1.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^5: skip junk lines in csv before header
by LanX (Saint) on Jul 28, 2022 at 15:45 UTC
    well, yes if you want to capture the first 'readline', a do block is better, because of all the scoping.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

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

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

    No recent polls found