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

Folks, Try to use "." to concatenation, but it did not work as I expect: $st1="12"; $st2="08"; $result = $st1 . $st2; Got "128" instead of "1208"! Do I miss something by using the "." operator? Thanks.

Replies are listed 'Best First'.
Re: string concatenation
by tachyon (Chancellor) on Nov 12, 2002 at 22:40 UTC

    Works fine for me.

    $st1="12"; $st2="08"; $result = $st1 . $st2; print "Got $result\n"; __DATA__ Got 1208

    You must be doing something else. Perhaps post your actual code. If you don't quote your numbers and use $st2=08 you will get an error (illegal octal) but if you use 007 then you get this:

    $st1="Bond, James Bond "; $st2=007; $result = $st1 . $st2; print "Got $result\n"; __DATA__ Got Bond, James Bond 7

    This looks a bit like your error....

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: string concatenation
by DamnDirtyApe (Curate) on Nov 12, 2002 at 22:41 UTC

    I can't seem to recreate your problem...

    / Mail: 0 / Tue Nov 12 14:39 / ~ $ cat concat.pl $st1 = "12"; $st2 = "08"; $result = $st1 . $st2; print "\n", $result, "\n\n"; / Mail: 0 / Tue Nov 12 14:39 / ~ $ perl concat.pl 1208 / Mail: 0 / Tue Nov 12 14:39 / ~ $

    Can you post a running example of the code not working as intended?


    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
      Hi, The actual code is like this: ($min, $hours) = localtime(2,3); $now = "$hour" . "$min" # expect to see - $now = "1208" For minutes equal or greater than 2 digits, it's fine. Only when minutes is less than 2 digits ( < 10). Any help is appreciated.
        The problem is that $min is returned from localtime as "2" instead of "02", the same with hours for that matter you could do something like the following:
        my ($min,$hours) = (localtime)[2,3]; my $now = $hours . sprintf ("%02",$min);

        -enlil

        Enlil beat me to the sprintf solution, but if you've got more complex date formatting to do, consider using the strftime function from the POSIX module.

        use POSIX ; my $now = POSIX::strftime( "%H%M", localtime ) ; print $now, "\n" ;

        _______________
        DamnDirtyApe
        Those who know that they are profound strive for clarity. Those who
        would like to seem profound to the crowd strive for obscurity.
                    --Friedrich Nietzsche