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

Re: foreach loop

2011-03-25 Thread Jim Gibson
On 3/25/11 Fri Mar 25, 2011 9:11 AM, "Chris Stinemetz" scribbled: > Thanks Rob. That seems to have done the trick. I understand this is a > for loop, but do you mind breaking it down line by line so I fully > understand what it is doing? My name isn't Rob, but I can give you a line-by-line des

Re: foreach loop

2011-03-25 Thread Chris Stinemetz
Thanks Rob. That seems to have done the trick. I understand this is a for loop, but do you mind breaking it down line by line so I fully understand what it is doing? Thank you, Chris > >  for my $i (44..47) { >    my $rlp = $data[$i]; >    $sum{$cell}{$sect}{$carr}{$dist} += $rlp if $rlp; >  } >

Re: foreach loop

2011-03-24 Thread Rob Dixon
On 25/03/2011 02:36, Chris Stinemetz wrote: > Jim, > > I am getting really close to finishing up this program. perl is lot's > of fun and I appreciate all your help! > > The output is giving me the data I am looking for except for the > following issues: > > How would I add the correct code to m

Re: foreach loop

2011-03-24 Thread Chris Stinemetz
Jim, I am getting really close to finishing up this program. perl is lot's of fun and I appreciate all your help! The output is giving me the data I am looking for except for the following issues: How would I add the correct code to make sure the array has a numeric value before the loop iterati

Re: foreach loop

2011-03-24 Thread John W. Krahn
Jim Gibson wrote: On 3/24/11 Thu Mar 24, 2011 9:04 AM, "Chris Stinemetz" scribbled: $sum{$cell}{$sect}{$carr} += $rlp1 += $rlp2 += $rlp3 += $rlp4 || 0 ; } I see you are changing your program requirements. You are now accumulating multiple rlp values instead of one. This line has two pro

Re: foreach loop

2011-03-24 Thread Jim Gibson
On 3/24/11 Thu Mar 24, 2011 9:04 AM, "Chris Stinemetz" scribbled: > I would like $dist be part of the print statement, but I am not sure how to > code it correctly. The value of $dist will vary for each row. If you want to print out the correct value of $dist that corresponds to the value of t

RE: foreach loop

2011-03-24 Thread Chris Stinemetz
Thanks again Jim! I have one more question..lol I appreciate all your help! I would like $dist be part of the print statement, but I am not sure how to code it correctly. I am getting the following error: Global symbol "@dist" requires explicit package name at ./jim.pl line 38. Execution of

Re: foreach loop

2011-03-23 Thread Jim Gibson
On 3/23/11 Wed Mar 23, 2011 12:49 PM, "Chris Stinemetz" scribbled: > Jim, > > I have another question. > > How do I sort the results so it is from smallest to largest starting with > $cell,$sect,$carr? It is difficult to sort a multi-level, nested hash. I would transfer the values to an arra

RE: foreach loop

2011-03-23 Thread Chris Stinemetz
Jim, I have another question. How do I sort the results so it is from smallest to largest starting with $cell,$sect,$carr? Thanks again for all you help. I am gaining a much better understanding. This is what I got: #!/usr/bin/perl use warnings; use strict; #my $filepath = 'C:/temp/PCMD';

RE: foreach loop

2011-03-23 Thread Chris Stinemetz
That worked! Thanks Jim. Chris -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/

Re: foreach loop

2011-03-23 Thread Jim Gibson
On 3/23/11 Wed Mar 23, 2011 10:02 AM, "Chris Stinemetz" scribbled: In addition to the missing semicolon, the declaration of %sum must appear before it is used, i.e. before the while() loop. The line adding values of $rlptxat1 to the sum must appear inside the while loop, not after it. Run this

Re: foreach loop

2011-03-23 Thread Chas. Owens
On Mon, Mar 21, 2011 at 01:46, Mike McClain wrote: snip >> > my @report = map >> > "$_->{cell}\t$_->{sect}\t$_->{carr}\t$_->{chan}\t$_->{dist}\n" , @sorted; >> > print @report ; >> >> This map will consume a lot of memory, better do it using a foreach loop. > > In what way will the use of map here

Re: foreach loop

2011-03-23 Thread Jim Gibson
On 3/23/11 Wed Mar 23, 2011 10:02 AM, "Chris Stinemetz" scribbled: > Thanks Jim, > > I am still unable to sum up the field $rlptxat. > > The error I am getting is below: > > Scalar found where operator expected at ./jim.pl line 52, near "$sum" > (Missing semicolon on previous line?)

