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

U

usenet

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

A. Sinan Unur

(e-mail address removed) wrote in @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

A. Sinan Unur


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
 
G

Gunnar Hjalmarsson

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";
 
S

Steven Kuo

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";
 
J

John Bokma

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

John Bokma

Steven said:
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.
 
B

Brian Wakem

John said:
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 {
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top