LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-24-2013, 08:41 PM   #1
hcbj44
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Rep: Reputation: Disabled
Perl 5.10 on RHEL 6.2: The "for" loop code is not executing within my Perl Script


Simply, I wrote a little Perl script that will have an outer loop (the "for" loop) to process all of the directories with a similiar name of "/home/customer*/input" and an inner loop (the "foreach" loop) that will process all files found within each directory of "/home/customer*/input".

When I execute the script, it does not fail but I do not receive my print command output (a checking mechanism). When I modify the script to remove the outer loop (the "for" loop) everything works correctly.

Here is the code:

#!/usr/bin/perl

use strict;
use warnings;
use English;
use File::Basename;
use File::Copy;

# VARIABLES

my $AIWBASE = "/aiw/aiw1";
my $totcus = `ls -d /home/customer* | wc -l`;
my $customer = 1;

# MAINLINE

for (my $customer=1; $customer > $totcus; $customer++) {
my $SaaS_Customer_Input_Data = "/home/customer$customer/input";
my $SaaS_Customer_Output_Data = "/home/customer$customer/output";
opendir(INPUTDIR, $SaaS_Customer_Input_Data) or die "Couldn't open '$SaaS_Customer_Input_Data': $!";
opendir(OUTPUTDIR, $SaaS_Customer_Output_Data) or die "Couldn't open '$SaaS_Customer_Output_Data': $!";
my @customer_files = grep {!/^\./ && -f "$SaaS_Customer_Input_Data/$_"} readdir(INPUTDIR);
foreach my $customer_file (@customer_files) {
print "total customers: $totcus\n";
print "Customer Number: $customer\n";
print "customer file: $customer_file\n";
}
closedir(INPUTDIR);
closedir(OUTPUTDIR);
}
exit 0;


Perhaps someone can point-out my failure here!
 
Old 01-24-2013, 08:50 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650
Would you mind adding code tags? .. it's a bit hard to read, thanks

Are you sure this is correct?:
Code:
for (my $customer=1; $customer > $totcus; $customer++) {

Last edited by kbp; 01-24-2013 at 08:54 PM.
 
Old 01-24-2013, 09:06 PM   #3
hcbj44
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
What are code tags, I'm sorry? Well, I think that the "for" loop code is correct but it is why I have posted the question for assistance.
 
Old 01-24-2013, 09:29 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
He is talking about putting code tags around your listing to make it easier to read.

I don't think readdir does what you think it does.

It opens a directory and gives a list of file names. It does not do subdirectory processing.

If a directory listing looks like
Code:
$ ls -r xyz
xyz/a
xyz/a/level
xyz/a/another
xyz/b
xyz
Then
Code:
opendir(INPUTDIR,"xyz");   # I'll assume it always opens
@list = readdir(INPUTDIR)
Will give you the array list with:
Code:
.
..
a
b
Your grep line appears to be looking for "xyz/a", which won't be there.

The result should be an empty list (@customer_files will have nothing in it), and the script then exits.

No output.

Last edited by jpollard; 01-24-2013 at 09:30 PM.
 
Old 01-24-2013, 09:36 PM   #5
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650
For loops are usually in the format
Code:
for (<initialise>; <test>; <increment>)
.. where the test needs to return true to execute the body.
Yours initialises a value to 1, tests whether the value is greater than $totcus and then skips executing the loop contents because it isn't.

I believe what you want is:
Code:
for (my $customer=1; $customer <= $totcus; $customer++) {
 
1 members found this post helpful.
Old 01-25-2013, 12:26 AM   #6
hcbj44
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Great post sir or madam! It worked beautifully! It wasn't so intuitive at the time regarding the second parameter of the "for" loop command but now it is so! Thanks a trillion!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
#In this perl code, im getting the input as "ip address" from the user and pinging it dawood munavar Linux - Newbie 1 04-07-2011 10:42 PM
Use of "Command line perl" in perl script using system command. aditya007 Linux - Newbie 4 11-29-2009 11:08 PM
"Not a CODE" reference error perl v5.10.0, Deprecated assignment operation. skuletm Programming 3 03-12-2009 01:21 AM
please help me with this perl code getting error "use of uninitialized value" haxpak Programming 10 03-01-2009 11:55 PM
Compiling KDE 3.2.0 - "Can't open perl script "/dcopidlng/kalyptus" linuxgamer09483 Linux - Software 3 04-09-2004 09:25 PM


All times are GMT -5. The time now is 08:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration