CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Problem in for loop

 



StarkRavingCalm
User

Jul 19, 2013, 12:22 PM

Post #1 of 15 (578 views)
Problem in for loop Can't Post

I have a script to check for certain file extensions and take actions on them. the filename itself will be the same but the extension will dictate what happens to the file.
That all works great as long as there is one filename to contest with. If there is more than one it loops thru and does the action equal to the number of filenames found.
I know, I know, thats what a for loop DOES..... Wink
But is there something I can change in the for loop so it will only perform the action one time per file instead of each file 6 (or whatever ) times? Or could it even just be my number of brackets causing the issue?


The script is really long and does a lot. So I am only posting the for loop for now:


Code
my @droppedFileNameArray; 

foreach my $key (sort keys %checkfile)


{ my $droppedfilename = fileparse($key, qr/\Q.check\E/);
print "$droppedfilename \n";
push(@droppedFileNameArray, $droppedfilename);


#}

my $file = $sftp->ls('/tmp/', wanted => qr/$droppedfilename/, wanted => qr/.doc/, no_wanted => qr/^\./ );
my %file = map { $_->{filename} => $_->{a}->size } @$file;

print Dumper \%file;
$logContents .= Dumper \%file;

$sftp->remove("/tmp/$droppedfilename.check");


}


BillKSmith
Veteran

Jul 19, 2013, 1:06 PM

Post #2 of 15 (573 views)
Re: [StarkRavingCalm] Problem in for loop [In reply to] Can't Post

There is nothing wrong with the for loop you posted. The unbalanced brace at the end of your post suggests that this code is inside another loop. If so, this scrap of code could be running multiple times. Please try to post code that demonstrates your problem!
Good Luck,
Bill


StarkRavingCalm
User

Jul 19, 2013, 1:15 PM

Post #3 of 15 (570 views)
Re: [BillKSmith] Problem in for loop [In reply to] Can't Post

no loop before or after.
After that block of code, I write my logs and exit.
Before that block of code, I connect to the ftp server and do an ls to build the %checkfile hash. That's it.


Laurent_R
Enthusiast / Moderator

Jul 20, 2013, 2:40 AM

Post #4 of 15 (563 views)
Re: [BillKSmith] Problem in for loop [In reply to] Can't Post


In Reply To
The unbalanced brace at the end of your post suggests that this code is inside another loop.


It seems to me that the closing brace at the end is not unbalanced and is closing the foreach loop (since the other closing curly brace is commented out).

(Unless I missed another closing brace somewhere.)


StarkRavingCalm
User

Jul 22, 2013, 7:01 AM

Post #5 of 15 (545 views)
Re: [Laurent_R] Problem in for loop [In reply to] Can't Post

So what can be done to have to perform those actions once and not go thru each action equal to the number of files it finds?


BillKSmith
Veteran

Jul 22, 2013, 7:19 AM

Post #6 of 15 (544 views)
Re: [StarkRavingCalm] Problem in for loop [In reply to] Can't Post

Sorry about my previous comment, but I still do not see anything wrong. I would like to read the documentation for modules you are using. Would you please post all the use... statements from your program.
Good Luck,
Bill


StarkRavingCalm
User

Jul 22, 2013, 7:25 AM

Post #7 of 15 (543 views)
Re: [BillKSmith] Problem in for loop [In reply to] Can't Post

No problem.

So are you saying that given the for loop I posted, if there are 6 files, it should only make one pass on them and complete all the actions?


BillKSmith
Veteran

Jul 22, 2013, 8:06 AM

Post #8 of 15 (541 views)
Re: [StarkRavingCalm] Problem in for loop [In reply to] Can't Post

Yes, that is what I think, but I will not say so with certanity until I have read the documents that I asked for.
Good Luck,
Bill


StarkRavingCalm
User

Jul 22, 2013, 8:19 AM

Post #9 of 15 (540 views)
Re: [BillKSmith] Problem in for loop [In reply to] Can't Post

Here you go:

use warnings;
use strict;
use Net::SFTP::Foreign;
use Net::SFTP::Foreign::Compat;
use Net::SFTP::Foreign::Attributes;
use Net::SMTP;
use Data::Dumper;
use IO::Capture::Stdout;
use File::Copy;
use File::Basename;
use File::Temp;
use POSIX qw(strftime);


BillKSmith
Veteran

Jul 22, 2013, 8:35 PM

Post #10 of 15 (527 views)
Re: [StarkRavingCalm] Problem in for loop [In reply to] Can't Post

I still do not see a problem. You have several print statements in the loop. Do you get far more output than expected? What does it tell you? Add some debug code. Perhaps print "start of loop\n" at the beginning of the loop and print "end of loop\n" at the end.
Run the program and count those messages. Verify that the ouput between each pair makes sense.
Good Luck,
Bill


