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

Hi Perl Monks.... Can any one one help me how to do the following. 1. I Will be receiving one of the below values(mentioned in Examples) +in the string and I need to find whether the string starts with one o +f : MS_ , TS_ , BP_ , Dummy_ and ends with one of _0001, _025f(alpha +numeric) OR _0001_678b, _0001_7d18,_0001_a6d8 (_0001_XXXX ) : XXXX is any decimal number or alphanumeric. 2. Also i need to store the second string(11BT801) i.e after MS_,TS_,B +P_,Dummy_ to some variable. ----------Examples----------- MS_11BT801_AQHT801_0001 TS_11BT801_AUBT801_0001 BP_11BT801_C2DT801_0001 BP_11BT801_DNWT801_025f Dummy_11BT801_CPZT801_3h81 MS_C2DT801_PU7T801_0001_678b TS_C2DT801_RAYT801_0001_7d18 BP_CJBT801_NKRT802_0001_a6d8
sub _print_nodeandip { my @ips; if(! open (FILE_FH, "$File")) { print "Failed to Open File $File for Reading.. Exiting.."; exit; } while (<FILE_FH>) { chomp($_); my @line = split(/,/,$_); my $node=$line[1]; if ($node =~ /[A-Za-z]\_*\_*\_*\[A-Za-z0-9]/) { print "$node\n"; } } my Input Line has something like : 11bt801,MS_11BT801_5A0T801_0001,3,10.197.28.38,11BT801,1,16,1,1,3,10.1 +97.28.37,mtx1.idc

Replies are listed 'Best First'.
Re: String with multiple underscores
by bellaire (Hermit) on Mar 05, 2009 at 22:08 UTC
    Basically you have two ways you can go about this:
    • You can use a regular expression to match the strings you described and capture the portion which you want to store in a variable.
    • You can split your strings into arrays on the underscore separator, and do comparisons against the last elements of those arrays to see whether they are in your list of items to match. If so, store the appropriate (second) element of the array.
    Neither is particulary complicated, but the second might be easier for you if you have no knowledge of regexes.
Re: String with multiple underscores
by jwkrahn (Abbot) on Mar 05, 2009 at 23:26 UTC

    You probably want something like:

    sub _print_nodeandip { my @ips; open my $FILE_FH, '<', $File or die "Failed to Open File $File for +: $!"; while ( <$FILE_FH> ) { my $node = ( split /,/ )[ 1 ]; if ( $node =~ /^(?:MS|TS|BP|Dummy)_([[:alnum:]]+)_[[:alnum:]]+ +_(?:0001(?:_[[:xdigit:]]{4})?|[[:xdigit:]]{4})$/ ) { print "$1\n"; } } }
      Thank you so much, it worked for me.
Re: String with multiple underscores
by Corion (Patriarch) on Mar 05, 2009 at 21:53 UTC

    So, what code have you written already?

    We will gladly help you with your existing code, but this is not a site where we will write a program for you.

Re: String with multiple underscores
by CountZero (Bishop) on Mar 06, 2009 at 06:28 UTC
    TIMTOWTDI:
    use strict; while (<DATA>) { next unless /^(?:MS_|TS_|BP_|Dummy_)/; next unless /(?:_0001|_025f|_0001_[[:alnum:]]{4})$/; print +(split '_')[1],"\n"; } __DATA__ MS_11BT801_AQHT801_0001 TS_11BT801_AUBT801_0001 BP_11BT801_C2DT801_0001 BP_11BT801_DNWT801_025f Dummy_11BT801_CPZT801_3h81 MS_C2DT801_PU7T801_0001_678b TS_C2DT801_RAYT801_0001_7d18 BP_CJBT801_NKRT802_0001_a6d8
    Output:
    11BT801 11BT801 11BT801 11BT801 C2DT801 C2DT801 CJBT801
    If by "ends with one of _0001, _025f(alphanumeric)" you mean that the string could end in any 4 alfanumerics (and not specifically _0001 or _025f) you can simplify the second regex to
    /[[:alnum:]]{4}$/

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James