Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl脚本,将输入打印到一行和其他行中_Perl - Fatal编程技术网

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

Perl脚本,将输入打印到一行和其他行中,perl,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

我这里有个问题。请看我的密码

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 月份:7月 年份:2011年 我想把它打印到4TQCJuly2011.jpg中

我陷入了圈套

谢谢你的阅读和帮助

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

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

它总是打印出来 4. 全面质量管理 七月 2011 jpg先生


我想要的不是4TQCJULY2011.jpg,而是这个结果

你可以在咀嚼后简单地组合它们

其他方法包括sprintf


有关更多信息,请参见perldoc-f sprintf

我想我一定误解了你的问题,但是如果你只想打印字符串$area、$equipment、$month和$year,为什么不使用


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

从VAR中删除所有换行符

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" );
    }
}

这应该可以帮助你开始做作业。

?另外,请更正格式谢谢您的格式。这是我第一次在这个网站上发布。连接实际上和我的$new\u值=$old\u值$other\u值一样简单。要删除前导和尾随空格,最好使用trim。。。from String::UtilI希望格式为$area$equipment$month$year.jpgso使用时会发生什么。运算符来连接这些字符串?记住每一个输入。另外,要将.jpg放在后面,请使用。又是接线员。我不明白为什么这么难。它打印2011年7月4日TQC。jpg在单独的行中,而不是一行。我说你需要先咀嚼这些输入。以前尝试过,但不起作用。谢谢你的回答。我只是个新手perl@Sharil定义不起作用。您得到了什么以及预期的结果是什么?它在单独的行中打印4 TQC 2011年7月.jpg,而不是一行也许您需要在输入字符串上使用chomp来删除尾部的CRs
my $str = "$area$equipment$month$year.jpg";
$str =~ s/(\r|\n)//g; #you could potentially filter this more.. 
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" );
    }
}