http://www.perlmonks.org?node_id=791437

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

I have to strings one if for 1st name the next is for last name. it prints out like this"1stlast" I want 1st last how can I do this. what can I use as an operator?
  • Comment on how to add space between printing strings

Replies are listed 'Best First'.
Re: how to add space between printing strings
by GrandFather (Saint) on Aug 26, 2009 at 20:16 UTC

    Show us your code then we can more easily see what is not right. I'd guess you are either concatenating two variables with the concatenation operator, in which case you need to concatenate a space in between, or you are interpolating two variables into a string, in which case you simply insert a space between the two variables.

    I've got some code samples that demonstrate those two techniques, but I'm not going to show you mine until you've shown me yours.


    True laziness is hard work
Re: how to add space between printing strings
by bv (Friar) on Aug 26, 2009 at 20:18 UTC

    TIMTOWTDI. Here are a few:

    my ($first,$last) = ("John", "Doe"); # interpolate into a string: print "$first $last\n"; # put a literal space between them in list context: print $first, ' ', $last,"\n"; # join a list of values with space. This works with arrays, too print join(' ', ($first, $last)),"\n"; # $, is the output field separator { local $,=' '; # prints "John Doe \n", note the space at the end! print $first, $last, "\n"; }
    $,=' ';$\=',';$_=[qw,Just another Perl hacker,];print@$_;
Re: how to add space between printing strings
by Tanktalus (Canon) on Aug 26, 2009 at 20:18 UTC

    How are you printing it out?

    print $f,$l,"\n"; #becomes one of: { local $, = ' '; print $f, $l; print "\n"; } print $f, ' ', $l, "\n"; print join(' ', $f, $l), "\n";
    or maybe
    print $f . $l . "\n"; # becomes print $f . " " . $l . "\n";
    or maybe
    print "$f$l\n"; # becomes print "$f $l\n";
    or maybe
    printf "%s%s\n", $f, $l; # becomes printf "%s %s\n", $f, $l;
    But I'm not sure what you're doing now, so it's hard to tell which one is most similar to your existing code.

Re: how to add space between printing strings
by Marshall (Canon) on Aug 26, 2009 at 20:19 UTC
    print "$first_name $last_name\n"; Perl interpolates the $var names within double quotes in a print.
    BTW, a valid var name can not start with a digit. (1st_name is not right).