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
.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: calculate within a loop

2016-09-21 Thread Chris Charley
""Chris Charley"" wrote in message news:20160912202839.22177.qm...@lists-nntp.develooper.com... "Nathalie Conte" wrote in message news:9d0654e0-8ec0-4051-87ca-541f90931...@ebi.ac.uk... Dear all, Thanks a lot for the codes various people which all work perfectly!! I have also discover som

Re: calculate within a loop

2016-09-14 Thread Chris Charley
""Chris Charley"" wrote in message news:20160912202839.22177.qm...@lists-nntp.develooper.com... "Nathalie Conte" wrote in message news:9d0654e0-8ec0-4051-87ca-541f90931...@ebi.ac.uk... Dear all, [snip] many thanks again for any tips/help, Nathalie [Chris wrote] [snip] for my $name (sor

Re: calculate within a loop

2016-09-12 Thread Uri Guttman
On 09/12/2016 06:48 PM, Aaron Wells wrote: @Jim, That eval bit i think Nathalie got from me. I need to review my core Perl. I think i’m in the habit of assuming Perl warns against any usage of undefined values, even in boolean context. Not so. I’ve proven to myself that this is not the case:

Re: calculate within a loop

2016-09-12 Thread Aaron Wells
@Jim, That eval bit i think Nathalie got from me. I need to review my core Perl. I think i’m in the habit of assuming Perl warns against any usage of undefined values, even in boolean context. Not so. I’ve proven to myself that this is not the case: $ perl -E’ >my ($foo, $bar); # these aren’t

Re: calculate within a loop

2016-09-12 Thread Chris Charley
"Nathalie Conte" wrote in message news:9d0654e0-8ec0-4051-87ca-541f90931...@ebi.ac.uk... Dear all, Thanks a lot for the codes various people which all work perfectly!! I have also discover some useful functions (eval and state) which can also be very helpful for this kind of data. In the li

Re: calculate within a loop

2016-09-12 Thread Jim Gibson
> On Sep 12, 2016, at 6:24 AM, Nathalie Conte wrote: > > Dear all, > > Thanks a lot for the codes various people which all work perfectly!! I have > also discover some useful functions (eval and state) which can also be very > helpful for this kind of data. > > In the light of this, my datas

Re: calculate within a loop

2016-09-12 Thread Nathalie Conte
keys %{ $hall{$name}{$subject}}) { state ($lgrade,$lsubject,$lname,$ltimes); my $grade = $hall{$name}{$subject}; print "grade is .$grade\n"; my $times = $hall{$name}{$subject}{$values}; print "time is .$times\n"; if (eval { $lgrade and $lname eq

Re: calculate within a loop

2016-09-10 Thread Aaron Wells
s %{$nameref}' would be the way to go. On Sat, Sep 10, 2016, 4:11 PM Jim Gibson wrote: > > > On Sep 9, 2016, at 8:54 AM, Nathalie Conte wrote: > > > > Hello, > > I have a question about making a calculation within a loop > > > > I have a hash o

Re: calculate within a loop

2016-09-10 Thread Jim Gibson
> On Sep 9, 2016, at 8:54 AM, Nathalie Conte wrote: > > Hello, > I have a question about making a calculation within a loop > > I have a hash of hashes > ## > #!/usr/bin/perl > use strict; > use warnings; > > use Data::Dumper qw(Dumper); &

Re: calculate within a loop

2016-09-09 Thread Gil Magno
Hi, Nathalie You could try the attached code. Best gil On 09/09/16 12:54, Nathalie Conte wrote: > Hello, > I have a question about making a calculation within a loop > > I have a hash of hashes > ## > #!/usr/bin/perl > use strict; > use warnings; > &

calculate within a loop

2016-09-09 Thread Nathalie Conte
Hello, I have a question about making a calculation within a loop I have a hash of hashes ## #!/usr/bin/perl use strict; use warnings; use Data::Dumper qw(Dumper); my %grades; $grades{"Foo "}{1} = 97; $grades{"Foo "}{2}= 107; $grades{"Peti "}

Re: XML Simple + parsing inner loop elements + help

2015-12-08 Thread perl kamal
Hi, Thanks you for your valuable comments,let me try the Twig module. On 12/8/15, Kent Fredric wrote: > On 8 December 2015 at 19:25, perl kamal wrote: >> I am trying to parse the inner loop elements of the attached input xml >> elements. >> The below code doesn'

Re: XML Simple + parsing inner loop elements + help

2015-12-08 Thread shawn wilson
tl;dr I'm not answering your specific question here. On Dec 8, 2015 1:26 AM, "perl kamal" wrote: > > Hi, > > I am trying to parse the inner loop elements of the attached input xml elements. Just fyi, I've found it easier to use xslt as an etl preprocessor to perl

Re: XML Simple + parsing inner loop elements + help

2015-12-08 Thread Kent Fredric
On 8 December 2015 at 19:25, perl kamal wrote: > I am trying to parse the inner loop elements of the attached input xml > elements. > The below code doesn't retrieve the inner loop() elements if > the properties tag contains more than one item. Will you please point > the

XML Simple + parsing inner loop elements + help

2015-12-07 Thread perl kamal
Hi, I am trying to parse the inner loop elements of the attached input xml elements. The below code doesn't retrieve the inner loop() elements if the properties tag contains more than one item. Will you please point the error and correct me. Please find the attached input xml file. Thanks.

Re: Statement failure inside a loop

2015-06-18 Thread Илья Рассадин
200 > Marco wrote: > > > Hello. > > > > I need some help in understanding why the first > > > > my ($Count,$Saw)=$self->{_Portobj}->read(1); > > > > is executed without any problem , however the second identical statement > > inside the while

Re: Statement failure inside a loop

2015-06-18 Thread Shlomi Fish
ical statement > inside the while loop fails with the error > > Can't call method "read" on an undefined value at wt800.pm line 121 > The problem is that you misspelled "_Portobj" as "_PortObj". This is easier to catch if you use accessors: * htt

Statement failure inside a loop

2015-06-18 Thread Marco
Hello. I need some help in understanding why the first my ($Count,$Saw)=$self->{_Portobj}->read(1); is executed without any problem , however the second identical statement inside the while loop fails with the error Can't call method "read" on an undefined value

Re: prime numbers - loop

2015-03-14 Thread Rob Dixon
ot clear what your array @div_array is for, as you only ever test that it contains the number 1 ($value never changes). What is happening is that, even for the non-primes, the `for` loop comes to an end and "is A prime number" is printed for everything. To fix it you need to add a `return`

prime numbers - loop

2015-03-14 Thread Ilan Shlossman
Hi , I asked this question on linkedin but I think the best place to get answers is here ... I have a perl cod that check a range of numbers and print for each number if it's prime number or not prime number. So why its not working properly ? ... #!C:\strawberry\perl\bin\perl use strict; use war

Re: loop does select next line

2014-07-05 Thread Kent Fredric
e; > *foreach $c (keys @words){* >print "word $words[$c]\n"; >$hash{W_c_start}++ if ($words[$c] =~ /^C.*/i); > } > } > > print "$_ :: $hash{$_}\n" foreach (keys %hash); > ==

loop does select next line

2014-07-05 Thread Sunita Pradhan
$c]\n"; $hash{W_c_start}++ if ($words[$c] =~ /^C.*/i); } } print "$_ :: $hash{$_}\n" foreach (keys %hash); = I want to use "while" loop instead of "foreach" . I tried it but

Re: MySQL, Perl, and LaTeX: Trying to loop through results by DAY (as a LaTeX section) for custom PDF report

2013-09-15 Thread J M
ot; . $date_added->strftime("%a, %d %b %Y ") . "} "; $date = $date_added->strftime("%d");} On Sun, Sep 15, 2013 at 1:16 AM, J M wrote: > > > <http://stackoverflow.com/questions/18809731/mysql-perl-and-latex-trying-to-loop-through-results-by-day-as-a-lat

MySQL, Perl, and LaTeX: Trying to loop through results by DAY (as a LaTeX section) for custom PDF report

2013-09-15 Thread J M
<http://stackoverflow.com/questions/18809731/mysql-perl-and-latex-trying-to-loop-through-results-by-day-as-a-latex-sectio#> I have written a Perl script (shown below) to loop through my MySQL database and then output a .tex file, as a sort of programmed custom report. As it stands, it

Re: perl hash loop: keys() vs values()

2013-06-15 Thread richard
Thanks. I looked at this site while searching for solutions but I probably skipped the section on sorting because I wasn't sorting the hash. regards, Richard -- tmqrich...@gmail.com On Sun, Jun 16, 2013 at 3:17 AM, Peter Gordon wrote: > On Sat, 15 Jun 2013 12:12:56 -0400, richard wrote: > >Hi >

Re: perl hash loop: keys() vs values()

2013-06-15 Thread Peter Gordon
On Sat, 15 Jun 2013 12:12:56 -0400, richard wrote: >Hi > >I'm trying to understand the difference between the keys() and >values () operators. What I'm not getting is why a hash must be >reversed to get the key corresponding to a value? (as stated in >perldoc). Can someone explain this please? > >

Re: perl hash loop: keys() vs values()

2013-06-15 Thread richard
7;c', '3], > ['d', 2], > ); > > To get the "value" for the element with the index 1 you need to do: > > my $value = $array[1][1]; #the value "2" > > and to get the "key" for the element with the index 1, you can do: > &g