RE: foreach loop

2011-03-23 Thread Chris Stinemetz
Thanks Jim, I am still unable to sum up the field $rlptxat. The error I am getting is below: Scalar found where operator expected at ./jim.pl line 52, near "$sum" (Missing semicolon on previous line?) "my" variable %sum masks earlier declaration in same scope at ./jim.pl line 54. "my" va

RE: foreach loop

2011-03-22 Thread Jim Gibson
At 9:58 PM -0600 3/22/11, Chris Stinemetz wrote: Jim, You hit it right on. This is exactly what I am trying to do. OK. With this information and that from previous posts, your requirements may be summaryized as follows: You have a file with one record per line, each line consisting of a n

RE: foreach loop

2011-03-22 Thread Chris Stinemetz
Jim, You hit it right on. This is exactly what I am trying to do. > OK. With this information and that from previous posts, your requirements may > be summaryized as follows: > You have a file with one record per line, each line consisting of a number of > fields. Within each record may be fou

Re: foreach loop

2011-03-22 Thread Jim Gibson
On 3/22/11 Tue Mar 22, 2011 2:24 PM, "Chris Stinemetz" scribbled: >>> No, it doesn't. What is a "rlptxat" element? Where do they come from. > > "rlptxat" is just an element indexed at 44 that has a value, in which I would > like to sum up, when it has the same elements "cell" "sect" and "carr"

RE: foreach loop

2011-03-22 Thread Chris Stinemetz
>>No, it doesn't. What is a "rlptxat" element? Where do they come from. "rlptxat" is just an element indexed at 44 that has a value, in which I would like to sum up, when it has the same elements "cell" "sect" and "carr" in the record. I hope this helps Thank you, Chris At 8:03 PM -0600 3/2

Re: foreach loop

2011-03-21 Thread Mike McClain
On Sun, Mar 20, 2011 at 10:06:25AM +0200, Shlomi Fish wrote: > On Sunday 20 Mar 2011 05:43:38 Chris Stinemetz wrote: > > I am trying to code a foreach loop, that will add all occurrences of the > > element rlptxat that have the same elements cell, sect and chan. > > $record{dist}/6.6/8/2*10/10 :

Re: foreach loop

