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
Even more readable: FILE: foreach my $file ( @files ) } ... last FILE if (some_condition); ... } Also note that lexical filehandles close when they go out of scope, except for the most recently "stat"ed file. Perl holds a reference to "the most recently stat-ed filehandle&qu

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
.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 (@files) > { > open (m

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

Re: map vs foreach

2014-10-01 Thread Andy Bach
you want to iterate with side-effects, then you should use a proper for or foreach loop. Somewhere there's a quote (possibly apocryphal but wait! [2]) from Larry Wall on a map vs foreach thread: "That being said, I'd never grep someone in a void context myself." He actually wasn

Re: map vs foreach

2014-09-30 Thread Shawn H Corey
On Tue, 30 Sep 2014 16:38:24 -0700 SSC_perl wrote: > On Sep 30, 2014, at 4:08 PM, Shawn H Corey wrote: > > code like you will have to read it after an all-night party > > (some day, you will). > > Those days are over, but point taken! ;) They're never over but they do get farther apart.

Re: map vs foreach

2014-09-30 Thread Uri Guttman
On 09/30/2014 05:08 PM, SSC_perl wrote: Is the output of these two lines equivalent? map { $hash->{$_} = shift @record } @{$self->{'FIELDNAMES'}}; $hash->{$_} = shift @record foreach @{$self->{'FIELDNAMES'}}; They appear to be in my te

Re: map vs foreach

2014-09-30 Thread SSC_perl
On Sep 30, 2014, at 4:19 PM, David Precious wrote: > So, e.g.: > > my %fields = map { $_ => shift @record } @{$self->{'FIELDNAMES'}}; > > ... would make sense, because you're using map to produce the data you > want, rather than using it instead of a for loop. Thanks, David. That was an

Re: map vs foreach

2014-09-30 Thread SSC_perl
On Sep 30, 2014, at 4:08 PM, Shawn H Corey wrote: > code like you will have to read it after an all-night party > (some day, you will). Those days are over, but point taken! ;) Thanks, Frank SurfShopCART https://github.com/surfshopcart/surfshop -- To unsubscribe, e-mail: beginners-uns

Re: map vs foreach

2014-09-30 Thread David Precious
On Tue, 30 Sep 2014 14:08:14 -0700 SSC_perl wrote: > Is the output of these two lines equivalent? > > map { $hash->{$_} = shift @record } @{$self->{'FIELDNAMES'}}; > > $hash->{$_} = shift @record foreach @{$self->{'FIELDNAMES'}}; [...] >

Re: map vs foreach

2014-09-30 Thread Shawn H Corey
On Tue, 30 Sep 2014 14:08:14 -0700 SSC_perl wrote: > Is the output of these two lines equivalent? > > map { $hash->{$_} = shift @record } @{$self->{'FIELDNAMES'}}; > > $hash->{$_} = shift @record foreach @{$self->{'FIELDNAMES'}}; > >

Re: map vs foreach

2014-09-30 Thread Omega -1911
ng schemes And, [ drum roll ] you're welcome. On Tue, Sep 30, 2014 at 5:08 PM, SSC_perl wrote: > Is the output of these two lines equivalent? > > map { $hash->{$_} = shift @record } @{$self->{'FIELDNAMES'}}; > > $hash->{$_} = shift @record f

map vs foreach

2014-09-30 Thread SSC_perl
Is the output of these two lines equivalent? map { $hash->{$_} = shift @record } @{$self->{'FIELDNAMES'}}; $hash->{$_} = shift @record foreach @{$self->{'FIELDNAMES'}}; They appear to be in my testing, but I'd like to make sure. Is o

Re: foreach and next

2012-04-09 Thread Rob Dixon
On 09/04/2012 14:24, Vyacheslav wrote: My code my %attr = ( PrintError => 0, RaiseError => 0 ); my $dbh = DBI->connect($dsn, $user, $pass, \%attr); unless ($dbh) { next; } my $query = "SHOW DATABASES"; I use unless ($dbh) { next; } and this work fine. Thanks 09.04.2012 01:22, Jim Gibson напи

Re: foreach and next

2012-04-09 Thread Vyacheslav
My code my %attr = ( PrintError => 0, RaiseError => 0 ); my $dbh = DBI->connect($dsn, $user, $pass, \%attr); unless ($dbh) { next; } my $query = "SHOW DATABASES"; I use unless ($dbh) { next; } and this work fine. Thanks 09.04.2012 01:22, Jim Gibson написал: At 12:50 AM + 4/9/12, Vyachesl

Re: foreach and next

