Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Skip 21 Lines

by qball (Beadle)
on Apr 24, 2001 at 19:36 UTC ( #75101=note: print w/ replies, xml ) Need Help??


in reply to Skip 21 Lines

You all have great comments. How would I incorporate any one of those code snippets into the following code:

open (DATA, "<file.txt") or die "Can't open file $!\n"; my @DATA = <DATA>; close (DATA); foreach $rec (@DATA) { chomp $rec; ($var1, $var2, $var3, $var4) = split(/,/,$rec); $var1 =~ s/\s+$//g; $var1 =~ s/"//g; $var2 =~ s/\s+$//g; $var2 =~ s/"//g; $var3 =~ s/\s+$//g; $var3 =~ s/"//g; $var4 =~ s/\s+$//g; $var4 =~ s/"//g; $compHash{$var1} = $var2; $aliasHash{$var3} = $var1; }

This code works great, just needs to skip the first 21 lines.

qball~"I have node idea?!"


Comment on Re: Skip 21 Lines
Download Code
Re: Re: Skip 21 Lines
by turnstep (Parson) on Apr 24, 2001 at 20:28 UTC
    open (DATA, "<file.txt") or die "Can't open file $!\n"; <DATA> while $. < 21; my @DATA = <DATA>; close (DATA);
      Nice use of an obscure variable (the obfuscator in me salutes you), but you'd better hope that the file is at least 22 lines long, or you'll be waiting quite a while for your data :-)
         MeowChow                                   
                     s aamecha.s a..a\u$&owag.print
        Point taken (although I don't think of $. as particularly obscure.) Slight modification:
        1 while <DATA> and $. < 21;
Re: Re: Skip 21 Lines
by greenFox (Vicar) on Apr 25, 2001 at 04:22 UTC
    You are repeating quite a bit of code there :) Try this (partially tested)-
    my $file = 'file.txt'; # open for input is default and now our die tells us which # file we were trying to open open (DATA, $file) or die "Can't open $file $!\n"; # assuming you aren't using @DATA for something else later # there is no need to slurp it all into memory now while (<DATA>) { next unless $. > 21; chomp; my @temp=(); foreach (split /,/){ s/"//g; # strip the quotes first if you want s/\s+$//; # to remove trailing whitespace push @temp, $_; } $compHash{$temp[0]} = $temp[1]; $aliasHash{$temp[2]} = $temp[3]; # you did mean $var4? } close (DATA);

    I am sure it could be reduced further with a judicious use of map but I can't get my head around it this morning :(

    Since you are parsing quoted CSV data you may want to look at Text::CSV which is a standard module or tilly's Text::xSV which is also available from CPAN.

    --
    my $chainsaw = 'Perl';

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2014-12-21 23:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (109 votes), past polls