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

Hi all:

I am trying to match on physical address (below) and get the last five numbers 75219.


Physical Address. . . . . . . . . : 00-B0-D0-87-52-19
DHCP Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 192.45.10.166

My snippet:

open(I,'ipconfig /all |'); while(<I>){ next unless /Physical Address/; /:/; chomp; tr/-//d; $new_string = substr($_, -6); print " new string is : hhh" . $new_string . "kk" . "\n"; last; }

output:

kkew string is : hhh75219

Should'nt output be hhh75219kk.

Can somebody tell me what is wrong here.

--thanx kirk

Edited 2002-08-15 by Ovid

Replies are listed 'Best First'.
Re: I need help with pattern matching
by DamnDirtyApe (Curate) on Aug 15, 2002 at 21:54 UTC

    The cause of your output is beyond me; however, I'd suggest a different approach to aquiring the numbers:

    #! /usr/bin/perl use strict ; use warnings ; # $|++ ; while ( <DATA> ) { /^Phys.*(\w)-(\w\w)-(\w\w)$/ ; my $match = 'hhh' . $1 . $2 . $3 . 'kk' ; print "The new string is: $match\n" ; last ; } __DATA__ Physical Address. . . . . . . . . : 00-B0-D0-87-52-19 DHCP Enabled. . . . . . . . . . . : No IP Address. . . . . . . . . . . . : 192.45.10.166

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
      Thanks to all: I 've just solved my own problem. Forgot to put length in substr() . ---kirk
Re: I need help with pattern matching
by Abigail-II (Bishop) on Aug 16, 2002 at 08:30 UTC
    I'd do it this way:
    use strict; use warnings 'all'; use Regexp::Common; while (<DATA>) { if (/$RE{net}{MAC}{hex}{-sep => '-'}{-keep}/i) { print "New string is: ", substr ("$5$6$7" => 1), "kk\n"; } } __DATA__ Physical Address. . . . . . . . . : 00-B0-D0-87-52-19 DHCP Enabled. . . . . . . . . . . : No IP Address. . . . . . . . . . . . : 192.45.10.166
    Abigail
Re: I need help with pattern matching
by converter (Priest) on Aug 16, 2002 at 07:10 UTC

    The cause of the "odd" output is the carriage return character you're leaving on the end of $new_string. I assume you're running this code on UNIX so that $/ is set to "\n" and chomp() removes only the newline, leaving the carriage return, which when output to the display device repositions the point at the beginning of the line.

    You could get rid of the carriage return by assigning the proper record separator string to a dynamic copy of $/:

    { # localized global variable # this copy of $/ is visible only within this block # and to subroutines called from within the block. local $/ = "\r\n"; while (<I>) { next unless /Physical Address/; /:/; # why is this here? chomp; # chomp now removes "\r" in addition to "\n" ... }
Re: I need help with pattern matching
by Anonymous Monk on Aug 15, 2002 at 22:14 UTC
    tr/0-9/cd $new_string = substr($_, -5);