Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Adding Files in a directory to an array

by Gerard (Pilgrim)
on Oct 09, 2001 at 12:56 UTC ( #117669=perlquestion: print w/ replies, xml ) Need Help??
Gerard has asked for the wisdom of the Perl Monks concerning the following question:

Hello All, I very new to all this so please bear with me. (This is also my first post). I am trying to add files from a directory to an array, which I learnt last night from reading on this site that I could do by @allcontacts = <*.cd>; That was fantastic and I also learnt that I could use something like @allcontacts = <a_directory/*.cd>; However I now want to use a variable to in place of the  a_directory/*.cd I tried doing this with the following code...
#set the initial values for the text boxes to the first contact's det +ails #cd32/ is the directory $searchcontacts = "cd32/*.cd"; @allcontacts = <$searchcontacts>; $count = "0"; #display the first files contents first open(DAT, $allcontacts[$count]) || die("Could not open file!"); @allcontactdetails=<DAT>; close(DAT);
But received the message unable to open the file. As it worked fine before I put the variable in I know it has something to do with this. I have tried escaping the characters but I am obviously doing something wrong. I am sure there is a simple answer, but I have looked for ages, and just feel really stupid. I would greatly appreciate it if some wise monk would impart some of their wisdom to me. Cheers, Gerard.

Comment on Adding Files in a directory to an array
Select or Download Code
Re: Adding Files in a directory to an array
by Caillte (Friar) on Oct 09, 2001 at 13:18 UTC

    Here are a couple of different ways....

    my $x = '~/tmp/*'; my @list = glob($x); print join '-', @list; $x = '/home/caillte/tmp/'; opendir(X, $x) or die "$!"; @list = readdir(X); closedir(X); print join '-', @list;

    How they differ..... Glob uses a shell to expand the argument... therefore you can specift the argument in the same way you would on the command line. Opendir, readdir and closedir treats the directory as if it were a file, giving you faster access, on general, than glob() and more flexibility. However, glob() works exactly like your shell does, giving you access to special characters like '~'.

    $japh->{'Caillte'} = $me;

Re: Adding Files in a directory to an array
by davorg (Chancellor) on Oct 09, 2001 at 13:21 UTC

    Yeah, you can't do that, because Perl gets confused between the globbing operator (<*.txt>) and the file input operator (<FILE>).

    The way to do it it to use the glob function instead.

    $searchcontacts = "cd32/*.cd"; @allcontacts = glob($searchcontacts);
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

      Not exactly.

      While it is better (and I mean really, really better) to use the functional glob, you still can force perl to interpolate variables. Actually, only filehandles and simple scalars are not interpolated (see perlop). So these:

      @dir=<$hash{$key}>; @dir=<$array[0]>; @dir=<$object->method>; @dir=<${foo}>; # and even @dir=<$structure{'filehandle'}>;
      are interpreted as globs. Note that this is a caveat in the last case! Since you probably want the <> to do a readline in the last case, people really need to be aware that nested structures are always globbed. Only simple, straightforward variables like $foo result in readlines.

      Jeroen
      "We are not alone"(FZ)

Re: Adding Files in a directory to an array
by busunsl (Vicar) on Oct 09, 2001 at 13:25 UTC
    The problem with globs (that cd32/*.cd thingy) is, that you can't use them in variables.

    You can use opendir/readdir/closedir to scan a directory.
    If you have to traverse down into subdirectories use File::Find.

      Thanks all for your great advice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://117669]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-09-30 11:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (363 votes), past polls