On May 25, Tielman Koekemoer (TNE) said:

Thanks for the help. So I cannot use an array or hash in angle
brackets as file handle. I have a variable amount of files which I'd
like opened and the contents shuffled. What would be the best way to
do this as my next idea also did not work?

@files=`ls /app2/koekemtn/scripts/dbstats/test`;

First of all, you need to chomp() this array, since all its elements end in newlines. Second of all, you should not be using `ls ...` for this, because Perl offers you opendir(), readdir(), and closedir(). Or, if you're a little lazy, glob().

  my $dbpath = '/app2/koekemtn/scripts/dbstats/test';
  opendir my($dbdir), $dbpath or die "can't readdir $dbpath: $!";
  my @dbfiles = readdir $dbdir;
  closedir $dbdir;

Now you have all the file *names* in an array. Since they're in another directory entirely, you'll have to put their path in front of them:

  # use this line instead of the readdir() line above
  my @dbfiles = map "$dbpath/$_", readdir $dbdir;

Finally, you probably only want files, not directories. Your `ls ...` code didn't discriminate, but readdir() returns ALL entries, including '.' and '..', so we do have to be specific:

  # use this
  my @dbfiles = grep -f, map "$dbpath/$_", readdir $dbdir;

(At this point, if you don't understand anything I've coded, I'd suggest you look at 'perldoc -f opendir', 'perldoc -f readdir', 'perldoc -f closedir', 'perldoc -f map', 'perldoc -f grep', and 'perldoc -f -X'. Or you could ask more here, but it would help those of us that answer questions a great deal if you read the docs first.)

$num =1;

foreach $line ( @files ) {
        chomp $line;

Eh, you chomp() now.  But I wouldn't have used `ls ...`.

        open ( $line , "$line") || die "Cannot open $files[0]\n";

You've got $files[0] there, but you're not always using $files[0], you're using $line. You also didn't include $! in your error message, which would tell you WHY it couldn't open the file.

        $file$num = $line;   # Problem assignment!

Whenever you see that you have a group of variables called $this_1, $this_2, $this_3, etc., it's a sign that you should be using an array. In fact, you already HAVE the array (@files) since you're using the elements of the array both as paths to files AND as the filehandles themselves. It's a little unorthodox, but it's not a crime. I wouldn't do it, though, I'd create filehandles:

  my @fh;

  for (@dbfiles) {
    open my($f), "< $_" or die "can't read $_: $!";
    push @fh, $f;
  }

Now for every element in @dbfiles, the matching element in @fh is a filehandle for it.

--
Jeff "japhy" Pinyan         %  How can we ever be the sold short or
RPI Acacia Brother #734     %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %    -- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to