Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to capture the last number in a string of numbers delimited by a period like so using the following code:
1.3.6.1.4.1.674.10893.2.1.200.0.2 1.3.6.1.4.326.10.2.1.2.0.63 my $map = $traps{$trap}{oid} =~ /\.\d+$/;
Can anyone tell me what I'm doing wrong?

Replies are listed 'Best First'.
Re: RegEx problems
by sauoq (Abbot) on Jul 01, 2003 at 17:22 UTC
    Can anyone tell me what I'm doing wrong?

    You aren't capturing or using list context. You are assigning a boolean to $map which answers "does this match?" Try:

    my ($map) = $traps{$trap}{oid} =~ /(\d+)$/;
    -sauoq
    "My two cents aren't worth a dime.";
    
Re: RegEx problems
by dws (Chancellor) on Jul 01, 2003 at 17:23 UTC
    Try changing
    my $map = $traps{$trap}{oid} =~ /\.\d+$/;
    to
    my ($map) = $traps{$trap}{oid} =~ /\.(\d+)$/;
    The final digit of an SNMP OID has no particular significance, unless it's an index into a table. (But you probably knew that.)

Re: RegEx problems
by dreadpiratepeter (Priest) on Jul 01, 2003 at 17:25 UTC
    you need to do two things. 1. create a submatch for the data you want and 2. evaluate the regexp in list context to get the result. Try:
    my ($map) = $traps{$trap}{oid} =~ /\.(\d+)$/;


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
Re: RegEx problems
by ant9000 (Monk) on Jul 01, 2003 at 17:42 UTC
    I'd personally prefer the syntax
    my $map = $1 if($traps{$trap}{oid} =~ /\.(\d+)$/);
    simply because I find it more readable... just a matter of taste, really.
Re: RegEx problems
by Not_a_Number (Prior) on Jul 01, 2003 at 17:54 UTC

    Or if you don't want to use a regex:

    my $map = substr $traps{$trap}{oid}, ( rindex $traps{$trap}{oid}, '.' ) + 1;
Re: RegEx problems
by Zed_Lopez (Chaplain) on Jul 01, 2003 at 19:13 UTC

    or there's

    my $map = (split /\./, $traps{$trap}{oid})[-1];