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

What is wrong in this script ??? help plz!!!!

P: 65
Hi All,

Thanks in Advance !!!!

I have one config file & am reading that file.later i am using one of the value specified in the config file & trying to get the files placed in the directory into the array.

problem is when i try to print the array varaible it doesn't hold any value even though i have placed some files under searching directory...

don't know where it's going wrong ??? can any one help me on this ???

script goes like this :
Expand|Select|Wrap|Line Numbers
  1. # parse the configuration text file entries into PARAMS associative array.
  2. open (CONFIGFILE, "$config_file") or die;
  3.  
  4. while (<CONFIGFILE>) {    
  5.     chomp;
  6.     if($_!~ /QUIT_TXT/ ){
  7.  
  8.                         if ( ! ( $_ =~ /^#/ )) {
  9.                $CONFIG = $CONFIG . $_ . " ";
  10.  
  11.         }
  12.     }
  13.     else{
  14.         last;        
  15.         }
  16.  
  17.     }
  18. close (CONFIGFILE);
  19.  
  20. %PARAMS = split(/\s+/, $CONFIG);
  21. #######################################
  22. # Build an array of all the input files
  23. my @files = `dir /B $PARAMS{INPUT_FILES_PATH}`;
  24. print "@files\n";
  25.  
Note : i have mention the directory path in "INPUT_FILES_PATH" under configfile.

i doubt on this code :
Expand|Select|Wrap|Line Numbers
  1. my @files = `dir /B $PARAMS{INPUT_FILES_PATH}`;
  2.  
Is this correct way to fetch the directory info ???
i have used GLOB also but no use !!!!

Regards,
Vijayarl
Oct 9 '08 #1
Share this Question
Share on Google+
12 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
The code you have posted will definitely not do what you are expecting. The %PARAMS is a hash (as you have written it) and not an array. You really need to look up a tutorial on how to define a hash and to read them. A has is different from an array in that it has a key=>value pair. You should look up a tutorial on using them and then try to re-write this code.

*Hint: When populating the hash, you will want to put the filehandle into something like a while loop, otherwise you will only get the first line of the file.

Regards,

Jeff
Oct 9 '08 #2

P: 65
Thanks Jeff for your patience reply...

let me tell you, this script works well when i run this script using Eclipse tool..
but the problem is when i try to run the same script through cmd line, i won't get the desired result.

Anyway's thanks for your valuable input on hash...for sure will go through that & get back to you.

Regards,
Vijayarl
Oct 9 '08 #3

Icecrack
Expert 100+
P: 174
try replacing the array @files

Expand|Select|Wrap|Line Numbers
  1. my @files = `dir /B $PARAMS{INPUT_FILES_PATH}`;
with:

Expand|Select|Wrap|Line Numbers
  1. opendir(DIR, $PARAMS{INPUT_FILES_PATH}) || die "can't opendir $PARAMS{INPUT_FILES_PATH}: $! \n";
  2. @files=readdir(DIR);
  3. closedir DIR;

Also as numberwhun said take a good look in hashes if you are going to use them,
Oct 9 '08 #4

P: 65
Thanks Icecrack for your reply...

sure i will go through hash concept then later try to use it in the scripts..

now i did use your idea but the problem is after reading the contents in @ files
Expand|Select|Wrap|Line Numbers
  1. @files=readdir(DIR);
  2.  
The @ files contains . .. as there first 2 elements (guess it's reading all the contents including all the hidden files)

now my script fails saying
Expand|Select|Wrap|Line Numbers
  1. Died at process_stats_iconprod.pl line 186.
  2.  
line 186 contains :
Expand|Select|Wrap|Line Numbers
  1. open (TXTFILE, "$file") or die;
  2.  
all i need now is how to skip reading first 2 array elements of the @files as it contains only . .. (dot & dot dot) then filenames...

Script goes like this :
Expand|Select|Wrap|Line Numbers
  1.  
  2. opendir(DIR, $PARAMS{INPUT_FILES_PATH}) || die "can't opendir $PARAMS{INPUT_FILES_PATH}: $! \n"; 
  3. @files=readdir(DIR); 
  4. closedir DIR;
  5. #print "@files\n";
  6.    foreach $file_name_token (@files){
  7.     chomp ($file_name_token);
  8.     $file_name = $file_name_token;
  9.     my $file = "";
  10.     $file = $PARAMS{INPUT_FILE_BASE_PATH} . $file_name;
  11.                open (TXTFILE, "$file") or die;
  12.  
  13.                 blahh blahh
  14.  
  15.           }
  16.  
  17.  

Regards,
Vijayarl
Oct 10 '08 #5

numberwhun
Expert Mod 2.5K+
P: 3,503
Ok, feel free to use this code how you like, but here is a script that will populate an array with everything in a directory, except the "." and ".." files:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my @tmparray;
  7. my @files;
  8. my $line;
  9.  
  10. opendir(DIR, "/my/dir/") or die "Cannot open /my/dir/\n");
  11. @tmparray = readdir(DIR);
  12.  
  13. foreach(@tmparray){
  14.     $line = $_;
  15.     if($line !~ /^\.{1,2}$/){
  16.         push(@files, $line);
  17.     }
  18. }
  19.  
  20. # To test that it worked
  21. foreach(@files){
  22.     print("$_\n");
  23. }
  24.  
I tested this on my Gentoo 2008 machine that is sitting next to me using Perl 5.8.8 and it works fine. Incorporate it as you choose.

Regards,

Jeff
Oct 10 '08 #6

P: 65
Thanks jeff for your kind reply....

guess what... i did understand of your script expect for this line

Expand|Select|Wrap|Line Numbers
  1.  
  2. if($line !~ /^\.{1,2}$/){ 
  3.         push(@files, $line); 
  4.     } 
  5.  
  6.  
hope u won't mind explaining me this ???

All that i can guess is that you are comparing & skipping 1 & 2 lines & then push everthing to @files... is that so !!!!!

Thanks again Jeff !!!!!

Thanks,
Vijayarl
Oct 10 '08 #7

numberwhun
Expert Mod 2.5K+
P: 3,503
Thanks jeff for your kind reply....

guess what... i did understand of your script expect for this line

Expand|Select|Wrap|Line Numbers
  1.  
  2. if($line !~ /^\.{1,2}$/){ 
  3.         push(@files, $line); 
  4.     } 
  5.  
  6.  
hope u won't mind explaining me this ???

All that i can guess is that you are comparing & skipping 1 & 2 lines & then push everthing to @files... is that so !!!!!

Thanks again Jeff !!!!!

Thanks,
Vijayarl
Actually, that is a regular expression. What I did was top do a foreach loop to cycle through the @tmparray array, element by element. The loop first assigns the current element to $line. Then, the if loop compares $line against the regular expression. If you are not up on regular expressions, then you really need to put a little time in on them as they are a critical part of perl and more powerful in Perl than any other language.

Here is the regex, written slightly different, yet still valid:

Expand|Select|Wrap|Line Numbers
  1. /          #  This begins the regex
  2. ^          #  This says to match from the beginning of the line
  3. \.         #  This matches a "."  The \ escapes its special nature in regex's
  4. {1,2}     # This is a modifier to the previous \., telling it to match once but up to twice in a row
  5. $         #  This says that after that match should be the end of line
  6. /          #  This ends the regex.
  7.  
Essentially, this will match a "." and "..", and that is it. But, because of this (!~), it will only enter the loop and print $line to file if the match does not occur.

I hope this helps.

Regards,

Jeff
Oct 10 '08 #8

KevinADC
Expert 2.5K+
P: 4,059
I tested this on my Gentoo 2008 machine that is sitting next to me.....

Jeff
You have a machine sitting next to you? Sould I be jealous or scared?
Oct 10 '08 #9

numberwhun
Expert Mod 2.5K+
P: 3,503
You have a machine sitting next to you? Sould I be jealous or scared?
Yup, here at work I have my typical Window$ laptop, and then I have a second laptop that I installed Gentoo 2008.0 on and am still configuring. If you think that's nice, I should list what I have at home. :-)

Regards,

Jeff
Oct 10 '08 #10

P: 65
Thanks Jeff... it was a indeed good explaination to an niewbiee to perl...

Thank you very much for kind reply....

Good Job please keep it going .it helps all newbiee's..

Ahh there comes our another perl guru (KevinADC)... :-)

Thanks for both & to other who are keeping helping the newbiee's

really makes us to learn things faster...

Regards,
Vijayarl
Oct 10 '08 #11

KevinADC
Expert 2.5K+
P: 4,059
Thanks Jeff... it was a indeed good explaination to an niewbiee to perl...

Thank you very much for kind reply....

Good Job please keep it going .it helps all newbiee's..

Ahh there comes our another perl guru (KevinADC)... :-)

Thanks for both & to other who are keeping helping the newbiee's

really makes us to learn things faster...

Regards,
Vijayarl

hehehe..... I had to wipe my face off after reading that much slobbery praise. But I do appreciate your kind words. Nice to know we help sometimes.

Regards,
Kevin
Oct 10 '08 #12

KevinADC
Expert 2.5K+
P: 4,059
Yup, here at work I have my typical Window$ laptop, and then I have a second laptop that I installed Gentoo 2008.0 on and am still configuring. If you think that's nice, I should list what I have at home. :-)

Regards,

Jeff
hehehe..... I'm already jealous, no need to pile it on, anymore and I'll get depressed.

I work at home so I can hardly brag about what I have sitting next to me. An old computer with my dirty laundry covering it. :(
Oct 10 '08 #13

Post your reply

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