Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Confused about string matching

Status
Not open for further replies.

columb

IS-IT--Management
Feb 5, 2004
1,231
EU
So, I'm working with data lines which end with a return code and an optional non numeric message code. What I have so far is
Code:
my @bits = split /\s+/;
if ( $bits[-1] =~ /^\d+$/ )
  { $retcode = $bits[-1]; }
else
  { $retcode = $bits[-2]; }
No problem with that, it works but why can't I use
Code:
my @bits = split /\s+/;
$bits[-1] =~ /^\d+$/ and $retcode = $bits[-1] or $retcode = $bits[-2];
or
Code:
my @bits = split /\s+/;
$bits[-1] =~ /^\d+$/ ? $retcode = $bits[-1] : $retcode = $bits[-2];

Both of those always set retcode to $bits[-2]. An explanation would be greatly appreciated.

Thanks.

On the internet no one knows you're a dog

Columb Healy
 
Hi

Maybe I misunderstand you, but your first and second code works for me. The third has bad syntax, should be :
Code:
[navy]$retcode[/navy] [teal]=[/teal] [navy]$bits[/navy][teal][-[/teal][purple]1[/purple][teal]][/teal] [teal]=~[/teal] [green][i]/^\d+$/[/i][/green] [teal]?[/teal] [navy]$bits[/navy][teal][-[/teal][purple]1[/purple][teal]][/teal] [teal]:[/teal] [navy]$bits[/navy][teal][-[/teal][purple]2[/purple][teal]];[/teal]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top