LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-01-2010, 02:59 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
conditional foreach loop in perl


#!/usr/bin/perl -w
use strict;
my @files = `ls -1`;
my $file;
foreach $file(@files) {
print "$file";
print "\n";
chomp $file;
sleep 7;
use File::Copy;
move("$file", "/home/casper1/tmp") or die "Move failed";
}

I use this to move file from one directory to a tmp directory. I works for me because it prints out the files before it moves them. If I do not want to move that particular file, I hit control c to stop script. I want to have the ability to skip over certain files in the array(have them remain in the designated folder) have been trying to add some if/else
statements asking for input verification - and then a shift(@files) command to skip over some files, but (see below) - but what the program does is wait for the first "y" then it moves all the files over. I want something that will ask me each time. I have been working on this two days it is time to get some advice.


#!/usr/bin/perl -w
use strict;
my @files = `ls -1`;
my $files;
print "Do you want to move file $files[0]";
my $answer = <>;

if ($answer eq "y") {
foreach $file(@files) {
print "$file";
print "\n";
chomp $file;
sleep 3;
use File::Copy;
move("$file", "/home/casper1/test") or die "Move failed";
}

elsif ($answer eq "n") {
$file = shift(@files);
}

else {
$file = shift(@files);
print " the file has been skipped";
}
}

Last edited by casperdaghost; 06-01-2010 at 03:02 PM.
 
Old 06-01-2010, 03:53 PM   #2
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,615

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
Why are you doing this in perl?

Perl is a wonderful tool, but for something like this with user I/O on every file it is less than optimal. You are not taking advantage of perl's internal functions, globing, or other features of power to process more efficiently than a shell, so why not use a shell?

Almost any shell that includes 'read' and 'if' can prompt and gulp user response and conditionally call 'mv' to perform as well with greater simplicity.

UNtested and just off the top of my tired old head:

#!/bin/bash
HOME=/home/casper1
TARG=${HOME}/test

for file in `ls` ; do
echo -n "Shall I move ${file}? "
read foo junk
case $foo in
Y|y)
if mv $file $TARG
then
echo " File $foo moved to $TARG "
else
echo " File $foo move FAILED! "
fi
;;
*)
echo " Skipping $foo "
;;
esac
done
 
0 members found this post helpful.
Old 06-01-2010, 04:23 PM   #3
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
In order to one day "take advantage of Perl's internal functions, globing, or other features of power" I am going to have to learn the language first- which means using it, even for the mundane. Thanks for the input though.
 
  


Reply



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
Perl wonky IF outcome inside a @list foreach loop OldGaf Programming 3 09-18-2009 10:21 AM
Perl - foreach loop reading only the last $scalar in a @list OldGaf Programming 3 08-08-2009 09:57 AM
PERL - foreach array not clearing michael.barnes Programming 8 08-21-2007 09:37 PM
Perl: Where am I in a foreach loop? jrtayloriv Programming 3 01-30-2005 10:43 PM
perl:foreach katana Programming 3 07-24-2001 01:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:10 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
Open Source Consulting | Domain Registration