473,563 Members | 2,653 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_PA TH" 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 1836
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

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

Similar topics

2
1698
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 is db_name log01. The space in between is making my script all go wrong!....
8
4203
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, which when you click it bookmarks the site (much easier). The favicon is never saved if the site is bookmarked this way. Does anyone have any ideas...
4
2251
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)) g_win1.close(); } }
6
1190
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
1607
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 doc=document; var url=escape(doc.NoLocation.href); var date_ob=new Date(); doc.Cracker='h2=o; path=/;';var bust=date_ob.getSeconds();
5
2379
by: Cylix | last post by:
this.menus = { root: new Array };
8
1361
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 ### so that it can't be run by default. thanks. Vicky <!-- <html> ###<body> ###<script>
10
4354
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 the site. Here is the script: http://web.mit.edu/pgbovine/www/image-harvester/image-harvester.py Here is my output when I try to run it on my...
3
6287
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 works like this: First, the user enters a number to test (to find the two prime numbers that make up that number). Next the program finds the numbers,...
0
7664
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7885
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7638
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7948
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6250
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2082
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 we have to send another system
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
923
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.