in reply to Re: Extracting data from a particular line to end of the file
in thread Extracting data from a particular line to end of the file

Sorry the code looks like below

open (file1,"<$dir/seq.results"); while (<file1>){ chomp $_; @lines= split(/\n/,$_); foreach $line(@lines){ if ($line = ~m/^*/){ push (@final,$line); } } } foreach (@final) { open (file2,">$dir/seq.out"); print file2 $_; close file2; } close file1;

Replies are listed 'Best First'.
Re^3: Extracting data from a particular line to end of the file
by Marshall (Canon) on Dec 21, 2010 at 04:38 UTC
    The simple implementation of this is like below. Keep a record of whether the "start" has happened yet. And if it has, then print all the lines after that and including that one.

    code example and output.. click below..

    More specific version of your code:
    open (my $file1,"<$dir/seq.results") or die "cannot open "$dir/seq.res +ults"; open (my $file2,">$dir/seq.out") or die "cannot open $dir/seq.out" for + write"; my $started =0; while (<$file1>) { $started =1 if (m/^\*/); #first line starting with * is seen print $file2 "$_" if $started; }
    Note: '*' has special meaning within a regex and it has to be "escaped" to get the literal value of '*'.

    Yes, there are ways of coding this in a more brief way. But do not confuse brevity with execution efficiency and certainly not clarity.

    Oh, in a very simple program like this (6 lines), there is no need to explicitly close the file handles ($file1 and $file2). The OS will do that for you when the program exits. In longer programs there can be good reasons to do that.

      Thankyou marshall, That was really helpful...