2012-04-08 Thread Jim Gibson
At 12:50 AM + 4/9/12, Vyacheslav wrote: I enabled RaiserError, then script die. ... my %attr = ( PrintError => 0, RaiseError => 1 ); Use: RaiseError => 0 instead so that your script will not raise an exception and die. Then check $dbh->err. -- To unsubscribe, e-mail: beginne

Re: foreach and next

2012-04-08 Thread Vyacheslav
I enabled RaiserError, then script die. ... my %attr = ( PrintError => 0, RaiseError => 1 ); my $dbh = DBI->connect($dsn, $user, $pass, \%attr); # or die "Can't connect to the DB: $DBI::errstr\n"; my $query = "SHOW DATABASES"; my $sth = $dbh->prepare($query) or die "Can't prepare SQL st

Re: foreach and next

2012-04-08 Thread Dr.Ruud
On 2012-04-08 17:10, Vyacheslav wrote: using eval helped me. You should not use exceptions for normal code flow. Read the DBI docs (perldoc DBI). If a failed connection must be an exception, set RaiseError to true. But if it isn't an exception, leave it false, and test $dbh->err (or the glob

Re: foreach and next

2012-04-08 Thread Shlomi Fish
Hi Vyacheslav, On Sun, 08 Apr 2012 15:10:06 + Vyacheslav wrote: > Thanks all. > > using eval helped me. > The problem with eval in Perl 5 is that it catches any and all thrown exceptions . I.e: by default, it doesn't do Object-Oriented exceptions like Java, Ruby, Python and other language

Re: foreach and next

2012-04-08 Thread Vyacheslav
Thanks all. using eval helped me. 08.04.2012 09:43, Shlomi Fish пишет: Hi Vyacheslav, On Sun, 08 Apr 2012 06:12:53 + Vyacheslav wrote: Hello all. My english bad and i have a problem. I am connected to databases in a cycle foreach and the script die, if one of database is not available

Re: foreach and next

2012-04-08 Thread Shlomi Fish
Hi Vyacheslav, On Sun, 08 Apr 2012 06:12:53 + Vyacheslav wrote: > Hello all. > My english bad and i have a problem. > > I am connected to databases in a cycle foreach and the script die, if > one of database is not available. > > #!/usr/bin/perl > > use strict

Re: foreach and next

2012-04-08 Thread Binish A.R
Enclose DBI operation inside eval - #!/usr/bin/perl use strict; use warnings; use DBI; use DBD::mysql; foreach $db (&hostdb("$project")) { eval {         my $server = "$db";         my $dbname = "information_schema";    

foreach and next

2012-04-07 Thread Vyacheslav
Hello all. My english bad and i have a problem. I am connected to databases in a cycle foreach and the script die, if one of database is not available. #!/usr/bin/perl use strict; use warnings; use DBI; use DBD::mysql; foreach $db (&hostdb("$project")) { my $server = &qu

Re: Can recursion eliminate nested foreach() loops?

2012-03-05 Thread Dr.Ruud
On 2012-02-27 03:52, Shawn H Corey wrote: There is no simplify way of doing this. perl -wle ' my $s = "{red,green,blue},{small,large},{light,dark}"; print for glob $s; ' -- Ruud -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl

Re: Can recursion eliminate nested foreach() loops?

2012-02-27 Thread Paul Johnson
On Sun, Feb 26, 2012 at 09:30:44PM -0500, Steve Bertrand wrote: > I came across a question early this morning on a forum that > intrigued me. I literally spent about five hours trying everything > to solve it, but I couldn't. Sometimes Perl is just a means to get at the corect tool: my $attribute

Re: Can recursion eliminate nested foreach() loops?

