develooper Front page | perl.beginners | Postings from April 2012

Re: split

Thread Previous | Thread Next
From:
John W. Krahn
Date:
April 9, 2012 20:24
Subject:
Re: split
Message ID:
4F83A7E0.4060202@shaw.ca
Matthew Bonner wrote:
> Hi Anamika
>
> I know this thread has focussed on using split -- thought I'd add a
> regex powered version for reference/comparison.
>
> cheers
>
> Matthew
>
>
> use strict;
> use warnings;
>
> while (<DATA>) {
> 	my @keys;
> 	@keys = $_ =~ m/(NM_\d+)+/g;
> 	$_ =~ m/\:1\s+(.*)$/;
> 	print "$_ = $1\n" foreach (@keys);
> }

Your loop could be written more simply as:

while (<DATA>) {
	mt @keys = /NM_\d+/g;
	/:1\s+(.*)$/;
	print "$_ = $1\n" for @keys;
}

Your use of capturing parentheses with a quantifier, ()+, around the 
pattern is superfluous and would produce the wrong results if it 
actually matched more than once.

Your use of $1 without verifying that the pattern actually matched could 
also produce the wrong results.



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

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About