2011-03-20 Thread terry
于 2011-3-21 13:33, Jim Gibson 写道: Iterate over the result: for my $cell ( sort keys %sum ) { for my $sect ( sort keys %{$sum{$cell}} ) { for my $chan ( sort keys %{$sum{$cell}{$sect}} ) { print "Value of sum($cell,$sect,$chan) is $sum{$cell}{$sect}{$chan}\n"; }

RE: foreach loop

2011-03-20 Thread Jim Gibson
At 8:03 PM -0600 3/20/11, Chris Stinemetz wrote: Jim, Thanks for your feedback. I am actually trying to sum up all rlptxat elements that have the same cell, sect, and chan elements in the array. I hope this clarifies. No, it doesn't. What is a "rlptxat" element? Where do they come from. You

RE: foreach loop

2011-03-20 Thread Chris Stinemetz
:58 PM To: beginners@perl.org Subject: RE: foreach loop At 8:31 AM -0600 3/20/11, Chris Stinemetz wrote: >So back to the question. How can I nest this foreach loop correctly >to add all occurrences of the element rlptxat that have the same >elements cell, sect and chan in the array? &

RE: foreach loop

2011-03-20 Thread Jim Gibson
At 8:31 AM -0600 3/20/11, Chris Stinemetz wrote: So back to the question. How can I nest this foreach loop correctly to add all occurrences of the element rlptxat that have the same elements cell, sect and chan in the array? $record{rlptxat} = ( length($record{rlptxat})> 1 ) ? $record{rlptxat

RE: foreach loop

2011-03-20 Thread Chris Stinemetz
So back to the question. How can I nest this foreach loop correctly to add all occurrences of the element rlptxat that have the same elements cell, sect and chan in the array? $record{rlptxat} = ( length($record{rlptxat})> 1 ) ? $record{rlptxat}{cell, sect, chan, dist}++ : '' ; Thank you very

Re: foreach loop

2011-03-20 Thread Uri Guttman
> "SF" == Shlomi Fish writes: >> my @records; >> >> my @lines = read_file( $filepath ); >> >> chomp @lines; >> SF> You should read the file line-by-line, using: SF> < $fh > SF> Not slurp it. there is no reason to read it line by line. it is small enough to slurp. sl

Re: foreach loop

2011-03-20 Thread Shlomi Fish
Hi Chris, On Sunday 20 Mar 2011 05:43:38 Chris Stinemetz wrote: > I am trying to code a foreach loop, that will add all occurrences of the > element rlptxat that have the same elements cell, sect and chan. > > My failed attempt can be located in between the ### lines. > If I understand your cod

Re: foreach loop

2009-11-06 Thread tom smith
On Fri, Nov 6, 2009 at 6:48 PM, John W. Krahn wrote: > tom smith wrote: > >> On Mon, Nov 2, 2009 at 5:10 AM, John W. Krahn wrote: >> >> Philip Potter wrote: >>> >>> 2009/11/2 Thomas Bätzler : while( my $line = <$file> ){ > > } > > Won't this loop terminate early if

Re: foreach loop

2009-11-02 Thread Peter Scott
On Mon, 02 Nov 2009 13:06:27 +0530, Anant Gupta wrote: > Hello, > > In the foreach loop, without going to the beginning of the loop, i want > to get the next iteration of data. How do i get it. eg > > use strict; > open(FILE,"; > foreach my $line(@lines) > { > if($lin =~ m/something/) >

Re: foreach loop

2009-11-02 Thread John W. Krahn
Philip Potter wrote: 2009/11/2 Thomas Bätzler : while( my $line = <$file> ){ } Won't this loop terminate early if there is a blank line or a line containing only '0'? If I do a readline loop I always do: while (defined ($line = <$file>)) Is there something magical happening here I don't know

Re: foreach loop

2009-11-02 Thread Philip Potter
2009/11/2 Thomas Bätzler : > while( my $line = <$file> ){ > > } Won't this loop terminate early if there is a blank line or a line containing only '0'? If I do a readline loop I always do: while (defined ($line = <$file>)) Is there something magical happening here I don't know about? I know abou

Re: foreach loop

2009-11-02 Thread Anant Gupta
Ohh, Thanks for the help On Mon, Nov 2, 2009 at 1:40 PM, John W. Krahn wrote: > Anant Gupta wrote: > >> Hello, >> > > Hello, > > In the foreach loop, without going to the beginning of the loop, i want to >> get the next iteration of data. How do i get it. >> eg >> >> use strict; >> open(FILE,">

Re: foreach loop

2009-11-02 Thread John W. Krahn
Anant Gupta wrote: Hello, Hello, In the foreach loop, without going to the beginning of the loop, i want to get the next iteration of data. How do i get it. eg use strict; open(FILE,"; foreach my $line(@lines) { if($lin =~ m/something/) { #some code # get next da

Re: foreach loop

2009-11-02 Thread Erez Schatz
2009/11/2 Anant Gupta : > Hello, > > In the foreach loop, without going to the beginning of the loop, i want to > get the next iteration of data. How do i get it. > eg You can always use a for loop and refer to the next item: for (my $i= 0; $i< @lines; $i++) { if ($lines[$i + 1] == ... > use str

Re: foreach loop

2006-07-24 Thread Dr.Ruud
"Sayed, Irfan (Irfan)" schreef: > Following is the code which I am executing. Missing: use warnings ; use strict ; > > my $fname3 = "/tmp/vob_repl_list_test1"; > open(FILE2, $fname3); Missing: error checking. open my $fh3, '<', $fname3 or die "open $fname3, stopped $!" ; > my @v

RE: foreach loop

2006-07-24 Thread Sayed, Irfan \(Irfan\)
chreplica -host puvob02 $a`; I know that array @vob_repl_list is not empty. plz help regards irfan. -Original Message- From: Stephen Kratzer [mailto:[EMAIL PROTECTED] Sent: Friday, July 21, 2006 6:46 PM To: beginners@perl.org Subject: Re: foreach loop On Friday 21 July 2006 08:59, Sa

Re: foreach loop

2006-07-21 Thread Stephen Kratzer
On Friday 21 July 2006 08:59, Sayed, Irfan (Irfan) wrote: > Hi, > > I am executing following code. > > foreach (@vob_repl_list) > > { > > print $_; > > `$MT chreplica -host puvob02 $_`; > > } > > but I am not getting the value of $_ in the command `$MT chreplica -host > puvob02 $_`; if I print the

Re: foreach loop current index

2005-05-21 Thread Peter Rabbitson
> The reason this has not been built into the language is that there would > be an overhead associated with each loop to maintain this variable. The > Perl5 team felt that this was not acceptable given that most loops do > not need this information and, and you have noted, it's trivial to do > the

Re: foreach loop current index

2005-05-21 Thread JupiterHost.Net
: Neither was I, but I suspect the reason is that if you need a : counter, you should not be using "foreach". Instead, use "for": : for(my $counter = 0; $counter < @array; ++$counter) { : do_something_with($array[$counter]); : } Why is that form favored over this typical foreach form? fore

RE: foreach loop current index

2005-05-21 Thread Charles K. Clarkson
Offer Kaye wrote: : : Neither was I, but I suspect the reason is that if you need a : counter, you should not be using "foreach". Instead, use "for": : for(my $counter = 0; $counter < @array; ++$counter) { : do_something_with($array[$counter]); : } Why is that for

Re: foreach loop current index

2005-05-21 Thread Offer Kaye
On 5/21/05, Peter Rabbitson wrote: > Hello, > > When perl executes a foreach loop, it creates some kind of array of all the > iterators I'm assuming you mean that this code: foreach my $num (1..1_000_000) { do_something($num); } creates a one-million number array? No, it doesn't, at least not

Re: foreach loop current index

2005-05-21 Thread Paul Johnson
On Fri, May 20, 2005 at 09:21:04PM -0500, Peter Rabbitson wrote: > When perl executes a foreach loop, it creates some kind of array of all the > iterators and then goes over them one by one. Is the current index pointer > accessible from inside the loop? I was unable to find anything related in

Re: foreach loop current index

2005-05-21 Thread John Doe
Am Samstag, 21. Mai 2005 04.21 schrieb Peter Rabbitson: > Hello, > > When perl executes a foreach loop, it creates some kind of array of all the > iterators and then goes over them one by one. Is the current index pointer > accessible from inside the loop? I was unable to find anything related in >

Re: foreach loop current index

2005-05-20 Thread Peter Rabbitson
On Sat, May 21, 2005 at 12:11:42AM -0400, Chris Devers wrote: > On Fri, 20 May 2005, Peter Rabbitson wrote: > > > my @lsit = qw(a b c d); > > my %indexes; > > > > my $current_index = 0; > > > > foreach my $element (@list) { > > $indexes{$element} = $current_index; > > ++ $current_index; >

Re: foreach loop current index

2005-05-20 Thread Chris Devers
On Fri, 20 May 2005, Peter Rabbitson wrote: > my @lsit = qw(a b c d); > my %indexes; > > my $current_index = 0; > > foreach my $element (@list) { > $indexes{$element} = $current_index; > ++ $current_index; > } Ah. Maybe this? for (0 .. $#list) { $indexes{$list[$_]} = $_;

Re: foreach loop current index

2005-05-20 Thread Peter Rabbitson
> When perl executes a foreach loop, it creates some kind of array of all the > iterators and then goes over them one by one. Is the current index pointer > accessible from inside the loop? I was unable to find anything related in > perlvar. I think I should clarify myself a little. Here is an

Re: foreach loop current index

2005-05-20 Thread Chris Devers
On Fri, 20 May 2005, Peter Rabbitson wrote: > >foreach $item ( @list ) { > > sub( $item ); > >} > > > > Are you sure this is not a typo? Sorry, I didn't mean that literally, I was just trying to stub in an indicator for "do something useful with $item". In hindsight, foreach $i

Re: foreach loop current index

2005-05-20 Thread Peter Rabbitson
>foreach $item ( @list ) { > sub( $item ); >} > Are you sure this is not a typo? I've never seen a sub statement taking a list argument... Besides this is what I get if I run it anyway: [EMAIL PROTECTED]:~$ cat 123 @list = qw(a bc c); foreach $item ( @list ) { sub( $item );

Re: foreach loop current index

2005-05-20 Thread Chris Devers
On Fri, 20 May 2005, Peter Rabbitson wrote: > When perl executes a foreach loop, it creates some kind of array > of all the iterators and then goes over them one by one. Is the > current index pointer accessible from inside the loop? I was > unable to find anything related in perlvar. I think you

Re: foreach loop :(

2002-03-17 Thread bob ackerman
works for me, so nothing wrong with your perl code. maybe you could show us the url you are using to reach this code. On Saturday, March 16, 2002, at 10:34 PM, Mariusz wrote: > html tags are not the problem. I included another print statement before > the loop and that outputs "text 1 here" to

Re: foreach loop :(

2002-03-17 Thread Mark Maunder
You have two dollar signs before the 'name' variable in the loop. That probably works provided the value of the variable is not a number, but it may be causing the strangeness you're experiencing. Always 'use strict' in your scripts. You should be storing your params in a hash and doing somethi

Re: foreach loop

2002-03-16 Thread bob ackerman
it won't print to web page. no tags. yes? On Saturday, March 16, 2002, at 01:26 PM, Mariusz wrote: > I know this foreach loop works (I included print statement within it and > it printed out results perfectly). However, for some reason the script > doesn't continue after the loop. Am I missi

Re: foreach loop

2002-03-16 Thread Ahmed Moustafa
Mariusz,your script works fine! Mariusz wrote: > I know this foreach loop works (I included print statement within it and it printed >out results perfectly). However, for some reason the script doesn't continue after >the loop. Am I missing something really obvious?(the print "text here" just

Re: foreach loop problems

2002-02-13 Thread dan radom
I've set SSH_AUTH_SOCK and SSH_AGENT_PID environment variables so the script can run from cron, but there is no password prompt with ssh-agent and hostkey authentication. an * Johnathan Kupferer ([EMAIL PROTECTED]) wrote: > I was actually surprised to see that ssh worked like this! I don't >

Re: foreach loop problems

2002-02-13 Thread James Taylor
Ha, Ok well that was interesting. I ran this script on a different machine and it works fine... Weird. So, nevermind :) On Wednesday 13 February 2002 03:35 pm, you wrote: > I'm curious how you got this script to work, I re-wrote his with something > like this: > > my @hosts = ("1.com", "2.com

Re: foreach loop problems

2002-02-13 Thread James Taylor
I'm curious how you got this script to work, I re-wrote his with something like this: my @hosts = ("1.com", "2.com", "3.com", "4.com"); foreach my $key (@hosts) { system("ssh -l @ARGV[0] $key"); } and it will SSH to 1.com ONLY. After the session with 1.com is ended, it doesn't continue on

Re: foreach loop problems

2002-02-13 Thread Johnathan Kupferer
I was actually surprised to see that ssh worked like this! I don't think you can always get away with it, but I just tested it out and was able to call up ssh for 4 different machines using a script similar to that one. I don't think all programs are going to be that nice. Can anyone explain

Re: foreach loop problems

2002-02-13 Thread Michael Kelly
On 2/13/02 2:44 PM, dan radom <[EMAIL PROTECTED]> wrote: > Hi, > > > I've got a problem with the following code... > > my @hosts=qw( lunar solar venus mars saturn pluto ); > > foreach (\@hosts) { > system("/usr/bin/ssh @hosts $ARGV[0]"); > } > > > > What I'm wanting to do is call foo.p

Re: foreach loop problems

2002-02-13 Thread James Taylor
I don't believe this is what he's asking - What the problem is in this code is that after the first instance of SSH runs, and then exits, it will not continue on to the next key in the array. I can't figure out why it won't do it, I don't generally write programs using system calls :) On Wed

Re: foreach loop problems

2002-02-13 Thread Johnathan Kupferer
# Create array @hosts... So far so good. my @hosts=qw( lunar solar venus mars saturn pluto ); # \@hosts creates a reference to @hosts. You just want @hosts. # # The way you've written it, the elements in @hosts will be # assigned to the special variable $_. This is all right, but # tends to ge

Re: foreach() loop creates $aString-s that I modify , AND they modifythe Array Contents !?

2001-08-22 Thread Jeff 'japhy/Marillion' Pinyan
On Aug 21, [EMAIL PROTECTED] said: >I change the value of $aString but ... it turns out that I am **also** >altering the values of the Strings held in the Array. A for-loop aliases a variable to each element of the list you're looping over. That's why you can't say: for (1, 2, 3, 4) { $_

RE: foreach() loop creates $aString-s that I modify , AND they modify the Array Contents !?

2001-08-22 Thread Bob Showalter
> -Original Message- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, August 21, 2001 1:30 PM > To: [EMAIL PROTECTED] > Subject: foreach() loop creates $aString-s that I modify , AND they > modify the Array Contents !? > > > Hi Folks, > > In the subroutine below, >