473,499 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

65 New Member
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
12 1826
numberwhun
3,509 Recognized Expert Moderator Specialist
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
vijayarl
65 New Member
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
174 Recognized Expert New Member
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
vijayarl
65 New Member
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
3,509 Recognized Expert Moderator Specialist
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
vijayarl
65 New Member
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
3,509 Recognized Expert Moderator Specialist
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
4,059 Recognized Expert Specialist
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
3,509 Recognized Expert Moderator Specialist
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
vijayarl
65 New Member
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
4,059 Recognized Expert Specialist
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
4,059 Recognized Expert Specialist
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

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

Similar topics

2
1690
by: Sparrow | last post by:
I need to get a print return of db_namelog01 for the script I have written below: select @cmd=@db_name+'to dump'+@db_name+'log'+right(stuff(convert(char(2),@log_number),1,0,'0'),2) What I get...
8
4191
by: Johnny Knoxville | last post by:
I've added a favicon to my site (http://lazyape.filetap.com/) which works fine if you add the site to favourites the normal way, but I have some JavaScript code on a couple of pages with a link,...
4
2240
by: Hal | last post by:
Can someone please tell me what is wrong with this script? <script language="JavaScript"> <!-- function HF_CloseAllChildren() { if (g_win1) != undefined { if ((g_win1) && !(g_win1.closed))...
6
1182
by: Wow | last post by:
var abc=64; document.write(abc^2); then it display 66 var abc=66; document.write(abc^2); then it displays 64 What is the ^2 ? 66^2=4365 not 64. even I mod 128 or 256, I got 4, not 64
2
1599
by: abuse | last post by:
this script is on a web page i visit i am not a java or any other code programmer. is this malicious? should i be leary of this site? thanks. <script language="ShonenScript 712.0"><!-- {var...
5
2370
by: Cylix | last post by:
this.menus = { root: new Array };
8
1354
by: VickyVirgo2k | last post by:
Could someone please give me some idea what this script is doing. It might be some malicious script that might have been used to spread virus or to hack username/ password, hence it has been ###...
10
4346
by: Ohmster | last post by:
I am trying to use this cool script that some MIT guy wrote and it just does not work, I get a stream of errors when I try to run it. It is supposed to visit a URL and snag all of the pictures on...
3
6271
by: sigkill9 | last post by:
I've been working on this script all weekend and am almost done with it accept for one last detail; implementing a "process another number?" prompt after a user input is processed. The script...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7009
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.