Perl脚本,将输入打印到一行和其他行

时间:2011-04-26 02:34:09

标签: perl

我在这里遇到了问题。请看我的代码。

use warnings;
use strict;


###ASK IMPUT FROM USER
print "Enter the Area: ";
my $area = <>;

print "Enter the Equipment: ";
my $equipment = <>;

print "Enter the Month: ";
my $month = <>;

print "Enter the Year: ";
my $year = <>;



###SUBROUTINE (FUNCTION) TO REMOVE THE WHITESPACE
sub rem_spaces {
    my $t = shift || return(0); #Common practice: Use shift to take $t as a first parameter passed to the subroutine
    $t =~ s/^\s+//; #remove leading spaces
    $t =~ s/\s+$//; #remove trailing spaces
    return $t;
}


####VARIABLE DECLARATION

my $REMOTE_HOST = "C:\\Users\\attmsbj1\\Desktop\\pic";
my $RESULT_FOLDER = "C:\\Users\\attmsbj1\\Desktop\\RESULT";


###CREATE FOLDER IF FOLDER DOES NOT EXIST.

if(-e $RESULT_FOLDER){
    print "File exists.";
}
else {
    mkdir ($RESULT_FOLDER);
}

###OPEN THE NEW FOLDER<br>
opendir (NewFolder, $RESULT_FOLDER) or die "Couldn't open the current directory: $!";



###OPEN PHOTOS FOLDER<br>
opendir (PicFolder, $REMOTE_HOST) or die "Couldn't open the current directory: $!";
@PicFolder =<PicFolder>;

###LOOP THROUGH ALL THE PICTURES IN THE FOLDER
foreach (@PicFolder)
{
    if(###FULL STRING INPUT BY USER = PICFOLDER)
    {
        ###MOVE OR COPY THE PICTURE TO THE NEW FOLDER
    }
}

closedir PicFolder;

现在的问题是1)如何将用户输入的输入打印成1个字符串?
例如。面积:4;
设备:TQC
月:七月 年份:2011年 我想把它打印到4TQCJuly2011.jpg

我被困在循环中

感谢阅读和帮助。

其他细节 我的主要目标是创建一个perl脚本

  1. 询问用户的输入
  2. 打开照片文件夹
  3. 将用户的输入名称(将输入合并为 * .jpg)与照片文件夹中的图片名称比较
  4. 如果相同,请移动图片或将图片复制到新文件夹
  5. 我希望用户提供的输入格式为$ area $ equipment $ month $ year.jpg

    它总是打印出来 4
    TQC
    JULY
    2011
    .JPG

    而不是4TQCJULY2011.jpg(我想要这个结果)

3 个答案:

答案 0 :(得分:0)

您可以在chomping

之后简单地将它们组合在一起
$area . $equipment . $month . $year

其他方法包括sprintf

$result = sprintf("%s%s%s%s", $area, $equipment, $month,$year);

有关详情

,请参阅perldoc -f sprintf

答案 1 :(得分:0)

我想我一定是在误解你的问题,但如果你想做的就是打印字符串$ area,$ equipment,$ month和$ year你为什么不用它?


 print ($area.$equipment.$month.$year.".jpg");'?

答案 2 :(得分:0)

从vars中删除所有换行符

my $str = "$area$equipment$month$year.jpg";
$str =~ s/(\r|\n)//g; #you could potentially filter this more.. 

注意:我没有在Windows上执行perl所以这可能不是100%正确,linux我会使用mv

foreach my $cur (@PicFolder) {
    if($str =~ /$cur/){
       # I don't think this will work on windows.. `cp $PicFolder/$cur $NewFolder/$cur`;
       #this might work..
       rename ("$PicFolder/$cur, "$NewFolder/$cur" );
    }
}

这应该可以帮助你开始..