Re: perl hash loop: keys() vs values()

2013-06-15 Thread Octavian Rasnita
and to get the "key" for the element with the index 1, you can do: my $key = $array[1][0]; #Will get "b" I don't know what you need to do... that's why I gave the idea of using arrays of arrays... --Octavian - Original Message - From: richard To: beginners@

perl hash loop: keys() vs values()

2013-06-15 Thread richard
Hi I'm trying to understand the difference between the keys() and values () operators. What I'm not getting is why a hash must be reversed to get the key corresponding to a value? (as stated in perldoc). Can someone explain this please? Here is my test script; My perl is freebsd 5.8.9 use warnin

Re: loop with additive matches

2013-05-01 Thread Jim Gibson
On May 1, 2013, at 11:27 AM, Noah wrote: > Hi there, > > I want to figure out a way to create a loop that will do actions based on > matches it collects in the loop and continue to do actions on all matches > till these are not m > > Hope this pseudo code makes sense:

loop with additive matches

2013-05-01 Thread Noah
Hi there, I want to figure out a way to create a loop that will do actions based on matches it collects in the loop and continue to do actions on all matches till these are not m Hope this pseudo code makes sense: 1) start loop 2) find all matches 3) end loop 4) start loop 5) do some action

Re: no warnings inside a loop

2013-04-03 Thread Rob Dixon
On 04/04/2013 00:29, Angela Barone wrote: I'm just curious about this. If you put "no warnings" inside a loop, is it good only for that loop, or will it be in effect until the end of the script? Also see `perllexwarn` Rob -- To unsubscribe, e-mail: beginners-unsubscr.

Re: no warnings inside a loop

2013-04-03 Thread Rob Dixon
On 04/04/2013 00:29, Angela Barone wrote: I'm just curious about this. If you put "no warnings" inside a loop, is it good only for that loop, or will it be in effect until the end of the script? Hi Angela The `warnings` pragma is *lexically* scoped. That means it applies

Re: no warnings inside a loop

2013-04-03 Thread Charles DeRykus
On Wed, Apr 3, 2013 at 4:29 PM, Angela Barone wrote: > I'm just curious about this. If you put "no warnings" inside a > loop, is it good only for that loop, or will it be in effect until the end > of the script? > See: perldoc perllexwarn >From above

Re: no warnings inside a loop

2013-04-03 Thread Jim Gibson
On Apr 3, 2013, at 4:29 PM, Angela Barone wrote: > I'm just curious about this. If you put "no warnings" inside a loop, > is it good only for that loop, or will it be in effect until the end of the > script? 'no warnings' is lexically scoped, meaning

Re: no warnings inside a loop

2013-04-03 Thread Brian Fraser
On Wed, Apr 3, 2013 at 8:29 PM, Angela Barone wrote: > I'm just curious about this. If you put "no warnings" inside a > loop, is it good only for that loop, or will it be in effect until the end > of the script? > > Only for the loop -- warnings, along w

no warnings inside a loop

2013-04-03 Thread Angela Barone
I'm just curious about this. If you put "no warnings" inside a loop, is it good only for that loop, or will it be in effect until the end of the script? Thanks, Angela A2 Hosting now has Perl 5.10.1 http://www.a2hosting.com/1250.html -- To unsubscribe, e-mail: beg

Re: anonymous array for loop

2013-03-10 Thread Brandon McCaig
On Sun, Mar 10, 2013 at 11:06:14AM -0400, Brandon McCaig wrote: > There is no array here. There are only lists. See perldoc > perldata. > > > List value constructors > > List values are denoted by separating individual values > > by commas (and enclosing the list in parentheses where

Re: anonymous array for loop

2013-03-10 Thread Brandon McCaig
semantics). Thus, in your example above you passed split /PATTERN/ and /EXPR/ (split is built-in and its semantics are not the same as user-defined functions), and in exchange split returned a list of ('a', 'b', 'c'), which your for-loop then iterated over. At no poin

Re: anonymous array for loop

2013-03-09 Thread Brandon McCaig
On Sat, Mar 09, 2013 at 08:19:43PM -0600, Chris Stinemetz wrote: > I don't think that is true. > > Example being: > > #!/usr/bin/perl > use warnings; > use strict; > > use Data::Dumper; > > my $firstVar = "One"; > my $secondVar = "Two"; > > my @array; > push @array,[$firstVar, $secondVar]; >

Re: anonymous array for loop

2013-03-09 Thread Chris Stinemetz
On Sat, Mar 9, 2013 at 2:57 PM, Brandon McCaig wrote: > On Sat, Mar 09, 2013 at 07:24:37AM -0600, Chris Stinemetz wrote: > > Each anonymous array @files and @newFiles has 5 total elements. > > Just to nitpick, @files and @newFiles are not anonymous arrays. > They are just arrays. They have names.

Re: anonymous array for loop

2013-03-09 Thread Brandon McCaig
On Sat, Mar 09, 2013 at 07:24:37AM -0600, Chris Stinemetz wrote: > Each anonymous array @files and @newFiles has 5 total elements. Just to nitpick, @files and @newFiles are not anonymous arrays. They are just arrays. They have names. You cannot have anonymous arrays in Perl except by reference. Th

Re: anonymous array for loop

2013-03-09 Thread *Shaji Kalidasan*
-- Your talent is God's gift to you. What you do with it is your gift back to God. --- From: Chris Stinemetz To

Re: anonymous array for loop

