wolverina has asked for the wisdom of the Perl Monks concerning the following question:

for some reason i can't quite get the format on this... i just want to append the characters .pl to a variable. So, if $a=wolf, i want to append the characters .pl, so $a now equals wolf.pl. I tried this: $a = ${a}.pl; but the period does not show up between wolf and pl. Any Idea's? -Lisa

Update by myocom: Changed the title to better fit the question.

  • Comment on Appending characters to an existing string

Replies are listed 'Best First'.
Re: Appending characters to an existing string
by rdfield (Priest) on Nov 22, 2002 at 10:29 UTC
    Can I have a go? :)

    First up, nice and short:

    $a .= ".pl";
    then of course there's the ever faithful:
    $a = "$a.pl";
    then of course we could get a little tricky:
    $a = pack("A*A*",$a,".pl");
    or just plain ridiculous:
    my $x = sub {return shift() . shift()}; $a = &$x($a,".pl");
    and onto the sublime:
    $a=~s#$#.pl#;
    rdfield
      I like the last one.
Re: Concatenation Operator (aka simple question)
by Enlil (Parson) on Nov 22, 2002 at 06:24 UTC
    You can be a little more descriptive in the Titles of your nodes. But anyhow try this:
    my $a = 'wolf'; $a = $a . ".pl";
    The . is the concatenation operator so the code above does the following:
    1. Assigns 'wolf' to $a
    2. concatenates what is $a to ".pl" (using the . operator in between).
    3. assigns the concatenated string to $a (string being "wolf.pl"

    it would not show up (though I don't think the code you posted would work)

    Update: Definately more descriptive than "simple question".

    -enlil

Re: simple question
by hydo (Monk) on Nov 22, 2002 at 06:21 UTC
    my $a = "wolf"; my $pled = $a . ".pl"; # $pled is now "wolf.pl"
    There are plenty of other ways to do it. I just got into the habit of doing it that way for no particular reason.
Re: Appending characters to an existing string
by revdiablo (Prior) on Nov 22, 2002 at 06:42 UTC

    In addition to the suggestions above, you can use .= as a little bit of a shortcut. This will append the given value to the end of the given variable. Example:

    $var = 'wolf'; $var .= '.pl';
Re: Appending characters to an existing string
by PodMaster (Abbot) on Nov 22, 2002 at 07:35 UTC
    so if $a=wolf, I want to appent the characters .pl, so $a now equals wolf.pl. I tried this: $a = ${a}.pl;
    Why did you try that?
    perl -we"$a=wolf;die $a" Unquoted string "wolf" may clash with future reserved word at -e line +1. wolf at -e line 1.
    Did you ever run accross this error?

    How do you declare a string in perl?

    $a and ${a} both refer to the scalar $a, and that part's understandable, but what did you think .pl would do?

    The period (.) is the concatination perator, and it will concatinate $a and pl.

    perl -we"$a=1;$a=${a}.pl;print $a" Unquoted string "pl" may clash with future reserved word at -e line 1.
    You should have seen this error before. What you're probably after is $a = "${a}.pl";

    Where are you learning perl from?
    Do you have a perl book?
    What perl documentation have you read?

    Good Advice and Maxims for Programmers


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: Appending characters to an existing string
by demerphq (Chancellor) on Nov 22, 2002 at 14:46 UTC
    You are doing perl without strictures enabled. This is a very very bad idea. The code samples you have provided would not even compile under strictures, and furthermore had you been using strictures the proper use of the concatenation operator '.' would have been clear.

    Please make sure that every script that you write starts with

    use strict; use warnings;
    or if you are on a pre 5.6 version of perl (you may need to alter the path after the #! mark as required by your local setup)
    #!perl -w use strict;
    You will find that you learn much faster when the compiler is constantly pointing out ambiguous structures, errors, and other forms of strangeness.

    Incidentally if you wish to append chars to a string contained within a scalar the normal approach is

    $foo.='some string';
    Just about anytime you have a construct that goes like
    $foo=$foo (operator) (value)
    you can rewrite it as
    $foo (operator)= (value)
    Note that there is NO space in between the operator and the equals. Common examples of this are
    $foo+=10; $foo*=2; $foo.='bar'; $foo||='default';
    HTH

    --- demerphq
    my friends call me, usually because I'm late....

Re: Appending characters to an existing string
by artist (Parson) on Nov 22, 2002 at 07:38 UTC
    Hi,
    You looks like that you want to join the extension and make the filename complete.
    You can also use:
    $file = 'wolf'; $ext = 'pl'; $file = join '.' => ( $file, $ext);
    Artist
      TIMTOWTDI :-)