Re: Foreach loop and hash of arrays

2012-02-07 Thread Shawn H Corey
On 12-02-07 06:26 AM, Rob Dixon wrote: in fact, if the objective is to reduce the code to something as brief as possible then this will do the trick my $match = (grep $_ eq $customers_zip, @{$states{$customers_state}}) ? 'yes' : 'no'; You can use first from List::Util for more efficient cod

Re: Foreach loop and hash of arrays

2012-02-07 Thread Rob Dixon
On 07/02/2012 01:39, sono...@fannullone.us wrote: On Feb 6, 2012, at 1:42 PM, Steve Bertrand wrote: This may be easier. It uses the hash elements directly as an array, then uses grep to see if the zip code is within the specific state. It returns true if the state owns that zip code, and false

Re: Foreach loop and hash of arrays

2012-02-06 Thread sono-io
On Feb 6, 2012, at 1:42 PM, Steve Bertrand wrote: > This may be easier. It uses the hash elements directly as an array, then uses > grep to see if the zip code is within the specific state. It returns true if > the state owns that zip code, and false if it doesn't. > > if ( grep( /^$customers_z

Re: Foreach loop and hash of arrays

2012-02-06 Thread Robert Wohlfarth
On Mon, Feb 6, 2012 at 3:14 PM, wrote: >So I'm creating a hash of arrays that contains a list of Zip Codes > for the United States. I've also written a foreach loop to access this > hash but I'd like to see if it could be written better. For example, do I > really need three foreach loo

Re: Foreach loop and hash of arrays

2012-02-06 Thread Uri Guttman
On 02/06/2012 04:58 PM, Parag Kalra wrote: On Mon, Feb 6, 2012 at 1:14 PM, wrote: For example, do I really need three foreach loops? You can get rid of third ForLoop for sure. you don't actually lose the third loop. grep is an implied loop. STATE: foreach my $state (keys %states

Re: Foreach loop and hash of arrays

2012-02-06 Thread Rob Dixon
On 06/02/2012 21:14, sono...@fannullone.us wrote: use strict; use warnings; my %states = ( AL => [ '350','351', ], AK => [ '995','996', ], AZ => [ '850','851', ], AR => [ '716','717', ], ); my $customers_state = 'AZ'; my $customers_zip = '850'; my $match = 'n

Re: Foreach loop and hash of arrays

2012-02-06 Thread Parag Kalra
On Mon, Feb 6, 2012 at 1:14 PM, wrote: >For example, do I really need three foreach loops? > > > You can get rid of third ForLoop for sure. use strict; use warnings; my %states = ( AL => [ '350','351', ], AK => [ '995','996', ], AZ => [ '850','851', ], AR => [ '

Re: Foreach loop and hash of arrays

2012-02-06 Thread Steve Bertrand
On 2012.02.06 16:14, sono...@fannullone.us wrote: I have a web form where people enter their address info and I want to make sure that the first three digits of their Zip Code correspond to their State. So I'm creating a hash of arrays that contains a list of Zip Codes for the

Foreach loop and hash of arrays

2012-02-06 Thread sono-io
I have a web form where people enter their address info and I want to make sure that the first three digits of their Zip Code correspond to their State. So I'm creating a hash of arrays that contains a list of Zip Codes for the United States. I've also written a foreach loop to