Search code examples
stringperlstring-concatenation

How to concatenate Perl variables with a hyphen?


I am trying to concatenate two or three variables with hyphens. Please check the below example.

my $QueueName   = Support;
my $CustomerID  = abc;
my $UserCountry = India;
my $Count       = 12345;

my $Tn = $QueueName.$CustomerID.$UserCountry.$Count;

I am getting the following output:

"$Tn" = SupportabcIndia12345

But I want it like this:

$Tn = Support-abc-India-12345

Solution

  • This isn't good Perl, and wouldn't run under use strict, which you really should be using. If you were, you'd see errors displayed.

    Write it like this, instead:

    use strict;
    use warnings;
    
    my $QueueName   = 'Support';
    my $CustomerID  = 'abc';
    my $UserCountry = 'India';
    my $Count       = '12345';
    
    my $Tn = "$QueueName-$CustomerID-$UserCountry-$Count";
    print "$Tn\n";
    

    You have to quote strings. If you want a hyphen you can use the method above to allocate the values separated by a hyphen.