Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Extracting lines starting with a pattern from an array

by CountZero (Bishop)
on Dec 16, 2015 at 21:27 UTC ( [id://1150549]=note: print w/replies, xml ) Need Help??


in reply to Extracting lines starting with a pattern from an array

Combining the IDs into one regex and then just reading line by line and matching the combined ID regex against the start of each string.

use Modern::Perl qw/2015/; my @regex = <DATA>; chomp @regex; my $regex = join '|', @regex; $regex = qr/$regex/; open( my $FH, '<', 'data.txt' ) or die "Could not open file: $!"; while ( my $line = <$FH> ) { print "Matched $1 at $line" if $line =~ m/^($regex)/; } __DATA__ GSAD1234 GSAD2345 GSAD4567

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^2: Extracting lines starting with a pattern from an array
by Alessandro (Acolyte) on Dec 17, 2015 at 16:15 UTC

    Thanks for the code, thanks all. But something really weird is happening... The script is returning only a single match (and I know for sure there should be more than one). I have tried a few other codes and they give me the same unique match as well.

    However I have written a dummy data set and the code works on it. So, knowing that my data have been given to me by someone and they are derived from an excel file, am I right to suspect that some kind of invisible characters are causing a problem?

      If you suspect that, you might try looking at the raw input data. For example, on *nix start with this

       $ od -cx input-data.txt > raw-input-data.txt

      and look at the output file.

        As an added bonus, od is part of the PerlPowerTools suite so you don't even have to rely on it being supplied as a binary by your O/S.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1150549]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-19 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found