Hello
Here is my script, I am missing something. All I am trying to do is to skip
LA and BURLINGTON lines, because they are missing 'MAIN'.
Thanks.
I.S
my %state;
%state = (
CHICAGO => ["MAIN", "BROADWAY", "OAK"],
LA => ["DELTA", "GAMMA"],
BOSTON => ["FIRST", "MAIN"],
BURLINGTON => ["SECOND", "ONE"],
SEATTLE => ["GREAT","MAIN"],
);
SWITCH:{ while ($line = <DATA>){
#next if /^\s*$/;
@line = split /\s*,\s*/, $line;
$city = $line[3];
#print @line;
#print "CITY : $city\n";
foreach $street (@{ $state{$city} } ){
print $street, "\n";
if ( $street ne "MAIN"){
next SWITCH;
}
}
}
}
__DATA__
STATE ,IL, SDW,CHICAGO
STATE,CA, SFD,LA
STATE, MA, FDR,BOSTON
STATE, CT,FGD,BURLINGTON
STATE, WA, SDF,SEATTLE
__END__
Michael Fowler <[EMAIL PROTECTED]> wrote:
>
> On Thu, Jun 28, 2001 at 02:34:31PM -0700, Paul wrote:
> > > $city = $record[3] ;
> > > for ($i = 0; $i <= $#{ $state{$city}; $i ++ ) {
> >
> > Never use the
> > for( ; ; ) { }
> > construct in Perl without a significant and compelling reason.
> > foreach is virtually always better for lots of reasons. Try:
> >
> > for my $i ( 0 .. $#{ $state{$city} ) {
>
> Well, you make a good point that one should use foreach, but then you use it
> in almost identical way as the for loop. If you were to use it this way,
> you might as well use a for loop. The most readable and idiomatic way to do
> this is:
>
> foreach my $street (@{ $state{$city} }) {
> ... if $street ne "MAIN";
> }
>
> You should try to avoid the foreach (0 .. $#array) form; that list has to go
> into memory somewhere, and it's just a more verbose way of saying foreach
> (@array).
>
> Otherwise, that's some good advice.
>
>
> Michael
> --
> Administrator www.shoebox.net
> Programmer, System Administrator www.gallanttech.com
> --
>
__________________________________________________________________
Get your own FREE, personal Netscape Webmail account today at
http://webmail.netscape.com/