Concatenation Question

C

Chip

How can I add a ".jpg" extension to a string?

I'm trying the following with 12345 as the input.

#!/usr/bin/perl -w
print "Please enter the item picture number to remove: ";
my $number=<STDIN>;
my $path=http://someplace.com/images/;
my $ext=".jpg";
my $picture="$path . $number . $ext";
system rm $picture;
print "$picture has been removed";
__END__

The output I'm hoping for is:
http://someplace.com/images/12345.jpg has been removed

What I get is:
http://someplace.com/images/12345 has been removed

How do I add the ".jpg"

Thanks
Chip
 
N

Nick Santos

Chip said:
How can I add a ".jpg" extension to a string?

I'm trying the following with 12345 as the input.

#!/usr/bin/perl -w
print "Please enter the item picture number to remove: ";
my $number=<STDIN>;
my $path=http://someplace.com/images/;
my $ext=".jpg";
my $picture="$path . $number . $ext";
system rm $picture;
print "$picture has been removed";
__END__

The output I'm hoping for is:
http://someplace.com/images/12345.jpg has been removed

What I get is:
http://someplace.com/images/12345 has been removed

How do I add the ".jpg"

Thanks
Chip
Ok, I put this code into my interpreter, and it founf a problem with your
$path declaration. Other than that, what you need is to put this in before
you declare $picture

chomp ($number);

so your code will look like this

print "Please enter the item picture number to remove: ";
my $number=<STDIN>;
my $path='http://someplace.com/images/';
my $ext='.jpg';
chomp ($number);
my $picture="$path$number$ext";
print "$picture has been removed";

That should give you output of

http://someplace.com/images/12345 has been removed
 
M

Matt Garrish

Chip said:
How can I add a ".jpg" extension to a string?

I'm trying the following with 12345 as the input.

#!/usr/bin/perl -w
print "Please enter the item picture number to remove: ";
my $number=<STDIN>;
my $path=http://someplace.com/images/;
my $ext=".jpg";
my $picture="$path . $number . $ext";
system rm $picture;
print "$picture has been removed";
__END__

The output I'm hoping for is:
http://someplace.com/images/12345.jpg has been removed

What I get is:
http://someplace.com/images/12345 has been removed

You couldn't possibly be getting that output based on the code above. For
one, it would never run because your $path variable is not quoted. Second,
when you read from STDIN, you also get the carriage return (\n). Third,
your extension is statically defined, so it must appear somewhere in your
output. And finally, you're not concatenating your string when you put it in
double quotes. Fixing your above code to run produces the following result:

http://someplace.com/images/ . 12345
. .jpg has been removed

If you post your real code, someone might be able to help you.

Matt
 
N

Nick Santos

How can I add a ".jpg" extension to a string?

I'm trying the following with 12345 as the input.

#!/usr/bin/perl -w
Ok, I put this code into my interpreter, and it founf a problem with your
$path declaration. Other than that, what you need is to put this in before
you declare $picture

chomp ($number);

so your code will look like this

print "Please enter the item picture number to remove: ";
my $number=<STDIN>;
my $path='http://someplace.com/images/';
my $ext='.jpg';
chomp ($number);
my $picture="$path$number$ext";
print "$picture has been removed";

That should give you output of

http://someplace.com/images/12345 has been removed
I'm sorry...I meant that it would give you
http://someplace.com/images/12345.jpg has been removed
Nick
 
S

Sam Holden

How can I add a ".jpg" extension to a string?

I'm trying the following with 12345 as the input.

#!/usr/bin/perl -w
print "Please enter the item picture number to remove: ";
my $number=<STDIN>;
my $path=http://someplace.com/images/;
my $ext=".jpg";
my $picture="$path . $number . $ext";
system rm $picture;
print "$picture has been removed";
__END__

The output I'm hoping for is:
http://someplace.com/images/12345.jpg has been removed

What I get is:
http://someplace.com/images/12345 has been removed

No you don't. Since it doesn't compile.
 
J

Jürgen Exner

Chip said:
How can I add a ".jpg" extension to a string?

I'm trying the following with 12345 as the input.

#!/usr/bin/perl -w
print "Please enter the item picture number to remove: ";
my $number=<STDIN>;
my $path=http://someplace.com/images/;
my $ext=".jpg";
my $picture="$path . $number . $ext";
system rm $picture;
print "$picture has been removed";
__END__

The output I'm hoping for is:
http://someplace.com/images/12345.jpg has been removed

What I get is:
http://someplace.com/images/12345 has been removed

You do? Most interesting! I am getting:

Unquoted string "http" may clash with future reserved word at C:\tmp\t.pl
line 4.
Bareword found where operator expected at C:\tmp\t.pl line 4, near
"//someplace"
(Missing operator before eplace?)
Unquoted string "eplace" may clash with future reserved word at C:\tmp\t.pl
line 4.
Unquoted string "com" may clash with future reserved word at C:\tmp\t.pl
line 4.
Unquoted string "images" may clash with future reserved word at C:\tmp\t.pl
line 4.
syntax error at C:\tmp\t.pl line 4, near "http:"
Execution of C:\tmp\t.pl aborted due to compilation errors.

