Concatenating 2 Perl Strings

W

William

Expected output:
/local/home/mk_murex/eod/fo_files/Vols/trs_vol.txt_back

Current output:
/local/home/mk_murex/eod/fo_files/Vols/
/local/home/mk_murex/eod/fo_files/Vols/trs_vol.txt_back


Code in question:

#!/usr/bin/perl -w

use strict;

my $InputFileDir = "/local/home/mk_murex/eod/fo_files/Vols/";
my $RunDir = "/mkapp/webapps/mxrt-cgi/upload_vol/";
my $filename = "trs_vol.txt";
my $scriptname = "upload_vol_main.pl";
my $checklist = "dummylist.txt";
my $cmd = $RunDir . $scriptname;
my $extension = "_back";

my $originalList = $InputFileDir . $filename;
my $originalListBack = $originalList . $extension;
my $dummy_list = $RunDir . $checklist;

#print $originalList . "\n";
print $originalListBack . "\n";

# need to rename the original trs_vol.txt
system "mv $originalList $originalListBack";


Questions:
1a) Why is the value in $originalListBack concatenated?
1b) Why is the concatenation only has the value of $InputFileDir and not
$InputFileDir . $filename?
1c) What is the proper way of concatenating a Perl string other than using
"join()"? Right now I am using a lot of variables.

P.S. I checked perldoc -f join

Thanks.
 
B

Brian Wakem

William said:
Expected output:
/local/home/mk_murex/eod/fo_files/Vols/trs_vol.txt_back


That's what I get.


Current output:
/local/home/mk_murex/eod/fo_files/Vols/
/local/home/mk_murex/eod/fo_files/Vols/trs_vol.txt_back


I don't get that.


Code in question:

#!/usr/bin/perl -w

use strict;

my $InputFileDir = "/local/home/mk_murex/eod/fo_files/Vols/";
my $RunDir = "/mkapp/webapps/mxrt-cgi/upload_vol/";
my $filename = "trs_vol.txt";
my $scriptname = "upload_vol_main.pl";
my $checklist = "dummylist.txt";
my $cmd = $RunDir . $scriptname;
my $extension = "_back";

my $originalList = $InputFileDir . $filename;
my $originalListBack = $originalList . $extension;
my $dummy_list = $RunDir . $checklist;

#print $originalList . "\n";
print $originalListBack . "\n";

# need to rename the original trs_vol.txt
system "mv $originalList $originalListBack";


Questions:


Your questions don't seem to apply as the output is what you expected.
 
U

usenet

William said:
Expected output:
/local/home/mk_murex/eod/fo_files/Vols/trs_vol.txt_back

Current output:
/local/home/mk_murex/eod/fo_files/Vols/

Where did that line come from? Your code only shows one print
statement. I don't think the output you are posting here was generated
by the script you posted here.
/local/home/mk_murex/eod/fo_files/Vols/trs_vol.txt_back

That is exactly what you said was your "Expected output." What's the
question?
#!/usr/bin/perl -w

# need to rename the original trs_vol.txt
system "mv $originalList $originalListBack";

If you want to rename a file in Perl, rename() it (perldoc -f rename).
Never shell out to system commands to do built-in core Perl functions.
And check $!
1a) Why is the value in $originalListBack concatenated?

Because you concatenated it (with a dot).
1b) Why is the concatenation only has the value of $InputFileDir and not
$InputFileDir . $filename?

Which concatenation? Do you mean the variable $originalList? How do
you know what the value of that variable is? You commented-out the
print statement in the code you posted.
1c) What is the proper way of concatenating a Perl string other than using
"join()"?

There are a number of ways, none of which are necessairly more or less
proper than any other. Usually when you say "concatenate strings"
within a Perl context, the first thing that comes to mind is the "."
operator (ie, print $foo . $bar . $baz). But you can do it with join
(join '', $foo, $bar, $baz), or just print a bunch of variables as a
string (ie, print "$foo$bar$baz";) or printf (print "%s%s%s", $foo,
$bar, $baz) or in a loop (print for $foo, $bar, $baz;) or probably a
dozen or more other ways.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top