2012-02-26 Thread Steve Bertrand
x27;, values => [qw/small large/] }, { type => 'shades', values => [qw/light dark/] }, ]; print "$_\n" foreach combos(map $_->{values}, @$attributes); sub combos { my $first = shift; return @$first unless @_; my @combinations; foreach my $beg (@$first) { foreach my $en

Re: Can recursion eliminate nested foreach() loops?

2012-02-26 Thread Rob Dixon
o use recursion to eliminate the repeated and pre-calculated calls to foreach as this OP is asking? From ... .. ... is verbatim 'dms000' ... I need to generate a list of combinations from a data structure such as: my $attributes = [ { type => 'colors', values => [qw/red g

Re: Can recursion eliminate nested foreach() loops?

2012-02-26 Thread Steve Bertrand
On 2012-02-26 21:52, Shawn H Corey wrote: On 12-02-26 09:30 PM, Steve Bertrand wrote: I know this isn't a beginner's question, but I know there are geniuses here. Is there a way to simplify this within Perl? There is no simplify way of doing this. Separate off the first attribute and cross-pro

Re: Can recursion eliminate nested foreach() loops?

2012-02-26 Thread Steve Bertrand
On 2012-02-26 21:52, Shawn H Corey wrote: On 12-02-26 09:30 PM, Steve Bertrand wrote: I know this isn't a beginner's question, but I know there are geniuses here. Is there a way to simplify this within Perl? There is no simplify way of doing this. Separate off the first attribute and cross-pro

Re: Can recursion eliminate nested foreach() loops?

2012-02-26 Thread Shawn H Corey
On 12-02-26 09:30 PM, Steve Bertrand wrote: I know this isn't a beginner's question, but I know there are geniuses here. Is there a way to simplify this within Perl? There is no simplify way of doing this. Separate off the first attribute and cross-product it with a recursive call to the rest.

Can recursion eliminate nested foreach() loops?

2012-02-26 Thread Steve Bertrand
pre-calculated calls to foreach as this OP is asking? From ... .. ... is verbatim 'dms000' ... I need to generate a list of combinations from a data structure such as: my $attributes = [ { type => 'colors', values => [qw/red green blue/] }, { type => &#

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

Re: Foreach loop and hash of arrays

2012-02-06 Thread Rob Dixon
27;,'717', ], ); my $customers_state = 'AZ'; my $customers_zip = '850'; my $match = 'no' ; STATE: foreach my $state (keys %states) { # print "$state \n"; if ($state eq $customers_state) {

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',&#

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
>>>>> "BF" == Brian Fraser writes: BF> 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 $

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

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 response

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: script takes long time to run when comparing digits within strings using foreach

2011-05-28 Thread John W. Krahn
#x27;, '1 2 9 10 11 12', '1 2 3 4 5 8', '1 2 3 4 5 9' , '6 7 8 9 10 11'); my $iteration_counter = -1; foreach (@datas){ $iteration_counter++; my $reference = $_; my $second_iteration_counter = -1; my $string = ''; for

Re: script takes long time to run when comparing digits within strings using foreach

2011-05-28 Thread Dr.Ruud
On 2011-05-27 10:18, eventual wrote: I have an array , @datas, and each element within @datas is a string that's made up of 6 digits with spaces in between like this “1 2 3 4 5 6”, so the array look like this @datas = ('1 2 3 4 5 6', '1 2 9 10 11 12', '1 2 3 4 5 8', '1 2 3 4 5 9' , '6 7 8 9 1

Re: script takes long time to run when comparing digits within strings using foreach

2011-05-27 Thread Shlomi Fish
# > > #!/usr/bin/perl > use strict; > > my @matched_location = (); > my @datas = ('1 2 3 4 5 6', '1 2 9 10 11 12', '1 2 3 4 5 8', '1 2 3 4 5 9' > , '6 7 8 9 10 11'); > my $iteration_coun

script takes long time to run when comparing digits within strings using foreach

2011-05-27 Thread eventual
2 9 10 11 12', '1 2 3 4 5 8', '1 2 3 4 5 9' , '6 7 8 9 10 11');   my $iteration_counter = -1; foreach (@datas){    $iteration_counter++;    my $reference = $_;      my $second_iteration_counter = -1;    my $string = '';    foreach (@datas){   $second_ite

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
t; modfied). In other words, if I have multiple records, just the first one in > the list gets modified. All others are skipped. > > Can someone help me figure out where I am going wrong with this? Thanks. > > > > # Iterate through all tasks > > foreach (@$tasks) { You w

Question/Problem with foreach loop

2011-05-18 Thread CM Analyst
eone help me figure out where I am going wrong with this? Thanks. # Iterate through all tasks foreach (@$tasks) { # Get a task entity for the current taskid (in $_) my $taskEntity = $session->GetEntity ('almtask', $_); $taskEntity->GetFieldValue(state)->GetValue()

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
On Monday 27 September 2010 10:17:16 HACKER Nora wrote: > Hi Shlomi, > > > You shouldn't modify an array (@arr1 in this case) while iterating > > over it using > > > foreach. Otherwise, the results will be unpredictable. One option to > > overcome it is to

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 &quo

AW: Problem with foreach loop

2010-09-27 Thread HACKER Nora
Hi Shlomi, > You shouldn't modify an array (@arr1 in this case) while iterating over it using > foreach. Otherwise, the results will be unpredictable. One option to > overcome it is to do: > > [code] > my @arr1_copy = @arr1; > > while (defined (my $arr1_elem = shift

Re: AW: Problem with foreach loop

2010-09-27 Thread Shlomi Fish
However, that is besides the point that one should not modify an array while one is iterating over it using foreach. Please think of a better way to achieve this. Furthermore, please configure you E-mail user agent to reply inline, and not top-post: http://en.wikipedia.org/wiki/Posting_style

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&quo

  1   2   3   4   5   6   >