Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyboady, I'm new in Perl, using it on unix. I'm trying to read many files each named by a "string"+a number+"string". Only the number is changing for each filename, like: loyl1i_lgr.sss , loyl2i_lgr.sss, ... Then to alocate read file to a new name to use it in a repeat section by using each one in a commands code. Many thanks to your ASAP answers. Asaad

Replies are listed 'Best First'.
Re: read an array of file
by davorg (Chancellor) on Dec 30, 2002 at 13:12 UTC

    For example:

    foreach my $number (1 .. 10) { my $file = "loyl1${number}_lgr.sss"; # do something with $file }
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Thanks Guys, esp. davorg, It did work. I fixed the problem.
Re: read an array of file
by vek (Prior) on Dec 30, 2002 at 13:58 UTC
    If all the files end with the .sss extension then you could also do this:
    my $path = "/path-to/some-dir"; opendir(DIR, $path) || die "Could not read $path - $!\n"; for my $fileFound (grep /\.sss$/, readdir(DIR)) { # do stuff here... }
    Or do the same thing if all the files begin with 'loy':
    my $path = "/path-to/some-dir"; opendir(DIR, $path) || die "Could not read $path - $!\n"; for my $fileFound (grep /^loy/, readdir(DIR)) { # do stuff here... }
    -- vek --

      And to complete the original request :

      my $path = "/path-to/some-dir"; opendir(DIR, $path) || die "Could not read $path - $!\n"; for my $fileFound (grep /^loyl\d+i_lgr\.sss$/, readdir(DIR)) { # do stuff here... }
      or
      my $path = "/path-to/some-dir"; opendir(DIR, $path) || die "Could not read $path - $!\n"; while (my $fileFound = readdir(DIR)) { next unless $fileFound =~ /^loyl\d+i_lgr\.sss$/ # do stuff here... }

      The second is better for large directories.

      Jenda