Re: Filehandle within foreach loop

2017-07-16 Thread David Mertens
Yes, and I think that gives us *two* reasons to always explicitly close filehandles. :-) David On Sun, Jul 16, 2017 at 8:00 AM, Shawn H Corey wrote: > On Sun, 16 Jul 2017 07:36:39 -0400 > David Mertens wrote: > > > Also note that lexical filehandles close when they go out of scope > > True but

Re: Filehandle within foreach loop

2017-07-16 Thread Shawn H Corey
On Sun, 16 Jul 2017 07:36:39 -0400 David Mertens wrote: > Also note that lexical filehandles close when they go out of scope True but you should always explicitly close your files. This gives you a chance to report any errors it had, have rather than silently ignoring them. -- Don't stop wher

Re: Filehandle within foreach loop

2017-07-16 Thread David Mertens
ch expression won't close until the end of the program. David On Wed, Jul 12, 2017 at 5:28 PM, Jim Gibson wrote: > If you wish to terminate execution of a foreach loop without iterating > over all of the elements (@files, in this case) use the “last” statement: > > foreach my $file

Re: Filehandle within foreach loop

2017-07-12 Thread Shawn H Corey
On Thu, 13 Jul 2017 00:50:42 +0530 perl kamal wrote: > open (my $FH, $file) or die "could not open file\n"; A quick note: output the file name and error message to have a better idea of what went wrong. open (my $FH, $file) or die "could not open file $file: $!\n"; -- Don't stop where th

Re: Filehandle within foreach loop

