View Full Version : how to read contents of a folder into an array?
hogtied
03-11-2003, 08:51 PM
Hey,
I was wondering if you can read the contents of a specified folder into an array? Can this be done? If so what is the procedure and or variables to achieve this.
Thanks,
Hogtied
StevenCoutts
03-12-2003, 05:00 AM
Please define "folder" so I can help:thumbsup:
mellin
03-12-2003, 08:31 AM
Like this?
-------------------------------------------------------
my $dir = "/home/hopkins";
opendir (FILEHANDLE, "$dir");
@content = grep(/^.+\.txt$/, readdir(FILEHANDLE));
closedir(FILEHANDLE);
-------------------------------------------------------
The regexp is there, because maybe you want't some control over what files are pushed to the array. Like maybe only .xml or .txt files.
hogtied
03-12-2003, 10:53 AM
sweet thanks, I looked everywhere for that info. I won't need the .txt cuz the folder contains all pictures. but it's helpful to know thanks again
mellin
03-12-2003, 11:26 AM
Glad to help.
Took me a while to learn Perl, so helping others really feels great.
MagicBobert
03-12-2003, 11:52 PM
Another useful command worth looking into would be "readdir"
hogtied
03-13-2003, 02:15 AM
yes in fact i came across the readdir() and am using that approach. I do have another question though.
I've seen some examples that show the following for arrarys.
@array = [something]
@array = (something)
$arrary
@_
$_
wondering what is the difference between the first two arrays and the $array is suppose to reteive a part of the @array? But the @_ and $_ i don't understand
You know better yet. I went to a book store to pick out a perl book or CGI Perl book and they had quite a few from 3 different authors. Which is the best one to get considering I'm still a novice in perl but not programming and will probably travel very deep into it. So I really don't want a beginners book.
StevenCoutts
03-13-2003, 02:59 AM
Perl Core Language - Little Black Book by Steven Holzner
mellin
03-13-2003, 08:46 AM
Originally posted by hogtied
yes in fact i came across the readdir() and am using that approach. I do have another question though.
I've seen some examples that show the following for arrarys.
@array = [something]
@array = (something)
$arrary
@_
$_
wondering what is the difference between the first two arrays and the $array is suppose to reteive a part of the @array? But the @_ and $_ i don't understand
Scalar "$_" is a special variable, a buiilt-in function when working with @arrays or %hashes.
When iterating over @array, that consists of normal text-file like this:
----
whatever here reads
is nothing but loads of bull****
but hey, whatever..
-----
with code like this:
-----
foreach(@thefile) {
print "$_\n";
}
-----
Every row gets printed through variable "$_".
Or maybe you have @array that consists of indexed data like this:
-----
@array = ("5", "10", "15", "20");
-----
Iterating through it like this:
-----
for (@array) {
if ($_ >= 15) {
push @larger_than, $_;
}
}
-----
Would push every index to array "@larger_than", if it's greater or same as 15.
I can't really explain it well, 'cause i suck teaching others but hey, just mess around and read Perl manuals.
If you use Linux just type "man perl", and search "special variables", it's all there.
hogtied
03-13-2003, 07:13 PM
Thanks for the reply mellin, the information you provided was helpful..
Thanks,
Hogtied
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.