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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable Data format 1

Status
Not open for further replies.

pani765

Programmer
Aug 14, 2003
12
US
Hello,

I have an event variable which can have 3 different format as follow:

1) $mod_item = 465
2) $mod_item = 465_0
3) $mod_item = 465_0_0

how can i determine what type of format the variable data is?

if ($mod_item =~ /_/o) {
..
} elsif ( ?) {
else {...}


I am fairly new to perl so any help would be appreciated. In addition where can i find more reading material on this topic.

Thanks

Roj
 
As is usually the case with Perl, you can do this a few different ways. Here's a couple:
Code:
1) # Pattern match
if ($mod_item =~ /_0_0/) {
} elsif ( $mod_item =~ /_0/) {
} else {
}

2) # Count the number of _0s
$Num++ while ($mod_item =~ /_0/g);
if ($Num == 2) {
    # _0_0
} elsif ($Num == 1) {
    # _0
} else {
    # No _0
}
 
you could check its length if it is always 3 digits followed by either _0 or _0_0

or similarly to your example

if ($mod_item =~ /_0_0/) {
# this will catch it immediately if it has _0_0
} elsif ($mod_item =~ /_0/) {
# this will catch it if it only has _0
} else {
# this will cat it if it if neither of the above are true
}


Kind regards
Duncan
 
And to add in a dash of spice, the one-liner:
Code:
$count = () = $mod_item =~ /(_0)/;

print "The item was of type $count\n";
This is cool becuase it uses an empty list just to force list context, and then evaluates that list in scalar context, giving us the count.

--jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top