in reply to Re: String related question
in thread String related question

I have written this code but it is wrong please help me in correcting this
$proteinfilename=<STDIN>; chomp $proteinfilename; open(FILENAME,$proteinfilename); @array=<FILENAME>; if ( @array=~m (/^AD/)){ print " @array"; }

Replies are listed 'Best First'.
Re^3: String related question
by bart (Canon) on Feb 02, 2011 at 12:37 UTC
    That is a reasonable attempt.

    Your error is that you're trying to apply the match to the whole array of lines at once, instead of to each line individually. Try this as a correction to your code:

    foreach my $line (@array) { if($line =~/^AD/){ print " $line"; } }
      Hi Thanks for correcting my code: Can you suggest me some links on net which , which step by step helps to learn Perl , any sort of good tutorial.
Re^3: String related question
by Anonyrnous Monk (Hermit) on Feb 02, 2011 at 12:53 UTC

    For tasks like you've described, there's no reason to read the entire file into an array. It's usually better to process the file line by line, because it scales in case your files should get huge. I.e. for, say, a 1 GB file, you'd need several gigs of RAM to hold the contents in the array, while if you process line by line, you'll only ever need storage for one line.

    open my $fh_in, "<", $proteinfilename or die "Couldn't open '$protein +filename': $!"; open my $fh_out, ">", $outfilename or die "Couldn't open '$outfilename +': $!"; while (my $line = <$fh_in>) { if ($line =~ /^AD/) { print $fh_out " $line"; } }