develooper Front page | perl.beginners | Postings from March 2002

Re: Newbie question

Thread Previous
From:
suraj rajendran
Date:
March 3, 2002 19:45
Subject:
Re: Newbie question
Message ID:
20020304034537.58593.qmail@web21202.mail.yahoo.com
Thank you very much.
this list is very helpful. Within minutes I got 3
replies.
regs

--- Eric Beaudoin <beaudoer@videotron.ca> wrote:
> At 21:15 2002.03.03, suraj rajendran wrote:
> >Here is a very basic question:
> >I am trying to print only the zip code of
> massachusets
> >Even though this works, i am pretty sure there is a
> >better way doing this. Any ideas?
> >
> >#!/usr/bin/perl
> >while (<DATA>) {
> >($name, $phone, $address, $dob, $salary) =
> split(":",
> >$_);
> >($add1, $city, $statezip) = split(",",$address);
> >($state, $zip) = split(' ',$statezip);
> >print "$zip\n" if $statezip =~/MA/;
> > 
> >}
> 
> I don't think you need all those variable. split
> return a list and you can always access a list
> element by using it's position in the list. So
> 
> use strict;
> use warnings;
> 
> while(<DATA>) {
>         print "$1\n" if
> (split(/,/,(split(/:/,$_))[2]))[2] =~ /MA\s(\d{5})/;
> }
> 
> __DATA__
> ...
> 
> (split(/:/,$_))[2] returns the third element of you
> split
> (split(/,/,(split(/:/,$_))[2]))[2] returns third
> element of a the third element
> The regex does
>         /MA             # Match MA for massachusets
>         \s              # Match one white space
>          (\d{5})        # Match 5 digits and put the
> result in $1 if matched
>         /x              # x would need to be used in
> order to put comments
>                         # in the regex
> 
> But then, your solution was more readable and you
> might want to do something with the other pieces of
> the address.
> 
> Hope this helps
> 
> 
>
----------------------------------------------------------
> �ric Beaudoin              
> <mailto:beaudoer@videotron.ca>
> 


__________________________________________________
Do You Yahoo!?
Yahoo! Sports - sign up for Fantasy Baseball
http://sports.yahoo.com

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About