Coder's Guild Mailing List

Perl Regular Expressions

Posted by Kspansel@xxx.xxx on 2000-02-10

Okay..
    I have a string: "tcpmux            1/tcp    TCP Port Service 
Multiplexer" and I am splitting the string into different variables.  Which 
are $keyword, $portnumber, $protocol, and $description.  How would I go about 
doing this?  In a nice, organized, clean way?  Here is what I have (keep in 
mind i've only been doing this for about a week.  hehe):

my $string = "tcpmux            1/tcp    TCP Port Service Multiplexer";

#remove leading and trailing whitespace
string =~ s/^\s*//;
string =~ s/^\s*$//;

#replace anyother whitespace with a single space (to keep it uniform)
string =~ s/\s*/ /g;

#get keyword
# I already know that this is not correct, cuz some of the keywords could 
have #s in #them.  How would I make this right?
$string =~ s/([a-zA-Z]*)\s*//;
$keyword = $1;

#get port number
$string =~ s/(\d*)\s*//;
$portnumber = $1;

#get protocol (UDP or TCP or anymore???)
$string =~ s/([a-zA-Z]*)\s*//;
$protocol = $1;

#get description ( just the rest of the remaining text)
$string =~ s/(.*)//;
$description = $1;

Is there any way I can combine this into one regexp?  And one more question 
what's a good way to compare a variable to an entire array?  I have this:

foreach $word (@words)
{
    if ($word eq $wordiwant)
    {
        print "$word matches.\n");
    }
}

Is there a better and faster way to do this?  Frank do you know?  
Anyway..thanks in advanced.  See you ppl later.

Kory Spansel

ps: and if anyone what's to know you can find the standard ports list in the 
rfc1700 at http://www.ietf.org/rfc/rfc1700.txt
Sponsored by Wrox Press - www.wrox.com. Programmer to Programmer (TM)
To signoff send a mail to listserver@xxxx.xx.xx with 
  "signoff tcg" in the body of your message.