Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: getting a sequence of numbers and leters

by BillKSmith (Monsignor)
on Mar 23, 2020 at 22:59 UTC ( [id://11114585]=note: print w/replies, xml ) Need Help??


in reply to getting a sequence of numbers and leters

The following code reads the content of a (simulated) file, extracts the two values and tests them. A regex is defined for each field. They are combined into a single regex which describes the rest of the line and specifies that both values should be extracted. If the match is successful, both values are tested.
use strict; use warnings; use Test::Simple tests => 3; my $fname = \do { my $line = "BOGUS PPI3_SYNY3 276 aa contentar BCT 13-NOV-2019\n"; }; open my $FH, '<', $fname or die "Cannot open $fname for input"; my $content = <$FH>; close $FH; my $get_bogus = qr/ [A-Z0-9_]+ /x; my $get_acid = qr/ [0-9]+\s[a-z]+ /x; my ( $bogus, $acids ) = $content =~ / BOGUS \s+ ($get_bogus) \s+ ($get_acid) /x; if (ok( ( defined($bogus) and defined($acids) ), 'Match') ) { ok( $bogus eq "PPI3_SYNY3" , "extracted '$bogus'"); ok( $acids eq '276 aa', "extracted '$acids'"); }

OUTPUT:

1..3 ok 1 - Match ok 2 - extracted 'PPI3_SYNY3' ok 3 - extracted '276 aa'
Bill

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2024-04-23 13:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found