Re: skip lines
by moritz (Cardinal) on Jul 20, 2010 at 13:04 UTC
|
Something along these lines, maybe?
# three-argument form of open() is safer
open(my $input, '<', $filename ) || die "can't open $filename \n" ;
my $in_header = 1;
while (<$input>) {
if (/Parameter/) {
$in_header = 0;
next;
}
next if $in_header;
print $_;
}
Perl 6 - links to (nearly) everything that is Perl 6.
| [reply] [d/l] |
|
|
thanks for the reply, i tried it on that way but "@data" is still the same as before....the header is still there......
| [reply] |
|
|
My example code doesn't use @input at all. But if you push $_ onto @input instead of printing it, you should get the desired result. (Note: start off with an empty @input array).
Perl 6 - links to (nearly) everything that is Perl 6.
| [reply] [d/l] [select] |
|
|
|
|
|
|
Re: skip lines
by oko1 (Deacon) on Jul 20, 2010 at 16:12 UTC
|
First off, your script does not compile; please make sure to test your code before posting it here. Next - no, your '@data' isn't going to change: you're populating it from a file, and never doing anything to change it. Try something like this, instead:
#!/usr/bin/perl -w
use strict;
open INPUT, '<', "/home/test/test.txt" or die "test.txt: $!\n";
my ($seen, @data);
while(<INPUT>) {
$seen++ if /^Parameter/;
push @data, $_ if $seen;
}
close INPUT;
print @data;
This will print out the content of '@data', which will only be populated per your requirements.
--
"Language shapes the way we think, and determines what we can think about."
-- B. L. Whorf
| [reply] [d/l] |
|
|
thanks oko1. it is working now
| [reply] |
Re: skip lines
by Ratazong (Monsignor) on Jul 20, 2010 at 13:03 UTC
|
my $parameterFound = 0; # the fla
+g
while( my $line = <@data> ) # your code that claims to read th
+e file line-by-line
{
if ($line =~ /Parameter/) { $parameterFound = 1 ;} # start readi
+ng real data now
next if ($parameterFound == 0); # we are stil
+l in the header
print ....
}
HTH, Rata | [reply] [d/l] |
|
|
....thanks...but nothing changed. why to use a flag?
| [reply] |
Re: skip lines
by johngg (Canon) on Jul 20, 2010 at 22:35 UTC
|
Perhaps you could make use of the flip-flop operator (..) to push lines onto your @data array from the occurence of the "Parameter" line through to the end of file. Given this data file:-
Header Line 1
Header Line 2
Third Header Line
Parameter line here
and the
rest of the
data follows
This code:-
use strict;
use warnings;
my $dataFile = q{spw850417.data};
open my $dataFH, q{<}, $dataFile
or die qq{open: < $dataFile: $!\n};
my @data;
while ( <$dataFH> )
{
push @data, $_ if m{^Parameter} .. eof $dataFH;
}
close $dataFH
or die qq{close: < $dataFile: $!\n};
print @data;
Gives this output:-
Parameter line here
and the
rest of the
data follows
I hope this is something along the lines of your requirement.
| [reply] [d/l] [select] |
Re: skip lines
by the_hawk_1 (Scribe) on Jul 20, 2010 at 14:56 UTC
|
#!/usr/bin/perl
use strict;
use warnings;
my $dest_dir = "/home/test/";
my @data = "";
my $filename = "$dest_dir/test.txt";
my @lines = "";
open(INPUT,'<'.$filename ) || die "can't open $filename \n" ;
@data=<INPUT>;
# Shift the lines of the header
do {
my $line=shift(@data);
} while(!$line =~ /Parameter/);
print @data;
# Or play with @data as you wish, as the header is no longer in it...
Hope this can help you...
The_Hawk_1 - an ignorant monk to be...
| [reply] [d/l] |
|
|
thanks, but @data is the same like before...the header is still there
| [reply] |
Re: skip lines
by halligalli (Novice) on Jul 21, 2010 at 08:38 UTC
|
now its working. thanks all for the help | [reply] |