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:
Problems matching first pattern

 



jddaly
New User

Apr 6, 2012, 5:18 PM

Post #1 of 3 (627 views)
Problems matching first pattern Can't Post

It's probably really obvious but if I said I was rusty it would mean I once actually knew what I was doign.

I want to match only the first match out of a series of possibilities. If I'm looking for apple or banana and the set is banana, apple, then return banana.

Code:

if ( $line =~ m/((\tCUT\t)|(\tWD\t)|(\tE\t)|(\-|\+)([0-9][0-9]|[0-9]))/ ) {
$golferscores[$x] = $1;

Data:

- 73 Jason Day +5 +1 WD 76 - - - 76

I'm expecting to get back "WD" in $1 but instead I get "+5."

Any idea where I'm going wrong?


BillKSmith
Veteran

Apr 6, 2012, 9:19 PM

Post #2 of 3 (621 views)
Re: [jddaly] Problems matching first pattern [In reply to] Can't Post

It is much easier to search for one possiblity at a time. I am not exactly sure what you do want, but this should give you the idea.


Code
  

use strict;
use warnings;
my $x = 0;
my @golferscores;
$_ = "- 73 Jason Day +5 +1\tWD\t76 - - - 76";
$golferscores[$x] = /\t(CUT)\t/ ? $1
: /\t(WD)\t/ ? $1
: /\t(E)\t/ ? $1
: /([-+]\d{1,3})/ ? $1
: 'Error'
;
print $golferscores[$x];

Good Luck,
Bill


jddaly
New User

Apr 6, 2012, 9:23 PM

Post #3 of 3 (620 views)
Re: [BillKSmith] Problems matching first pattern [In reply to] Can't Post

Never thought of that, nice. Thanks, appreciate it sir.

 
 


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

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