2017-07-12 Thread Jim Gibson
If you wish to terminate execution of a foreach loop without iterating over all of the elements (@files, in this case) use the “last” statement: foreach my $file ( @files ) { # process file open( my $fh, ‘<‘, $file ) or die(…); while( my $line = <$fh> ) { # process line } close

Re: Filehandle within foreach loop

2017-07-12 Thread Chas. Owens
That code will read each line from each file. The problem is likely in the part that says: #[process the lines & hash construction.] What are you doing there? On Wed, Jul 12, 2017 at 3:23 PM perl kamal wrote: > Hello All, > > I would like to read multiple files and process them.But we could r

Filehandle within foreach loop

2017-07-12 Thread perl kamal
Hello All, I would like to read multiple files and process them.But we could read the first file alone and the rest are skipped from the while loop. Please correct me where am i missing the logic.Thanks. use strict; use warnings; my @files=qw(Alpha.txt Beta.txt Gama.txt); foreach my $file (@file

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 >

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
s 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 loops? Also, the first line that's printed contains "499" and I can't figure out whe

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 fo

Re: Question/Problem with foreach loop

2011-06-07 Thread CM Analyst
Gents, Sorry for my delayed response. Thank you for your suggestions. Based on your feedback, I made the following changes, and the hook is now working as expected. Thanks a million! my $taskstate = $taskEntity->GetFieldValue(state)->GetValue();     $session->OutputDebugString ("Task's state

Re: Using $variable outside a foreach loop

2011-06-03 Thread Uri Guttman
> "sw" == shawn wilson writes: sw> On Jun 3, 2011 3:17 PM, "Uri Guttman" wrote: >> >> >> perl -le 'my $x = "zzz" ; for $x ( qw( foo bar ) ) { print "L: $x" } print sw> "E: $x"' >> L: foo >> L: bar >> E: zzz sw> That's odd, I would have thought that would have given 'foo

Re: Using $variable outside a foreach loop

2011-06-03 Thread shawn wilson
On Jun 3, 2011 3:17 PM, "Uri Guttman" wrote: > > > perl -le 'my $x = "zzz" ; for $x ( qw( foo bar ) ) { print "L: $x" } print "E: $x"' > L: foo > L: bar > E: zzz > That's odd, I would have thought that would have given 'foo bar bar'. So, how would you keep data from a loop once you're outside of

Re: Using $variable outside a foreach loop

2011-06-03 Thread Uri Guttman
>>>>> "R" == Ruud writes: R> On 2011-06-03 17:37, sono...@fannullone.us wrote: >> Maybe it's too early in the morning here, but I can't seem to remember how to use a lexical $variable that is defined inside a foreach loop, outside of

Re: Using $variable outside a foreach loop

2011-06-03 Thread Dr.Ruud
On 2011-06-03 17:37, sono...@fannullone.us wrote: Maybe it's too early in the morning here, but I can't seem to remember how to use a lexical $variable that is defined inside a foreach loop, outside of that loop.=:\ Here's the loop: f

Re: Using $variable outside a foreach loop

2011-06-03 Thread sono-io
On Jun 3, 2011, at 1:38 PM, Shawn H Corey wrote: > That implies something is wrong with your logic. Yep. I came to the same conclusion. =:) -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/

Re: Using $variable outside a foreach loop

2011-06-03 Thread Shawn H Corey
On 11-06-03 03:58 PM, sono...@fannullone.us wrote: I wasn't very clear in what I wanted. Sorry. I wanted to use the value of $name in another loop but after testing That implies something is wrong with your logic. The question is: what value of $name do you want? The first? The last? E

Re: Using $variable outside a foreach loop

2011-06-03 Thread sono-io
orry. I wanted to use the value of $name in another loop but after testing, it looks like Shawn was correct when he wrote: > The variable used in a foreach loop is independent of anything outside the > loop. I actually had to use another variable in the second loop to grab the nam

Re: Using $variable outside a foreach loop

2011-06-03 Thread Uri Guttman
, if you want to use the variable outside of the for, BF> don't name the two variables the same. The rough rule of thumb is BF> that leaving off the my in a for(each) is probably introducing BF> subtle bugs into your program. why search PBP when the perldocs are very clear a

Re: Using $variable outside a foreach loop

2011-06-03 Thread Brian Fraser
On Fri, Jun 3, 2011 at 1:23 PM, Jim Gibson wrote: > Declare the variable just before the loop, and remove the 'my' from the > foreach statement: > > my $name; > foreach $name ( ... ) { > ... > } > That won't do. What that code actually translated to is my $name; for my $name ( ... ) { ... } Wi

Re: Using $variable outside a foreach loop

2011-06-03 Thread Uri Guttman
>>>>> "s" == sono-io writes: s>Maybe it's too early in the morning here, but I can't seem to remember how to use a lexical $variable that is defined inside a foreach loop, outside of that loop.=:\ s>Here's the loop: s>

Re: Using $variable outside a foreach loop

2011-06-03 Thread Shawn H Corey
On 11-06-03 11:37 AM, sono...@fannullone.us wrote: I want to use "$name" in another loop just after this one, but when I do, I get "Global symbol $name requires explicit package". Could someone please point me in the right direction? Certainly. The variable used

Re: Using $variable outside a foreach loop

2011-06-03 Thread sono-io
C.DeRykus wrote: > One option is an outer enclosing block that'll extend the scope of $name to > that entire block: Jim Gibson wrote: > Declare the variable just before the loop, and remove the 'my' from the > foreach statement: Thanks for the responses. I was able to get it to work

Re: Using $variable outside a foreach loop

2011-06-03 Thread C.DeRykus
On Jun 3, 8:37 am, sono...@fannullone.us wrote: > ... >         I want to use "$name" in another loop just after this one, but when I > do, I get "Global symbol $name requires explicit package". > One option is an outer enclosing block that'll extend the scope of $name to that entire block:

Re: Using $variable outside a foreach loop

2011-06-03 Thread Jim Gibson
At 8:37 AM -0700 6/3/11, sono...@fannullone.us wrote: Maybe it's too early in the morning here, but I can't seem to remember how to use a lexical $variable that is defined inside a foreach loop, outside of that loop.=:\ Here's the loop: foreach

Re: Using $variable outside a foreach loop

2011-06-03 Thread sono-io
On Jun 3, 2011, at 8:45 AM, Alan Haggai Alavi wrote: > Here, the scope of $name is limited to the foreach loop and not outside it. > So, you will have to declare the variable again for use outside the loop. But wouldn't that make the second "$name" a different vari

Re: Using $variable outside a foreach loop

2011-06-03 Thread Alan Haggai Alavi
Hello, > foreach my $name (split (/, */, $names)) { Here, the scope of $name is limited to the foreach loop and not outside it. So, you will have to declare the variable again for use outside the loop. Regards, Alan Haggai Alavi. -- The difference makes the difference. --

Using $variable outside a foreach loop

2011-06-03 Thread sono-io
Maybe it's too early in the morning here, but I can't seem to remember how to use a lexical $variable that is defined inside a foreach loop, outside of that loop.=:\ Here's the loop: foreach my $name (split (/, */, $names)) {

Re: Question/Problem with foreach loop

2011-05-18 Thread Uri Guttman
> "CA" == CM Analyst writes: CA> my $taskEntity = $session->GetEntity ('almtask', $_); that gets a perl object in $taskEntity. CA> $taskEntity->GetFieldValue(state)->GetValue(); where is the value being assigned to? $taskEntity is not being modified or set in that line of code. it is j

Re: Question/Problem with foreach loop

2011-05-18 Thread Jim Gibson
On 5/18/11 Wed May 18, 2011 5:06 PM, "CM Analyst" scribbled: > Hi, > > In this code, the intent is to iterate through the tasks and modify the ID > field. The Task record (entity) should not be modified if it's state equals > "Completed". > > When I run this routine, there are two problems:

Question/Problem with foreach loop

2011-05-18 Thread CM Analyst
Hi, In this code, the intent is to iterate through the tasks and modify the ID field. The Task record (entity) should not be modified if it's state equals "Completed". When I run this routine, there are two problems: Problem 1) The "if" statement is not being evaluated. The record (even if it

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

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. > > $recor

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 ) ? $rec

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}++ : '' ; T

Re: foreach loop

2011-03-20 Thread Uri Guttman
it. there is no reason to read it line by line. it is small enough to slurp. slurping is faster and generally cleaner than line by line. considering he was doing a poor version of slurping before, this is much better. >> print @report ; SF> This map will consume a lot of memory, better

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

foreach loop

2011-03-19 Thread Chris Stinemetz
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. Below is what I have so far. Thank you in advance, Chris #!/usr/bin/perl use warnings; use

Re: AW: Problem with foreach loop

2010-09-27 Thread Shlomi Fish
do: > > > > [code] > > my @arr1_copy = @arr1; > > > > while (defined (my $arr1_elem = shift(@arr1_copy))) { > > > > # Do something with $arr1_elem > > > > } > > [/code] > > Is the principle you are trying to explain that

Re: Problem with foreach loop

2010-09-27 Thread John W. Krahn
HACKER Nora wrote: Hello list, Hello, Could someone please explain why this test script: my @arr1 = qw(one two three); my @arr2 = qw(1 2 3); foreach my $arr1 ( @arr1 ) { print "Arr1: $arr1\n"; foreach my $arr2 ( @arr2 ) { print "Arr2: $arr2\n";

AW: Problem with foreach loop

2010-09-27 Thread HACKER Nora
(@arr1_copy))) { > # Do something with $arr1_elem > } > [/code] Is the principle you are trying to explain that the foreach loop should be changed to a while or that it is advisable to make a copy of the original array? (Or just both? :-) ) I suppose it's all about the 'w

Re: AW: Problem with foreach loop

2010-09-27 Thread Shlomi Fish
On Monday 27 September 2010 09:40:43 HACKER Nora wrote: > Hi Michael, > > > > Thanks for your fast reply. I didn't realize that the shift is of > immediate effect not only to the array but also to the current element > of iteration in the loop itself. > It may be. However, that is besides the

Re: Problem with foreach loop

2010-09-27 Thread Rob Coops
On Mon, Sep 27, 2010 at 9:19 AM, HACKER Nora wrote: > Hello list, > > Could someone please explain why this test script: > > my @arr1 = qw(one two three); > my @arr2 = qw(1 2 3); > > foreach my $arr1 ( @arr1 ) { >print "Arr1: $arr1\n"; >foreach my $arr2 ( @arr2 ) { >

AW: Problem with foreach loop

2010-09-27 Thread HACKER Nora
ember 2010 09:34 An: HACKER Nora Betreff: Re: Problem with foreach loop When $arr2 eq '2', you shift 'one' off @arr1, but you're still working with$ arr[0], which is now 'two'. When the second foreach ends, it moves onto $arr1[1], which is 'three'

Re: Problem with foreach loop

2010-09-27 Thread Shlomi Fish
Hi Nora, On Monday 27 September 2010 09:19:46 HACKER Nora wrote: > Hello list, > > Could someone please explain why this test script: > > my @arr1 = qw(one two three); > my @arr2 = qw(1 2 3); > > foreach my $arr1 ( @arr1 ) { > print "Arr1: $arr1\n"; > foreach my $arr2 ( @arr2 )

WG: Problem with foreach loop

2010-09-27 Thread HACKER Nora
ndet: Montag, 27. September 2010 09:20 > An: beginners@perl.org > Betreff: Problem with foreach loop > > Hello list, > > Could someone please explain why this test script: > > my @arr1 = qw(one two three); > my @arr2 = qw(1 2 3); > > foreach my $arr1 ( @arr1 ) { >

Problem with foreach loop

2010-09-27 Thread HACKER Nora
Hello list, Could someone please explain why this test script: my @arr1 = qw(one two three); my @arr2 = qw(1 2 3); foreach my $arr1 ( @arr1 ) { print "Arr1: $arr1\n"; foreach my $arr2 ( @arr2 ) { print "Arr2: $arr2\n"; if ( $arr2 eq '2' ) {

Re: next in foreach loop

2010-08-12 Thread Chas. Owens
On Aug 12, 2010, at 19:08, Kryten wrote: > Hi, > > Complete newbie. > > Is there any way to use "next" from within a foreach loop? > > All the examples I have seen/read use a while loop to demo. Yes, next will work on for/foreach, while, and until loops. So yo

Re: next in foreach loop

2010-08-12 Thread Jeff Pang
2010/8/13 Kryten : > Hi, > > Complete newbie. > > Is there any way to use "next" from within a foreach loop? > Sure. $ perl -le ' > for (1..10) { > next if $_ == 5; > print; > } ' 1 2 3 4 6 7 8 9 10 -- Jeff Pang http://home.arco

next in foreach loop

2010-08-12 Thread Kryten
Hi, Complete newbie. Is there any way to use "next" from within a foreach loop? All the examples I have seen/read use a while loop to demo. Thanks, Stuart -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/

Re: Syntax of Foreach loop

2010-03-31 Thread Shawn H Corey
On Tue, 30 Mar 2010 23:05:00 -0600 Jon Forsyth wrote: > I am truly a beginner. Could someone help me understand this syntax > out of a code example from "Learning Perl"? On line 5 I'm confused > as to why "my $number" is between "foreach" and the ()? is "my > $number part of the loop? Yes, it

AW: AW: Syntax of Foreach loop

2010-03-31 Thread Thomas Bätzler
Uri Guttman wrote: > > "TB" == Thomas Bätzler writes: > TB> with the -w switch or the "use warnings;" pragma which requires > TB> variables to be declared before their first use. > > that is the use strict pragma, not the warnings one. Of course. I knew I shouldn't have posted befor

Re: AW: Syntax of Foreach loop

2010-03-30 Thread Uri Guttman
> "TB" == Thomas Bätzler writes: TB> Jon Forsyth asked: >> I am truly a beginner. Could someone help me understand this syntax >> out of a code example from "Learning Perl"? On line 5 I'm confused >> as to why "my $number" is between "foreach" and the ()? is "my >> $number part

AW: Syntax of Foreach loop

2010-03-30 Thread Thomas Bätzler
the loop? "my" declares a variable for the lexical scope. While Perl by itself will happily let variables spring into existence whenever you first refer to them, the accepted best practice is to run perl with the -w switch or the "use warnings;" pragma which requires variab

Syntax of Foreach loop

2010-03-30 Thread Jon Forsyth
I am truly a beginner. Could someone help me understand this syntax out of a code example from "Learning Perl"? On line 5 I'm confused as to why "my $number" is between "foreach" and the ()? is "my $number part of the loop? sub running_sum { state $sum = 0; state @numbers; foreach my $

Re: map foreach loop into array

2010-03-21 Thread Shlomi Fish
On Sunday 21 Mar 2010 17:44:49 perl wrote: > I have been trying to make this thing work .. > this is not a actual code > @s = { some elemnts}; > foreach my $s(@s){ > # i made that $s into array something like @data1 > foreach my$data(@data1) >if( $data =~ some text){ >

map foreach loop into array

2010-03-21 Thread perl
I have been trying to make this thing work .. this is not a actual code @s = { some elemnts}; foreach my $s(@s){ # i made that $s into array something like @data1 foreach my$data(@data1) if( $data =~ some text){ #here i will get my data for sure

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) >

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

AW: foreach loop

2009-11-02 Thread Thomas Bätzler
Philip Potter asked: > 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 > about the magical: > > while (<$fil

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.

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

AW: foreach loop

2009-11-02 Thread Thomas Bätzler
Anant Gupta <mailto:anantgupta...@gmail.com> asked: > 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. You should probably not "slurp" (read in all at once) the file unless you need to. In your p

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

foreach loop

2009-11-01 Thread 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 use strict; open(FILE,"; foreach my $line(@lines) { if($lin =~ m/something/) { #some code # get next data # Without goi

Re: Difference between for & foreach loop

2009-05-25 Thread Chas. Owens
On Mon, May 25, 2009 at 23:50, sanket vaidya wrote: snip > So to conclude > 'for' & 'foreach' are the two different ways to write same thing & there is > no particular advantage of using one over other. > > Group, correct me if I am wrong. snip I wouldn't say there is no advantage; for is four ch

RE: Difference between for & foreach loop

2009-05-25 Thread sanket vaidya
-Original Message- From: Randal L. Schwartz [mailto:mer...@stonehenge.com] Sent: Monday, May 25, 2009 10:10 PM To: beginners@perl.org Subject: Re: Difference between for & foreach loop >>>>> ""sanket" == "sanket vaidya" writes: &quo

Re: Difference between for & foreach loop

2009-05-25 Thread Randal L. Schwartz
uot;sanket> separate? Can anyone suggest me a good url which can tell the difference "sanket> between two? Back in the early days of Perl, Larry Wall decided to borrow from both C (the "for" loop) and Csh (the "foreach" loop) to provide a means of a computed itera

Re: Difference between for & foreach loop

2009-05-25 Thread Chas. Owens
t as well get used to using for as an iterator loop now, because in Perl 6 there is no foreach loop. And the c-style loop is named loop. -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/

Re: Difference between for & foreach loop

2009-05-25 Thread Gunnar Hjalmarsson
sanket vaidya wrote: What is the difference between the 'for' & 'foreach' loops? I know that they can be used interchangeably then what is the purpose of keeping them separate? The _words_ 'for' and 'foreach' can be used interchangeably in foreach loops, but a true for loop is something else.

Re: Difference between for & foreach loop

2009-05-25 Thread Erez Schatz
2009/5/25 sanket vaidya : > What is the difference between the 'for' & 'foreach' loops? There is none. http://perldoc.perl.org/perlsyn.html#Foreach-Loops : "The foreach keyword is actually a synonym for the for keyword, so you can use foreach for readability or for for brevity. (Or because the

Difference between for & foreach loop

2009-05-24 Thread sanket vaidya
Hi all, What is the difference between the 'for' & 'foreach' loops? I know that they can be used interchangeably then what is the purpose of keeping them separate? Can anyone suggest me a good url which can tell the difference between two? Thanks & Regards, Sanket Vaidya _

  1   2   3   >