Re: Skip 21 Lines
by merlyn (Sage) on Apr 24, 2001 at 18:59 UTC
|
Good time to bring out that underused "flip-flop" operator:
while (<HANDLE>) {
next if 1..21;
... rest of processing here ...
}
-- Randal L. Schwartz, Perl hacker | [reply] [d/l] |
|
That one is a weird one to get your head around... I never
think of it. It is very cool, though. Of course, you could always do...
while (<HANDLE>) {
last unless 1..21;
}
... rest of processing here ...
to prevent that check running for the following lines,
but that's just downright ANAL :)
- Ant | [reply] [d/l] |
|
| [reply] |
|
|
|
Any reason you wouldn't use:
<HANDLE> for 1..21;
...
MeowChow
s aamecha.s a..a\u$&owag.print | [reply] [d/l] |
|
| [reply] [d/l] [select] |
Re: Skip 21 Lines
by davorg (Chancellor) on Apr 24, 2001 at 18:54 UTC
|
while (<FILE>) {
unless (1 .. 21) {
# process the line - which is in $_
}
}
--
<http://www.dave.org.uk>
"Perl makes the fun jobs fun
and the boring jobs bearable" - me
| [reply] [d/l] |
Re: Skip 21 Lines
by arturo (Vicar) on Apr 24, 2001 at 18:52 UTC
|
Gee, if only computers could count!
for (my $i = 0; $i < 21; $i++) {
<FILEHANDLE>;
}
# now read
| [reply] [d/l] |
Re: Skip 21 Lines
by suaveant (Parson) on Apr 24, 2001 at 18:52 UTC
|
| [reply] |
Re: Skip 21 Lines
by greenFox (Vicar) on Apr 24, 2001 at 18:55 UTC
|
| [reply] |
Re: Skip 21 Lines
by qball (Beadle) on Apr 24, 2001 at 19:36 UTC
|
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?!" | [reply] [d/l] |
|
open (DATA, "<file.txt") or die "Can't open file $!\n";
<DATA> while $. < 21;
my @DATA = <DATA>;
close (DATA);
| [reply] [d/l] |
|
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 | [reply] |
|
|
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'; | [reply] [d/l] |
Re: Skip 21 Lines
by Beatnik (Parson) on Apr 24, 2001 at 19:16 UTC
|
Something I always use for the xth line of a file....
(undef,undef,undef,undef,$line) = <FILE>;
Greetz
Beatnik
... Quidquid perl dictum sit, altum viditur. | [reply] [d/l] |
|
| [reply] [d/l] |
|
Isn't it more maintainable to write that as:
$line = (<FILE>)[4];
? | [reply] [d/l] |
|
|
A reply falls below the community's threshold of quality. You may see it by logging in.
|
Re: Skip 21 Lines
by satchboost (Scribe) on Apr 24, 2001 at 22:39 UTC
|
Ummm .... why not use seek? Something like:
open MY_FILE, $filename;
seek MY_FILE, 21, 0;
while (<MY_FILE) {
... do reading schtuff here ...
}
That way, you go right to where you want to go. No silly while loops or anything like that. | [reply] [d/l] |
|
| [reply] |
|
whoops. I'm a dunce! That'll teach me to think I can see something easy that 4 monks before me didn't.
| [reply] |