By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,252 Members | 1,346 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,252 IT Pros & Developers. It's quick & easy.

Help with arrays needed

P: 7
Hello everybody ,
I have a problem with arrays ...
I need to get some data from files in some directories .

The names of the dirs are something like 12.12344 or 23.34441.
Some contains one , some 2 or 3 files .

I'd like to get data from those dirs recursively ... but dunno how .

I've tried something like :

Expand|Select|Wrap|Line Numbers
  1. @DIR=("12.12344","23.34441");
  2. @12.12344=("height","width");
  3. @23.34441=("height");
and then reading with 2 nested foreach :

Expand|Select|Wrap|Line Numbers
  1. foreach $directory (@DIR) {
  2.   foreach $element ($[@directory]){ #<- this is wrong I know , but how can i do something like this ??
1) I can't use the "." in the name of the array
2) Dunno how to use the variable name as the name of the array ...


I tried even with something like :
Expand|Select|Wrap|Line Numbers
  1. %DIR=("12.12344","height",
  2.       "23.34441","height",
  3.       "12.12344","width")
and using :
Expand|Select|Wrap|Line Numbers
  1. while (($directory, $element) = each(%DIR)){
but isn't working either , seems because the keys are repeated .

Maybe it's a stupid problem , I know , but this is my first
try in Perl :) ( Yes , I' m a total noob :) )

Someone can help ?

Thx in advance

David
Jan 2 '08 #1
Share this Question
Share on Google+
5 Replies


eWish
Expert 100+
P: 971
Welcome to TSDN!

In order to get the contents of a direcory either use a module like File::Find or you use the opendir function to open and the readdir function to read the directory.

Expand|Select|Wrap|Line Numbers
  1. opendir(my $SOMEDIR, $directory_name)  || die "Can't open dir : $!\n";
  2. my @dir_list = readdir $SOMEDIR;
  3. closedir $SOMEDIR;
  4.  
  5.        #Get a list of the files and print them excluding the ".." & "." 
  6.  
  7. foreach  my $file (@dir_list) {
  8.     next if $file eq '.'  || 
  9.             $file eq '..' || 
  10.  
  11.     my ($name, $ext) = split(/\./, $file); 
  12.  
  13.     print $name, "\n";
  14. }
--Kevin

*Cross my fingers that this is not Homework or Schoolwork.*
Jan 2 '08 #2

P: 7
Many Thx Kevin ,
I'll use those functions .
But this doesn't solve my problem ...
In those dirs there are many files , and not always I want to read some ...
For example , sometimes the content of "height" and "width" are fakes , with some number very big or very little but sometimes they have reasonable valors too , so they can actually trick the script .

What i'd need ( I think ) is something like an array withh the values I need for each file in each dir ...

Something like
Expand|Select|Wrap|Line Numbers
  1. @DIRECTORIES=("DIR1","DIR2","DIR3");
  2. @DIR1=( "width", "height" ) ;
  3. @DIR2=("width");
  4. @DIR3("height");
  5.  
Is there a way to access recursively those 2 arrays ?

Dunno if I have explained it well ...


Thx

David
Jan 2 '08 #3

eWish
Expert 100+
P: 971
Yes, you can put them in a loop and then do what you wish with them. I am not real sure what you are trying to achieve.

Expand|Select|Wrap|Line Numbers
  1. foreach (@array) {
  2.     ...do something here....
  3. }
Without knowing what you are trying to achieve it is hard to give recommendations. I would consider using a hash to store the data. This is an example of using the directory names as keys and the files as the values.

Expand|Select|Wrap|Line Numbers
  1. use Data::Dumper;
  2. use File::Basename;
  3. use File::Find::Wanted;
  4.  
  5. my $path = 'path/to/your/dir';
  6. my @files = find_wanted( sub { -f }, $path ) ;
  7.  
  8. my %dir_hash =();
  9.  
  10. foreach my $file (@files) {
  11.      my $name = basename($file);
  12.       my $dir_name = dirname($file);
  13.          $dir_name =~ s/^.*\///g;
  14.  
  15.       push(@{$dir_hash{$dir_name}}, $name);
  16.  
  17. }
  18.  
  19. print Dumper %dir_hash;
--Keivn
Jan 3 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
File::Find may not be a good choice for this task since he seems to know the names of the directories and the files. After that I can't tell what it is he is trying to do.
Jan 3 '08 #5

P: 7
Thx all guys ...
Finally I achieved it . Sorry If I was unable to explain it well , but I'm not a native english speaker :)
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl 
  2.  
  3. @Set_1=("height","width");
  4. @Set_2=("height");
  5.  
  6. %DIRS=(    "26.7CA662000000" , "Set_1" ,
  7.         "26.9A9680000000" , "Set_2");
  8.  
  9.  
  10. while (($To_Read_Dir, $Set) = each(%DIRS))
  11. {
  12.     if ( $Set eq Set_1 ) {
  13.         foreach $Feature (@Set_1){
  14.         do stuff ...
  15.         }
  16.     }
  17.     if  ($Set eq Set_2 ) {
  18.         foreach $Feature (@Set_2){
  19.         do stuff ...
  20.         }
  21.     }
  22. }
  23.  
  24.  
It's a correct thing to do ?
Another question , do I need to open each dir to open the file inside ? Or I can open directly the file ?


Thx again

David
Jan 5 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.