If you fix the syntax error (hint hint: do not retype your code, copy and
paste it!) and comment out the bogus "system" call, then I get (in two
lines!):
http://someplace.com/images/ . 123
. .jpg has been removed
from the one single print statement.
How do I add the ".jpg"

First of all you don't want to add something but you want to get rid of that
superflous line break from your input line: perldoc -f chomp

And then you want to get rid of those additional spaces when defining
$mypicture. Why did you put them in there?

And then you can use either a string
my $picture="$path$number$ext";
or a concatenation
my $picture=$path . $number . $ext;
but dots inside of a string are just that: dots.

Inserting slashes in the proper places is left as an excercise.

As far as the bogus system call is concerned: Did you mean something like
system "rm $mypicture";
If yes then I wonder why you are forking off an external process instead of
using Perl's unlink() command.

jue
 
C

Chip

Thanks Nick,

It was the "\n".
I was getting:

.../images/12345
..jpguser@server#

giving me the .jpg before the prompt.
Sorry I missed that.
I did not cut and paste the code,
just typed it in for illistrative
purposes.
Here is the code:
(cut and paste)
#!/usr/bin/perl -w
print "Please enter the number to be removed: ";
my $number=<STDIN>;
chomp($number);
my $path='../images/';
my $ext='.jpg';
my $picture = $path . $number . '.jpg';
system "rm $picture";
print "$picture has been removed\n";

It works fine now.
Thanks again for your help
Chip
 
C

Chip

Thanks Matt for your reply,

I should have cut and pasted the code
instead of just typing in what I did.

You are right in that the code was giving me
the carrage return displaying the .jpg before
the prompt on the new line.

Adding the chomp fixed it.
Thanks Again
Chip
 
C

Chip

Thanks Jürgen,

Sorry for not cut and pasting!
Here is the code now:
#!/usr/bin/perl -w
print "Please enter the number to be removed: ";
my $number=<STDIN>;
chomp($number); #was the problem
my $path='../images/';
my $ext='.jpg';
my $picture = $path.$number.$ext;
system "rm $picture";
print "$picture has been removed\n";

As far as the system call,
I'll change
system "rm $picture";
to
unlink "$picture";

Thanks
Chip
 
T

Tad McClellan

Chip said:
Sorry for not cut and pasting!


Have you seen the Posting Guidelines that are posted here frequently?

Here is the code now:
#!/usr/bin/perl -w

use strict; # ask for all the help you can get

I'll change
system "rm $picture";
to
unlink "$picture";


Don't do that, as your proposed replacement has two errors in it too.

You have a useless use of double quotes, and you may want to check
to see if you actually got what you asked for by checking the
return value:

unlink $picture or die "could not remove '$picture' $!";




[snip 50 lines of TOFU.
Please learn the accepted way of composing a followup.]
 
M

Matt Garrish

Chip said:
Thanks Matt for your reply,

I should have cut and pasted the code
instead of just typing in what I did.

You are right in that the code was giving me
the carrage return displaying the .jpg before
the prompt on the new line.

Adding the chomp fixed it.

But there was no way to know that for sure from the code you posted, which
is why you should always post your exact code. From the question you were
asking, it sounded like your $ext variable did not contain a value, not that
you were having a problem with the carriage return. It was especially bad
that you paraphrased the output, as that only seemed to confirm that the
code you were using was significantly different from the code you posted.
So, as you've been told many times now, make sure to be precise about your
code and what it does in any future posts.

Matt
 
G

geoffroy

Chip said:
How can I add a ".jpg" extension to a string?

I'm trying the following with 12345 as the input.

#!/usr/bin/perl -w
print "Please enter the item picture number to remove: ";
my $number=<STDIN>;
my $path=http://someplace.com/images/;
my $ext=".jpg";
my $picture="$path . $number . $ext";
system rm $picture;
print "$picture has been removed";
__END__

The output I'm hoping for is:
http://someplace.com/images/12345.jpg has been removed

What I get is:
http://someplace.com/images/12345 has been removed

How do I add the ".jpg"

Thanks
Chip

The problem you are having is that your input ($number) has a new line
character.

You should use


chomp($number = <STDIN>);

instead.



Geoffroy
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The problem you are having is that your input ($number) has a new line
character.

Really? How did you divine that from:

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13

iD8DBQE/3JlfY96i4h5M0egRAtscAJ0cgeETZZ3azGoDpeay4cwZO/IlvQCeJ68T
ALygYWgFNpY8qnWFUJm/MkE=
=ujac
-----END PGP SIGNATURE-----
 
J

James Willmore

The problem you are having is that your input ($number) has a new
line character.

You should use


chomp($number = <STDIN>);

instead.


I'd write something like ... (untested)

#!/usr/bin/perl -w

use strict;
use diagnostics;

print "Please enter the item picture number to remove: ";
chomp( my $number = <STDIN> );
my $path = 'http://someplace.com/images/';
my $ext = '.jpg';
my $picture = sprintf "%s%s%s", $path, $number, $ext;
unlink $picture or die "Can't remove $picture: $!\n";
print "$picture has been removed";

*How* the OP's code ran is beyond me. I got a syntax error right out
of the gate.

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
When Marriage is Outlawed, Only Outlaws will have Inlaws.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top