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

Hi monks!
I'm at a loss about why this simple concatenation is not working. I'm pulling a value from a hash and attempting to concatenate ".cal" on the end.
$Cal_ref = { 'cal_name' => $cal_name, 'dates' => \@dates, 'desc' => $cal_desc }; $twsCalendars[$i] = $Cal_ref; $i++; ... for (my $i = 0; $i < $length; $i++) { # cycle through the list and print all the calendar names my $c_href = $twsCalendars[$i]; my $cal_name = $$c_href{'cal_name'}; my $foo = ".cal"; my $var1 = $cal_name . "$foo"; print ("$cal_name\n"); print ("$var1"); print "\n"; print (" $var1"); print "\n";
This outputs the following:
BCPFTPCL .calTPCL .calBCPFTPCL
Whatever I try, I can't concatenate the ".cal" on the end of $cal_name.
thanks,
Dan H.

Replies are listed 'Best First'.
Re: Concatenation appends to front of variable
by ikegami (Patriarch) on May 29, 2009 at 20:51 UTC
    $cal_name has a trailing Carriage Return (i.e. $cal_name eq "BCPFTPCL\x0D"), so $var1 ends up being "BCPFTPCL\x0D.cal".
      Ahh, thank you. I had tried chomp to no avail earlier and never pressed beyond that when it didn't work.
      Chop solved my problem!
      Dan H.
        s/\s+$//; will get rid of trailing LF, CR or CRLF (and any trailing space and tabs) safely.

        Avoid chop. It is a rather blunt tool and will cause wailing and gnashing of teeth. If chomp didn't do the job then most likely the line ends are not what you think they are. Are you processing Windows generated files on a *nix box perchance?


        True laziness is hard work