Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

how to add space between printing strings

by Anonymous Monk
on Aug 26, 2009 at 20:09 UTC ( #791437=perlquestion: print w/ replies, xml ) Need Help??
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
Re: how to add space between printing strings
by GrandFather (Cardinal) 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 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 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 Marshall (Prior) 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).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://791437]
Approved by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (16)
As of 2014-12-18 19:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (61 votes), past polls