in reply to Parsing in perl?

#! usr/bin/perl -w open(FILE,"<gene.txt"); my @lines=<FILE>; close(FILE); foreach my $line(@lines) { if($line=~/:/) { print "$`\n"; } }

output
BRCA1 TNF OMG

PS: $` is a special variable used to extract data before the regular expression

Replies are listed 'Best First'.
Re^2: Parsing in perl?
by Anonymous Monk on Dec 08, 2011 at 05:23 UTC

    Or without the $` ( $PREMATCH ) imposed penalty ( see, depending on your shell, perldoc -v "$`" or perldoc -v '$`') , its more Modern Perl-ish

    #!usr/bin/perl -- use strict; use warnings; use autodie qw/ open close /; # error checking open my($file), '<', 'gene.txt'; while( <$file> ){ print $1 if /^([^:]+):/; } close $file;