Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: arrays within arrays?

by hdp (Beadle)
on Apr 26, 2001 at 02:42 UTC ( [id://75651] : note . print w/replies, xml ) Need Help??

in reply to arrays within arrays?

There are two really big red flags here (thanks Dominus). First, you have a group of variables that are all named similarly; you should be using an array instead, e.g. @dir_vn (which would then have 6 elements). If they were named similarly but were distinguished by some "property name" (instead of a number, as here) you would probably want a hash; for example, $animal_name, $animal_color, $animal_food becomes %animal with name, color, and food as keys. Second, you're repeating essentially the same block of code six times! Ouch. Clearly you need some sort of loop. Happily, you recognized that something's not quite right here.

It turns out that you don't even need the array at all; the only place it ends up being used is in the loop control statement. See below.

#!/usr/local/bin/perl my $dir_split = '/opt/stats/data/split'; for $dir ($dir_split, map { $dir_split . "/vn$_" } 1..6) { opendir DIR, $dir or die $!; my @files = grep -f "$dir/$_", readdir DIR; # the chomp was unnecessary closedir DIR; chdir($dir); unlink @files or die $!; }

Replies are listed 'Best First'.
Re: Re: arrays within arrays?
by trowa (Acolyte) on Apr 26, 2001 at 03:49 UTC
    Yes, that is awesome... I would never have come up with something that concise. on a side note, i noticed that when the last line is changed from:
    unlink @files or die $!;

    unlink @files || die $!;
    it runs without any errors, but does not delete anything. what is the difference between or and ||? or, is it all in my imaginition? Much Thanks,


    "In God we trust, all others bring data."
      See perldoc perlop. || has a rather high precedence. or is lower. When using the former, it's as if you'd written unlink(@files || die $!) which will never die as long as @files is true.