Concatenation Question

Discussion in 'Perl Misc' started by Chip, Dec 13, 2003.

  1. Chip

    Chip Guest

    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
    Chip, Dec 13, 2003
    #1
    1. Advertising

  2. Chip

    Nick Santos Guest

    "Chip" <> wrote in message
    news:jLvCb.456$...
    > 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
    Nick Santos, Dec 13, 2003
    #2
    1. Advertising

  3. Chip

    Matt Garrish Guest

    "Chip" <> wrote in message
    news:jLvCb.456$...
    > 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
    Matt Garrish, Dec 13, 2003
    #3
  4. Chip

    Nick Santos Guest


    > "Chip" <> wrote in message
    > news:jLvCb.456$...
    > 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
    >
    >

    I'm sorry...I meant that it would give you
    http://someplace.com/images/12345.jpg has been removed
    Nick
    Nick Santos, Dec 13, 2003
    #4
  5. Chip

    Sam Holden Guest

    On Sat, 13 Dec 2003 03:32:31 GMT, Chip <> wrote:
    > 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.

    --
    Sam Holden
    Sam Holden, Dec 13, 2003
    #5
  6. Chip <> wrote:

    > my $path=http://someplace.com/images/;


    > What I get is:



    A syntax error.

    Put quotes around strings.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 13, 2003
    #6
  7. Chip wrote:
    > 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
    Jürgen Exner, Dec 13, 2003
    #7
  8. Chip

    Chip Guest

    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


    "Nick Santos" <> wrote in message
    news:j_vCb.379074$ao4.1265853@attbi_s51...
    >
    > "Chip" <> wrote in message
    > news:jLvCb.456$...
    > > 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
    >
    >
    Chip, Dec 13, 2003
    #8
  9. Chip

    Chip Guest

    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

    "Matt Garrish" <> wrote in message
    news:U%vCb.16250$...
    >
    > "Chip" <> wrote in message
    > news:jLvCb.456$...
    > > 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
    >
    >
    Chip, Dec 13, 2003
    #9
  10. Chip

    Chip Guest

    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

    "Jürgen Exner" <> wrote in message
    news:JOwCb.1039$...
    > 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
    >
    >
    Chip, Dec 13, 2003
    #10
  11. Chip <> wrote:

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

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 13, 2003
    #11
  12. Chip

    Matt Garrish Guest

    "Chip" <> wrote in message
    news:xWDCb.569$...
    > 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
    Matt Garrish, Dec 13, 2003
    #12
  13. Chip

    geoffroy Guest

    Chip wrote:
    > 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
    geoffroy, Dec 14, 2003
    #13
  14. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    geoffroy <> wrote in
    news:3fdc3ed9$0$2879$:

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

    line
    > character.


    Really? How did you divine that from:

    > Chip wrote:
    >> 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"


    - --
    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-----
    Eric J. Roode, Dec 14, 2003
    #14
  15. On Sun, 14 Dec 2003 11:43:48 -0500
    geoffroy <> wrote:

    > Chip wrote:
    > > 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__

    <snip>
    > 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.
    James Willmore, Dec 14, 2003
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf Hildebrandt

    type conversion and concatenation

    Ralf Hildebrandt, Jul 23, 2003, in forum: VHDL
    Replies:
    3
    Views:
    6,678
  2. walala
    Replies:
    3
    Views:
    4,796
    walala
    Sep 18, 2003
  3. colonel

    Question about concatenation error

    colonel, Sep 7, 2005, in forum: Python
    Replies:
    4
    Views:
    302
    Terry Hancock
    Sep 7, 2005
  4. Karsus
    Replies:
    2
    Views:
    1,175
    Karsus
    Nov 22, 2011
  5. dredge

    Javascript Concatenation Question

    dredge, Oct 9, 2007, in forum: Javascript
    Replies:
    10
    Views:
    297
    Thomas 'PointedEars' Lahn
    Oct 12, 2007
Loading...

Share This Page