For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > June 2005 > In search of elegant code - How to know if a loop iterated?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author In search of elegant code - How to know if a loop iterated?
usenet@DavidFilmer.com

2005-06-10, 3:58 pm

If I try to iterate a loop over an empty list, Perl just skips the
loop. Is there any way to know that's what happened (without kludges
such as setting counters or bool flags within the loop)?

What I would like to do is something 'elegant' like:

for (glob '*.txt') {
... do some stuff with the files ...
}else{ #Iwish I could do this!
warn "No files found!\n";
}

But, of course, I can't do use an else in a for loop. I can think of
all sorts of UGLY ways to accomplish this (like putting a counter in
the loop or assigning the glob to an array and then warning if the
array is empty), but I prefer something elegant that doesn't require
the creation of a special variable just to know if the loop iterated.
Anyone have any suggestions?

A. Sinan Unur

2005-06-10, 3:58 pm

usenet@DavidFilmer.com wrote in news:1118426161.787423.268140
@o13g2000cwo.googlegroups.com:

> If I try to iterate a loop over an empty list, Perl just skips the
> loop.


Well, what else would you want it (or any computer language) to do? You
can prove all sorts of interesting things by allowing one to assume the
existence of an element in the empty set.

> Is there any way to know that's what happened (without kludges
> such as setting counters or bool flags within the loop)?
>
> What I would like to do is something 'elegant' like:
>
> for (glob '*.txt') {
> ... do some stuff with the files ...
> }else{ #Iwish I could do this!
> warn "No files found!\n";
> }


No, you don't wish that, please.

> But, of course, I can't do use an else in a for loop. I can think of
> all sorts of UGLY ways to accomplish this (like ... assigning the glob
> to an array and then warning if the array is empty),


Why on every holy being's green, brown and blue earth is that ugly?


#! /usr/bin/perl

use strict;
use warnings;

my $ext = shift || 'txt';

if(my @files = glob "*.$ext") {
print "$_\n" for @files;
} else {
warn "No $ext files found in the current directory\n"
}

__END__

> but I prefer something elegant that doesn't require
> the creation of a special variable just to know if the loop iterated.


I don't understand you. A temporary will be created to hold the list
returned by glob anyway, why not just explicitly assign that, and
thereby access the information yo wanted to access? What's the drawback?

Sinan

--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
John Bokma

2005-06-10, 8:57 pm

A. Sinan Unur wrote:

>
> No, you don't wish that, please.


Why not?

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

A. Sinan Unur

2005-06-10, 8:57 pm

John Bokma <john@castleamber.com> wrote in
news:Xns967191587C863castleamber@130.133.1.4:

> A. Sinan Unur wrote:
>
>
> Why not?


There is a well established meaning of a 'for' loop in various
languages. It works for me, I am used to it, I see no benefit from
changing that meaning, and I do not want the number of people wishing
such a feature as the one the OP described to reach a critical mass.

True, none of these are 'objective', but they are good enough for me to
try to dissuade people.

Sinan

--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
Gunnar Hjalmarsson

2005-06-10, 8:57 pm

usenet@DavidFilmer.com wrote:
> If I try to iterate a loop over an empty list, Perl just skips the
> loop. Is there any way to know that's what happened (without kludges
> such as setting counters or bool flags within the loop)?
>
> What I would like to do is something 'elegant' like:
>
> for (glob '*.txt') {
> ... do some stuff with the files ...
> }else{ #Iwish I could do this!
> warn "No files found!\n";
> }


map { print "$_\n" } glob '*.txt' or warn "No files found!\n";

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Steven Kuo

2005-06-10, 8:57 pm

On Fri, 10 Jun 2005, Gunnar Hjalmarsson wrote:

> usenet@DavidFilmer.com wrote:
[color=darkred]
>
> map { print "$_\n" } glob '*.txt' or warn "No files found!\n";
>




Clever.

A minor edit would guard against the possibility of an empty list
being returned from map. Contrast:

map { () } ( 1 .. 3 ) or warn "No args";

vs.

map { ();1 } ( 1 .. 3 ) or warn "No args";

So perhaps:

map { dosomething(); 1 } glob '*.txt' or warn "No files found\n";

--
Regards,
Steven
John Bokma

2005-06-10, 8:57 pm

A. Sinan Unur wrote:

> John Bokma <john@castleamber.com> wrote in
> news:Xns967191587C863castleamber@130.133.1.4:
>
>
> There is a well established meaning of a 'for' loop in various
> languages.


And none for unless for example :-D

> It works for me, I am used to it, I see no benefit from
> changing that meaning, and I do not want the number of people wishing
> such a feature as the one the OP described to reach a critical mass.
>
> True, none of these are 'objective', but they are good enough for me
> to try to dissuade people.


The else option sounds useful to me. It doesn't matter that it's weird,
a lot of Perl things are weird. What matters is: is it a feature that is
usefull and improves readability? I think yes

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

John Bokma

2005-06-10, 8:57 pm

Steven Kuo wrote:

> So perhaps:
>
> map { dosomething(); 1 } glob '*.txt' or warn "No files found\n";


Ok, so my point

for { } else { } is getting more valid :-D I will remember this idea, and
see how often my code could become more readable with such an addition.

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

Brian Wakem

2005-06-10, 8:57 pm

John Bokma wrote:

> A. Sinan Unur wrote:
>
>
> And none for unless for example :-D
>
>
> The else option sounds useful to me. It doesn't matter that it's weird,
> a lot of Perl things are weird. What matters is: is it a feature that is
> usefull and improves readability? I think yes
>



I agree, though I think it should be called something other than 'else' to
avoid confusion.

Maybe

foreach(..) {
}
or {
}


--
Brian Wakem


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com