CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [Laurent_R] Search for text within a line of text: Edit Log



FishMonger
Veteran / Moderator

May 31, 2013, 7:20 AM


Views: 1175
Re: [Laurent_R] Search for text within a line of text


In Reply To
Using a hash as suggested bu Fishmonger is of course far more efficient, provided however that the fields in the files are equal. If it is not the case (the fact that you use a regex tells me it is probably not the case), then the two nested loops approach is the right solution.


I don't necessarily agree that nested loops would be the right solution. Here's a short example of a non nested loop using the contrived sample data.


Code
use 5.10.0; 
use strict;
use warnings;

my %fields = map { chomp; $_ => 1 } <DATA>;

foreach my $str ('ga016d400:KUX', 'blahblahblah:ga016D400:KYNA') {
my $wanted = (split /:/, $str)[-2];

if ( exists $fields{$wanted} ) {
say "$wanted is in $str";
}
}

__DATA__
ga016d4e
ga016d63
ga016d77
ga016d98
ga016a395
ga016d400
ga016D400


Outputs:
ga016d400 is in ga016d400:KUX
ga016D400 is in blahblahblah:ga016D400:KYNA

However, the format of the actual data probably does not conform to that sample, so this test case would need to be adjusted accordingly.


(This post was edited by FishMonger on May 31, 2013, 7:20 AM)


Edit Log:
Post edited by FishMonger (Veteran) on May 31, 2013, 7:20 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives