Sharil
Sharil

Reputation: 1

Perl Script, Print input into one line and others

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 objective 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)

Upvotes: 0

Views: 669

Answers (3)

Zach Leighton
Zach Leighton

Reputation: 1941

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..

Upvotes: 0

blankabout
blankabout

Reputation: 2627

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");'?

Upvotes: 0

ghostdog74
ghostdog74

Reputation: 342263

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

Upvotes: 0

Related Questions