2013-03-09 Thread Chris Stinemetz
Never mind, I got it. I was over thinking it. I just created one anonymous array with the elements I wanted then used the following loop. foreach my $file (@files) { print join( "\t", @$file[0], @$file[1] ), "\n"; $ftp->put(@$file[0], @$file[1]) || die "can'

anonymous array for loop

2013-03-09 Thread Chris Stinemetz
Thank you in advance. Each anonymous array @files and @newFiles has 5 total elements. How can I alter this nested for so that just the unique elements are printed instead of each element twice? foreach my $file (@files) { foreach my $newFileName ( @newFiles ) { print join( "\t", @$file, @$ne

Re: How to end a loop

2012-03-14 Thread lina
This way works: ## read the .bib database and extract the cited out foreach $item (@citeditems){ match($item); } sub match{ open my $fh2, '<', $bib_filename; LINE: while(my $line = <$fh2>){ if ($line =~ m/@_\,/ .. $line =~ /^\}$/){

Re: How to end a loop

2012-03-14 Thread lina
## read the .bib database and extract the cited out open my $fh2, '<', $bib_filename; foreach $item (@citeditems){ match($item); } sub match{ my $reg = @_; LINE: while(my $line = <$fh2>){ if ($line =~ m/$reg\,/ .. $line =~ /^\}$/){

Re: How to end a loop

2012-03-14 Thread timothy adigun
Hi Lina, On Wed, Mar 14, 2012 at 10:15 AM, lina wrote: > Thanks all, here comes the update one: > > #!/usr/bin/env perl > > use strict; > use warnings; > use Carp qw(croak); > use autodie qw(open close); > use File::Basename; > > croak "Usage: ./bibextract.pl tex_file.tex" unless defined $ARGV[0

Re: How to end a loop

2012-03-14 Thread lina
Thanks all, here comes the update one: #!/usr/bin/env perl use strict; use warnings; use Carp qw(croak); use autodie qw(open close); use File::Basename; croak "Usage: ./bibextract.pl tex_file.tex" unless defined $ARGV[0]; my $infile = $ARGV[0]; my $tex_file = $infile; #my $bib_filename = "/hom

Re: How to end a loop

2012-03-14 Thread lina
open my $fh, '<', $tex_file; while(<$fh>){ if(/cite\{(\S+)\}/){ $cited = sprintf "%s\n",$1; $cited =~ tr/\,/\n/; push(@citeditems,$cited); } } close($fh); ## read the .bib database and extract the cited out open my $fh2, '<',

Re: How to end a loop

2012-03-14 Thread lina
I updated a bit to sr/bin/env perl use strict; use warnings; use Carp qw(croak); use autodie qw(open close); use File::Basename; croak "Usage: ./bibextract.pl tex_file.tex" unless defined $ARGV[0]; my $infile = $ARGV[0]; my $tex_file = $infile; #my $bib_filename = "/home/lina/texmf/bibtex/bib/

How to end a loop

2012-03-13 Thread lina
#x27;, $tex_file; while(<$fh>){ if(/cite\{(\S+)\}/){ $cited = sprintf "%s\n",$1; $cited =~ tr/\,/\n/; print $cited; } } close($fh); ## read the .bib database and extract the cited out open my $fh2, '<',

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
rint $match; You are missing the point of hashes: that they can be indexed directly by a key value, and there is no need to loop through all elements of the has. You can reduce you code to just one loop: foreach my $zip (@{$states{$customers_state}}) { if ($zi

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: loop break condition

2011-08-30 Thread Mike McClain
On Mon, Aug 29, 2011 at 03:27:52PM +0100, Rob Dixon wrote: > > Hey Mike. Ruud was just proposing a "wouldn't it be nice if" syntax. It > isn't valid Perl! > > Rob Well that explains the errors. I've gotten so many good ideas from his posts it didn't dawn on me to doubt the validity of this one.

Re: loop break condition

2011-08-29 Thread Rob Dixon
On 28/08/2011 19:48, Mike McClain wrote: On Sat, Aug 27, 2011 at 08:16:50PM +0200, Dr.Ruud wrote: I would like 'lazy' syntax like: <$fh> x {/^#/ or print}; to process a list. When I tried your code like so: open my $fh, '<', $file or die qq(Unable to open $file: $! ); <$fh>

Re: loop break condition

2011-08-28 Thread Mike McClain
On Sat, Aug 27, 2011 at 08:16:50PM +0200, Dr.Ruud wrote: > I would like 'lazy' syntax like: > > <$fh> x {/^#/ or print}; > > to process a list. When I tried your code like so: open my $fh, '<', $file or die qq(Unable to open $file: $! ); <$fh> x {/^\s*#/ or print}; close $fh

Re: loop break condition

2011-08-28 Thread Dr.Ruud
On 2011-08-26 19:23, Brandon McCaig wrote: Personally I think that it's easier to read without the labels. I think that any programmer that added an inner loop and didn't refactor the corresponding 'next', 'last', or 'redo' statement should be given up on

Re: loop break condition

2011-08-27 Thread Dr.Ruud
On 2011-08-26 21:08, Rob Dixon wrote: But I would be sad to rule against niceties like not /^#/ and print for <$fh>; Which can also be written as /^#/ or print for <$fh>; I would like 'lazy' syntax like: <$fh> x {/^#/ or print}; to process a list. -- Ruud -- To unsubscribe, e-mail:

Re: loop break condition

2011-08-27 Thread C.DeRykus
On Aug 26, 12:25 pm, shlo...@shlomifish.org (Shlomi Fish) wrote: > ... > The problem starts to happen when you try to declare $a and $b using my. This > program: > > [CODE] > #!/usr/bin/perl > > use strict; > use warnings; > > my $a = 5; > my $b = 6; > > print map { "$_\n" } sort { $a <=> $b } (9,1

Re: loop break condition

2011-08-27 Thread C.DeRykus
On Aug 26, 12:25 pm, shlo...@shlomifish.org (Shlomi Fish) wrote: > On Fri, 26 Aug 2011 20:08:31 +0100 > > ... > > The problem starts to happen when you try to declare $a and $b using my. This > program: > > [CODE] > #!/usr/bin/perl > > use strict; > use warnings; > > my $a = 5; > my $b = 6; > > pri

Re: loop break condition

2011-08-27 Thread Rob Dixon
On 26/08/2011 18:12, Jim Gibson wrote: On 8/25/11 Thu Aug 25, 2011 5:20 PM, "Rob Dixon" scribbled: On 25/08/2011 20:36, Shlomi Fish wrote: If you want to use $_ so be it, but it can easily introduce subtle errors into your code, because $_ is so easy to modify and clobber. So I would recomm

Re: loop break condition

2011-08-26 Thread Shlomi Fish
information on which to base this decision. > > > > They also need to be encouraged to use "best practices". However, what > > exactly best practices are is open to debate. > > I believe the fragility of $_ is apocryphal, and Shlomi's insistence on > avoidi

Re: loop break condition

2011-08-26 Thread Shawn H Corey
On 11-08-26 03:08 PM, Rob Dixon wrote: As an aside, the same applies to the common warning against using the global values $a and $b 'because they are used by sort'. Once again, sort localizes these variables within the comparison code so there is very little chance of inadvertently modifying the

Re: loop break condition

2011-08-26 Thread Rob Dixon
dds noise to a program and makes it frustrating to read. As Randal says, $_ is localized everywhere it is used implicitly by a loop, including foreach, map and grep as well as the List::Util functions, so I believe that corrupting it is rarely a problem in practice. The significant exception is whi

Re: loop break condition

2011-08-26 Thread Brandon McCaig
ly I think that it's easier to read without the labels. I think that any programmer that added an inner loop and didn't refactor the corresponding 'next', 'last', or 'redo' statement should be given up on. :P Whenever you modify code you can potentially

Re: loop break condition

2011-08-26 Thread Jim Gibson
On 8/25/11 Thu Aug 25, 2011 5:20 PM, "Rob Dixon" scribbled: > On 25/08/2011 20:36, Shlomi Fish wrote: >> >> If you want to use $_ so be it, but it can easily introduce subtle errors >> into >> your code, because $_ is so easy to modify and clobber. So I would recommend >> against these, and st

Re: loop break condition

2011-08-25 Thread Rob Dixon
On 25/08/2011 20:36, Shlomi Fish wrote: If you want to use $_ so be it, but it can easily introduce subtle errors into your code, because $_ is so easy to modify and clobber. So I would recommend against these, and still think it's a good idea. Please substantiate this assertion. I believe you

Re: loop break condition

2011-08-25 Thread Shlomi Fish
Hi Randal, On Thu, 25 Aug 2011 11:41:34 -0700 mer...@stonehenge.com (Randal L. Schwartz) wrote: > > "Shlomi" == Shlomi Fish writes: > > Shlomi> Well, I believe I've always avoided using an implicit $_ as > Shlomi> preventative measure (out of thinking I know better) and so cannot > Shlomi>

Re: loop break condition

2011-08-25 Thread Randal L. Schwartz
> "Shlomi" == Shlomi Fish writes: Shlomi> Well, I believe I've always avoided using an implicit $_ as preventative Shlomi> measure (out of thinking I know better) and so cannot present such a case Shlomi> first-hand. However, see: Shlomi> http://www.forum2.org/gaal/perl/Pitfall/slide001.htm

Re: loop break condition

2011-08-25 Thread Shlomi Fish
can be clobbered and devastated too > Shlomi> easily, which also makes depending on it error prone. > > Well, that's interesting. I've *never* accidentally clobbered > $_. Nearly everything that uses $_ localizes it immediately, except for > the implicit assignment t

Re: loop break condition

2011-08-25 Thread Randal L. Schwartz
one. Well, that's interesting. I've *never* accidentally clobbered $_. Nearly everything that uses $_ localizes it immediately, except for the implicit assignment to $_ during a while-reading-filehandle loop, or being the target of a s/old/new/ operation, which I consider something to learn properl

Re: loop break condition

2011-08-25 Thread Shlomi Fish
Hi Rob, On Wed, 24 Aug 2011 23:07:13 +0100 Rob Dixon wrote: > On 22/08/2011 10:29, Shlomi Fish wrote: > > Hi Alan, > > > > On Mon, 22 Aug 2011 14:43:48 +0530 > > Alan Haggai Alavi wrote: > > > >> Hello Shlomi, > >> > >>> It's a good idea to always use "last LABEL;" instead of "last;" (as well >

Re: loop break condition

2011-08-24 Thread Rob Dixon
On 22/08/2011 10:29, Shlomi Fish wrote: Hi Alan, On Mon, 22 Aug 2011 14:43:48 +0530 Alan Haggai Alavi wrote: Hello Shlomi, It's a good idea to always use "last LABEL;" instead of "last;" (as well as "next LABEL;" etc. in case more loops are added in between. ⋮ http://perl-begin.org/tutorial

Re: loop break condition

2011-08-24 Thread Jim Gibson
On 8/24/11 Wed Aug 24, 2011 8:32 AM, "Emeka" scribbled: > Do we really need "goto" here? > > Emeka No, we don't need a goto here. The statement 'last' is effectively a 'goto' but with the restriction that it will only work inside a loop

Re: loop break condition

2011-08-24 Thread Shlomi Fish
On Wed, 24 Aug 2011 16:32:30 +0100 Emeka wrote: > Do we really need "goto" here? > Where do you see a "goto"? Perl 5 has a goto statement (see http://perldoc.perl.org/functions/goto.html ) and it's pretty flexible, but we did not use it here, and instead used "last LABEL" or "next LABEL" which

Re: loop break condition

2011-08-24 Thread Emeka
Do we really need "goto" here? Emeka On Mon, Aug 22, 2011 at 10:03 AM, Shlomi Fish wrote: > Hi Alan, > > On Mon, 22 Aug 2011 13:10:05 +0530 > Alan Haggai Alavi wrote: > > > Hello Anant, > > > > > i want to input some numbers via in while

Re: loop break condition

2011-08-22 Thread Shawn H Corey
On 11-08-22 07:37 PM, Randal L. Schwartz wrote: "anant" == anant mittal writes: anant> $ln[$i]=; I'd swear that lowercase "stdin" was deprecated already, but I can't find any record of it in the deltas, and it still works in 5.12 (I don't have 5.14 compiled here). In any case, you should s

Re: loop break condition

2011-08-22 Thread Randal L. Schwartz
> "anant" == anant mittal writes: anant> $ln[$i]=; I'd swear that lowercase "stdin" was deprecated already, but I can't find any record of it in the deltas, and it still works in 5.12 (I don't have 5.14 compiled here). In any case, you should shift to the proper STDIN, as everyone else's a

Re: loop break condition

2011-08-22 Thread Shawn H Corey
On 11-08-22 05:03 AM, Shlomi Fish wrote: It's a good idea to always use "last LABEL;" instead of "last;" (as well as "next LABEL;" etc. in case more loops are added in between. Good idea but try to choose meaningful names. Also, the else clause is not needed. [CODE] use strict; use warnings

Re: loop break condition

2011-08-22 Thread Shlomi Fish
Hi Alan, On Mon, 22 Aug 2011 14:43:48 +0530 Alan Haggai Alavi wrote: > Hello Shlomi, > > > It's a good idea to always use "last LABEL;" instead of "last;" (as well as > > "next LABEL;" etc. in case more loops are added in between. > > ⋮ > > http://perl-begin.org/tutorials/bad-elements/#flow-stm

Re: loop break condition

2011-08-22 Thread Alan Haggai Alavi
Hello Shlomi, > It's a good idea to always use "last LABEL;" instead of "last;" (as well as > "next LABEL;" etc. in case more loops are added in between. > ⋮ > http://perl-begin.org/tutorials/bad-elements/#flow-stmts-without-labels Now I understand why it is always good to label loops that use `l

Re: loop break condition

2011-08-22 Thread Shlomi Fish
Hi Alan, On Mon, 22 Aug 2011 13:10:05 +0530 Alan Haggai Alavi wrote: > Hello Anant, > > > i want to input some numbers via in while loop.And loop should be > > broken if any nonnumeric character is entered.So how

Re: loop break condition

2011-08-22 Thread timothy adigun
arr=qw( home father son sun mother drinks); for(my $i=0;$i<=1;$i++){ pop @arr; # you can also use function "pop" to reduce the # element of array, here using a for loop to # iterate twice with pop we removed the last # two

Re: loop break condition

2011-08-22 Thread Alan Haggai Alavi
Hello Anant, > i want to input some numbers via in while loop.And loop should be > broken if any nonnumeric character is entered.So how it can be checked. > > > . > my @ln; > my $i=0; > print"Give line numbers

loop break condition

2011-08-21 Thread anant mittal
hello! i want to input some numbers via in while loop.And loop should be broken if any nonnumeric character is entered.So how it can be checked. . my @ln; my $i=0; print"Give line numbers you want to put into array.\n"; while(1){ $ln[$i

  1   2   3   4   5   6   7   8   9   10   >