SPUG:for help

Chris Wilkes cwilkes-spug at ladro.com
Thu Feb 6 18:28:50 CST 2003


On Wed, Jan 29, 2003 at 10:19:57PM -0600, Liu, Zhu wrote:
>
> Hi, everybody, I experienced a very stupid simple problem for three
> hours. I just want to extract three substrings from one string. In the
> beginning of the string there are two spaces like
>    207 MFSNLASDEQDSVRLLAV---EAC--VNIAQLLPQEDLEALVMPTLRQAAEDKSWRVRYM   261
> 
> My part code is
> if( $line =~ /^\s+\d+.*\d$/)
> {
>       ($part, $part2, $part3) = /\s+(\d+)\s(.*)\s+(\d+)/;
                               ^^^^
> }

Since you're already doing through the trouble of checking the line to
match a regex you should try to do it all at once.

> it always tells me "Use of uninitialized value in pattern match (m//)",
> so if I use the following codes

The reason you're getting that is highlighted above.  You've switched
from doing a regex on "$line" and now you're implicitly doing it on "$_"
which is (probably) undefined.  You have to do something like this:
  my ($part, $part2, $part3) = ($line =~ /\s+(\d+)\s(.*)\s+(\d+)/);
don't forget the "my" as you're using strict.

Taking a cue from The Damian I would re-do your regex using /x so that
you can self comment your code.  Here's an example.  You could change it
to be like:
  if (my @parts = (regular expression) ) { }
if you wanted to as well.


#!/usr/bin/perl -w

use strict;
my $word = qr /[A-Z]+/;

while (<DATA>) {
   chomp;
   if (         # matching string like ### ABCD---EFG--HIJKL ###
       /^\s+    # skip leading spaces
       (\d+)    # the batch number
       \s+
       ($word)  # your Martian name (first part of ABCD---EFG--HIJKL)
       ---
       ($word)  # your first saying (second part of match)
       --
       ($word)  # the MD5 hash of the email (third part of match)
       \s+
       (\d+)    # cost in cents of taco bell meal
       /x) {
     my @parts = ($1, $2, $3, $4, $5);
     print "Good: '" . (join ",", @parts) . "'\n";
   }
}

__DATA__
  207 MFSNLASDEQDSVRLLAV---EAC--VNIAQLLPQEDLEALVMPTLRQAAEDKSWRVRYM   261
  307 ABCDEFGHIJKLMNOPQR---EAC--STRUVWXYZABCDEFGHIJKLMNOPQRSTRUVWX   361
this line is bad
  310    soisthigsone--eac-adsljasldsalj 310
  317 XXXXXXXXXXXXXXXXXX---EAC--YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY   371



More information about the spug-list mailing list