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

hi,
I know on the command ln in unix I can put ls *.seq >list.txt
to get a list of all the seq files for example. Is there a command in perl so I can do the same kind of thing. ie look in the directory to find all *.seq files
ta

Replies are listed 'Best First'.
Re: *.txt
by Abigail-II (Bishop) on Sep 29, 2003 at 15:45 UTC
    while (<*.txt>) { # Do something with $_ }

    Abigail

Re: *.txt
by dragonchild (Archbishop) on Sep 29, 2003 at 15:47 UTC
    Method 1:
    foreach my $filename (<*.seq>) { print "$filename\n"; }

    Update: (as per Abigail-II's comments below)

    while (my $filename = <*.seq>) { print "$filename\n"; }

    Method 2:

    use IO::Dir; my $dh = IO::Dir->new('.'); while (defined(my $filename = $dh->read)) { next unless $filename =~ /\.seq$/; print "$filename\n"; }

    And, there's at least 3-4 others people could come up with, depending on your exact needs.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      A while loop instead of a foreach is preferred in method 1. The foreach will store all matching files in a list, which could take lots of memory if there are many files. The while method doesn't store an intermediate list. (List vs scalar context behaviour of <>).

      Abigail

Re: *.txt
by flounder99 (Friar) on Sep 29, 2003 at 15:48 UTC
    my @seqfiles = glob '*.seq';

    --

    flounder

      2 things,
      1. Are you looking to emulate the behavior of ls from perl?
      2. Or are you looking for an purely perl way of accomplishing your task

      If you're simply looking to emulate the behavior of ls, then call it from perl, by using either
      system("ls *.seq"); or backticking ie my $cmd_output = `ls *.seq`;
      If you were looking for a purely perl way to accomplish this task, then my information is of little use to you, but if you were looking to emulate the behavior of ls from perl, regardless of it being a system call or a perl function, then maybe my comments are of some use.

Re: *.txt
by vek (Prior) on Sep 29, 2003 at 17:11 UTC
    opendir (FOO, $somedir) || die "Couldn't read $somedir - $!\n"; for my $fileFound (grep /.seq$/, readdir FOO) { print $fileFound, "\n"; } closedir (FOO);
    -- vek --
Re: *.txt
by usless_blonde (Initiate) on Oct 01, 2003 at 11:24 UTC
    Thank you. I thought you'd all laugh at how clueless I am! But you are lovely perl people!!
    If anyone cares I want to open all the DNA sequence files I've genetared elsewhere and then compare them all to each other to see how closely related they are. i'll prob do this with array's haven't figured out how but am working on something else.
    thanks again!