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

Discussion in 'Perl Misc' started by usenet@DavidFilmer.com, Jun 10, 2005.

  1. Guest

    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?
     
    , Jun 10, 2005
    #1
    1. Advertising

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

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Jun 10, 2005
    #2
    1. Advertising

  3. John Bokma Guest

    John Bokma, Jun 10, 2005
    #3
  4. John Bokma <> 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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Jun 10, 2005
    #4
  5. 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
     
    Gunnar Hjalmarsson, Jun 10, 2005
    #5
  6. Steven Kuo Guest

    On Fri, 10 Jun 2005, Gunnar Hjalmarsson wrote:

    > 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
     
    Steven Kuo, Jun 10, 2005
    #6
  7. John Bokma Guest

    A. Sinan Unur wrote:

    > John Bokma <> 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 :-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, Jun 10, 2005
    #7
  8. John Bokma Guest

    John Bokma, Jun 10, 2005
    #8
  9. Brian Wakem Guest

    John Bokma wrote:

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



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

    Maybe

    foreach(..) {
    }
    or {
    }


    --
    Brian Wakem
     
    Brian Wakem, Jun 10, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pierre Thibault
    Replies:
    10
    Views:
    451
    Pierre Barbier de Reuille
    Jul 28, 2006
  2. Daniel Vallstrom
    Replies:
    7
    Views:
    379
    Daniel Vallstrom
    Oct 19, 2004
  3. JD
    Replies:
    5
    Views:
    258
  4. Replies:
    9
    Views:
    160
    Sam Smoot
    Aug 11, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    444
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page