Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > In search of elegant code - How to know if a loop iterated?

Reply
Thread Tools

In search of elegant code - How to know if a loop iterated?

 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      06-10-2005
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?

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      06-10-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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 <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      06-10-2005
A. Sinan Unur wrote:

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


Why not?

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

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      06-10-2005
John Bokma <(E-Mail Removed)> wrote in
news:Xns967191587C863castleamber@130.133.1.4:

> A. Sinan Unur wrote:
>
>>> 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.

>
> 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 <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-10-2005
(E-Mail Removed) 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
 
Reply With Quote
 
Steven Kuo
Guest
Posts: n/a
 
      06-10-2005
On Fri, 10 Jun 2005, Gunnar Hjalmarsson wrote:

> (E-Mail Removed) 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";
>




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
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      06-10-2005
A. Sinan Unur wrote:

> John Bokma <(E-Mail Removed)> wrote in
> news:Xns967191587C863castleamber@130.133.1.4:
>
>> A. Sinan Unur wrote:
>>
>>>> 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.

>>
>> Why not?

>
> There is a well established meaning of a 'for' loop in various
> languages.


And none for unless for example

> 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

 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      06-10-2005
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 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

 
Reply With Quote
 
Brian Wakem
Guest
Posts: n/a
 
      06-10-2005
John Bokma wrote:

> A. Sinan Unur wrote:
>
>> John Bokma <(E-Mail Removed)> wrote in
>> news:Xns967191587C863castleamber@130.133.1.4:
>>
>>> A. Sinan Unur wrote:
>>>
>>>>> 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.
>>>
>>> Why not?

>>
>> There is a well established meaning of a 'for' loop in various
>> languages.

>
> And none for unless for example
>
>> 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
>



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

Maybe

foreach(..) {
}
or {
}


--
Brian Wakem


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
In search of elegant code: is variable is within a range??? David Filmer Perl Misc 11 12-05-2003 02:46 AM
In search of elegant code: inverting a string David Filmer Perl Misc 9 10-30-2003 08:00 PM
IN SEARCH OF ELEGANT CODE: Setting a hash value to a chomp'ed thing David Filmer Perl Misc 9 09-03-2003 09:07 PM



Advertisments