StarkRavingCalm
User

Jul 23, 2013, 10:26 AM

Post #11 of 15 (516 views)
Re: [BillKSmith] Problem in for loop [In reply to] Can't Post

So you are saying that if the ls finds 6 files, it should go thru and delete:
file1
file2
file3
etc...
and end the loop?

instead of delete
file1
file2
file3
etc...
start at beginning of loop
repeat 5 more times?


BillKSmith
Veteran

Jul 24, 2013, 5:13 AM

Post #12 of 15 (500 views)
Re: [StarkRavingCalm] Problem in for loop [In reply to] Can't Post

I do not see anything wrong with the code that you posted. Please explain how you know it is wrong.
Good Luck,
Bill


StarkRavingCalm
User

Jul 24, 2013, 7:27 AM

Post #13 of 15 (495 views)
Re: [BillKSmith] Problem in for loop [In reply to] Can't Post

because I get results equal to the number of files found:

Example:
$VAR1 = {
'DailyFile1.check' => 0,
'DailyFile3.check' => 0,
'DailyFile6.check' => 0,
'DailyFile4.check' => 0,
'DailyFile2.check' => 0,
'DailyFile5.check' => 0
};

We found .check files, going to sleep now....

DailyFile1
$VAR1 = {
'DailyFile5.doc' => 102,
'DailyFile2.doc' => 48,
'DailyFile6.doc' => 114,
'DailyFile1.doc' => 20,
'DailyFile3.doc' => 83,
'DailyFile4.doc' => 91
};
DailyFile2
$VAR1 = {
'DailyFile5.doc' => 102,
'DailyFile2.doc' => 48,
'DailyFile6.doc' => 114,
'DailyFile1.doc' => 20,
'DailyFile3.doc' => 83,
'DailyFile4.doc' => 91
};
DailyFile3
$VAR1 = {
'DailyFile5.doc' => 102,
'DailyFile2.doc' => 48,
'DailyFile6.doc' => 114,
'DailyFile1.doc' => 20,
'DailyFile3.doc' => 83,
'DailyFile4.doc' => 91
};
DailyFile4
$VAR1 = {
'DailyFile5.doc' => 102,
'DailyFile2.doc' => 48,
'DailyFile6.doc' => 114,
'DailyFile1.doc' => 20,
'DailyFile3.doc' => 83,
'DailyFile4.doc' => 91
};
DailyFile5
$VAR1 = {
'DailyFile5.doc' => 102,
'DailyFile2.doc' => 48,
'DailyFile6.doc' => 114,
'DailyFile1.doc' => 20,
'DailyFile3.doc' => 83,
'DailyFile4.doc' => 91
};
DailyFile6
$VAR1 = {
'DailyFile5.doc' => 102,
'DailyFile2.doc' => 48,
'DailyFile6.doc' => 114,
'DailyFile1.doc' => 20,
'DailyFile3.doc' => 83,
'DailyFile4.doc' => 91
};



So the code "works" in the sense that it find what I need as a pre-requisite (.check file) but I want it to take action ONE time, not how many files it finds.

In my original post, I said I understand thats what 'for each' does. What I am looking for is a loop that will do the same thing but only do it ONCE, no matter how many files it finds.

Expected results would be similar to:

DailyFile1
DailyFile2
DailyFile3
DailyFile4
DailyFile5
DailyFile6

$VAR1 = {
'DailyFile5.doc' => 102,
'DailyFile2.doc' => 48,
'DailyFile6.doc' => 114,
'DailyFile1.doc' => 20,
'DailyFile3.doc' => 83,
'DailyFile4.doc' => 91
};


BillKSmith
Veteran

Jul 24, 2013, 9:58 AM

Post #14 of 15 (492 views)
Re: [StarkRavingCalm] Problem in for loop [In reply to] Can't Post

For each of the six files, your loop does each of the following things once.

  • print the basename of the file


  • print the directory of .doc files


  • Remove the .check file



  • To get your expected output, you must move the second function (but not the third) out of the loop.
    Good Luck,
    Bill


    StarkRavingCalm
    User

    Jul 24, 2013, 10:21 AM

    Post #15 of 15 (490 views)
    Re: [BillKSmith] Problem in for loop [In reply to] Can't Post

    I think I may have figured out.
    You are correct, it's not the loop.
    I think it's because I am dereferencing $droppedfilename but then going back the server to do an 'ls' which SFTP:Foreign treats as hash reference. So I probably need to derefernce a second time.


    Thank you for your help


    (This post was edited by StarkRavingCalm on Jul 24, 2013, 10:21 AM)

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives