Steve Bertrand wrote:
On 2012.01.26 12:59, Chris Stinemetz wrote:

#!/usr/bin/perl
use warnings;
use strict;

while (<DATA> ) {
chomp;
my @array = split;
my $GeneID = $array[6];

if ($GeneID =~ /^C|D/) {
print $_,"\n";
}
}

__DATA__
Line1 c 2 3 4 5 C 7 8 9
Line2 1 2 3 4 5 6 7 8 9
Line3 1 2 3 4 5 D 7 8 9
Line4 1 2 3 4 5 6 7 8 9
Line5 1 2 3 4 5 D 7 8 9
Line6 1 2 3 4 5 6 7 8 9

***output***
Line1 c 2 3 4 5 C 7 8 9
Line3 1 2 3 4 5 D 7 8 9
Line5 1 2 3 4 5 D 7 8 9

This code erases the contents of @array and creates a new instance on
each loop of while(). If I'm not mistaken, the OP needs to save the
subset outside of the loop. I've modified your code a bit:

#!/usr/bin/perl
use warnings;
use strict;

my @array;

while ( <DATA> ) {

chomp;

my $GeneID = ( split( /\s+/, $_ ))[6];

Or just:

my $GeneID = ( split )[ 6 ];

Why make something more complicated than it needs to be? And change the semantics?


if ($GeneID =~ /^C|D/) {
push( @array, $_ );
}
}

print "$_\n" for @array;

That's basically the same as Chris' code except that you use more memory to do the same thing

Also, why chomp the input and then add a newline on the output?


__DATA__
Line1 c 2 3 4 5 C 7 8 9
Line2 1 2 3 4 5 6 7 8 9
Line3 1 2 3 4 5 D 7 8 9
Line4 1 2 3 4 5 6 7 8 9
Line5 1 2 3 4 5 D 7 8 9
Line6 1 2 3 4 5 6 7 8 9

Note that the shift() line does the same thing yours does, but

"shift() line"???


eliminates the step of having to assign to a temporary array variable.
All of the logic could technically be shortened to:



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to