开发者

Perl Script, Print input into one line and others

开发者 https://www.devze.com 2023-02-28 21:31 出处:网络
I got a problem here. Please see my codes. use warnings; use strict; ###ASK IMPUT FROM USER print \"Enter the Area: \";

I got a problem here. Please see my codes.

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;

Problem now is 1) How do I print out the input given by the user into 1 string ??

Eg. Area : 4;

Equipment : TQC

Month : July

Year : 2011

I want to print this into 4TQCJuly2011.jpg

I'm stuck in the loop

Thanks for reading and helping.

Additional Details My main ob开发者_StackOverflow社区jective is to create a perl script to

  1. Ask input from user

  2. Open the photo folder

  3. Compare the input name(combine the input into *.jpg) from the user with the picture name in the photo folder

  4. If it is the same, move the picture or copy the picture to the new folder

I want the format of the input given by the user to be $area$equipment$month$year.jpg

It always print out

4

TQC

JULY

2011

.jpg

instead of 4TQCJULY2011.jpg (I want this result)


You can simply combine them after chomping

$area . $equipment . $month . $year

other methods include sprintf

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

See perldoc -f sprintf for more


I think I must be misunderstanding your question, but if all you want to do is print the strings $area, $equipment, $month and $year why don't you just use


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


Remove all the newline chars from the vars

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

Note: I haven't done perl on windows so this might not be 100% correct, linux I'd use 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" );
    }
}

This should help you get started..

0

精彩评论

暂无评论...
验证码 换一张
取 消