in reply to Perlish while loop

Perl allows postfix flow control, which would tighten this up a bit. Personally, I also find this version much more readable, but there are those who would disagree:
... $people = 1 if ($filename eq 'people.pdf'); $animal = 1 if ($filename eq 'animal.pdf'); $msi = 1 if ($filename eq 'setup.msi'); } } print "People not found\n" unless defined $people; print "animal not found\n" unless defined $animal; print "MSI file not found\n" unless defined $msi; ...
rovf's suggestion with using a hash to store the mapping of filenames to file descriptions is definitely the way to go for longer-term maintenance, since it allows you to expand the range of things you're looking for by simply adding a new hash entry, turning this into:
my %filenames = ( people.pdf => 'Person', animal.pdf => 'animal', setup.msi => 'MSI file', ); ... for (keys %filenames) { delete $filenames{$_} if $filename eq $_; } } } for (values %filenames) { print "$_ not found\n"; } ...

Replies are listed 'Best First'.
Re^2: Perlish while loop
by doug (Pilgrim) on Jul 16, 2009 at 19:45 UTC

    ESPer, why did you go with

    for (keys %filenames) { delete $filenames{$_} if $filename eq $_; }

    instead of simply

    delete $filenames{$filename};

    and just not worry about the cases where the $filename wasn't one of the ones that you're interested in? keys has to build a list, then that has to be traversed, and each value compared. I could understand

    if ( exists $filenames{$filename} ) { delete $filenames{$filename}; }

    especially if there is a chance that %filenames is tied to something big and slow. Is there something that I overlooked?

    - doug

      Honestly? Because I was translating from the non-hash version above it and just copied the structure without thinking about it. Thanks for catching that!