Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Skip a row of csv file.

by aaron_baugher (Curate)
on May 04, 2012 at 02:43 UTC ( #968850=note: print w/ replies, xml ) Need Help??


in reply to Skip a row of csv file.

Have you tried printing the return value of your substr function? That might offer a clue.

For what it's worth, while this solution will work (once you get the length right), I think most monks would use a regex:

if( $data[0] =~ /^A\.SERIAL_NUMBER\|\|/ ){

Aaron B.
My Woefully Neglected Blog, where I occasionally mention Perl.


Comment on Re: Skip a row of csv file.
Select or Download Code
Replies are listed 'Best First'.
Re^2: Skip a row of csv file.
by Tux (Monsignor) on May 04, 2012 at 06:15 UTC

    If the data is indeed CSV, and not pipe-separated, most monks would use a CSV parser and write

    while (my $row = $csv->getline ($fh)) { $row->[0] eq "A.SERIAL_NUMBER||" and next; :

    Giving the OP's code, I seriously doubt however that it is a CSV file.

    If the file is a pipe-separated-value file, that can also be read with Text::CSV or Text::CSV_XS.


    Enjoy, Have FUN! H.Merijn
Re^2: Skip a row of csv file.
by TomDLux (Vicar) on May 04, 2012 at 18:02 UTC

    While many people do use regular expressions, I think that's like using an M16 to crack walnuts.

    R.E. are the appropriate solution when you have variable components, but testing a constant string is precisely what 'eq' is for.

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

      You make a good point, but I found myself wondering how the combination of substr, length, and eq would compare to a single anchored regex, so I did a benchmark. I'm convinced!

      bannor:~/work/perl/monks$ cat 968838.pl #!/usr/bin/env perl use Modern::Perl; use Benchmark qw(:all); my $str = 'A.SERIAL_NUMBER||abcdefghijklmnopqrstuvwxyz'; my $ss = 'A.SERIAL_NUMBER||'; my $yay; cmpthese( 10_000_000, { 'substr' => sub { if(substr($str,0,length($ss)) eq $ss ){ $yay = 1; } }, 'regex' => sub { if( $str =~ /^\Q$ss\E/ ){ $yay = 1; } }, }); bannor:~/work/perl/monks$ perl 968838.pl Rate regex substr regex 1312336/s -- -65% substr 3802281/s 190% --

      Aaron B.
      My Woefully Neglected Blog, where I occasionally mention Perl.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (15)
As of 2015-07-31 16:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (279 votes), past polls