Deane grumbled: > I've not seen this done, so I don't know if it can be, but... Is there a way to get $1 from a pair of parens in a regexp into a variable in one line? That is... my $foo = 'http://10.20.30.40/gargle'; $foo =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/; my $fee = $1; print "IP = \"$fee\"\n"; > ..is there some way to get the second and third lines into one line?
Well - maybe. But $foo =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/; my $fee = $1; is a Bad Thing, on its own. Did it match? You don't know. If it didn't, there's the oft-overlooked issue w/ perl in that "$1" and the rest of the capture vars retain their old values after a failed match. That is (note the RE change to 3,3 for the first octet so it fails): if ( $do_this =~ /(y)/ ) { my $foo = 'http://10.20.30.40/gargle'; $foo =~ /(\d{3,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/; my $fee = $1; print "IP = \"$fee\"\n"; prints: IP = "y" You always want to test your matches. What you want to do is have the match return a list of capture vars: if ( my ($fee) = $foo =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ ) { print "IP = \"$fee\"\n"; } else { print "NO IP found in \"$foo\"\n"; } # if ( my ($fee) = $foo =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ ) You need the parens around "$fee" to make it into list context, the match returns the list of capture vars. If you did: if ( my $fee = $foo =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ ) { that is, scalar context, you get back the true/false (Number of matches, actually) in $fee. You can get multiple returned values here: if ( my ($fee, $fie, $foe, $fum ) = $foo =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/ ) { printf('IP = "%d.%d.%d.%d"%s', $fee, $fie, $foe, $fum, "\n") ; using printf is a little more readable esp if the vars can sort line up under their template markers. The "\n" on the end was one way to avoid \" you could do printf("IP = '%d.%d.%d.%d'%s", $fee, $fie, $foe, $fum) ; if: IP = '10.20.30.40' is acceptable. Kevin wondered: > but I thought the general purpose of this listserv was to > discuss issues related to the ActiveState perl, not coding issue. Maybe, but its been used as as such for so long, one'd have to go w/ a "no, its for active perl users to ask perl questions". The unix-user and, to a lesser extent, the win32-user perl lists have pretty much gone the same route. I don't imagine there's enough 'just ActiveState' perl issues to keep a list going ;-> that's a good thing. a Andy Bach Systems Mangler Internet: [EMAIL PROTECTED] VOICE: (608) 261-5738 FAX 264-5932 "CM/ECF is a complex unfinished suit. Pull on a loose cuff thread and your pants fall down." MEC _______________________________________________ ActivePerl mailing list ActivePerl@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs