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

Hello Monks, How to combine string and digits in perl suppose dgrs and 1 should print as dgrs_1 When I am applying . operator between $abc . _$_ it is printing dgrs ._1
while ( my $line = <$in_fh> ) { if (my ($abc) = $line =~ m{ \A (.*dgrs) }xms) { print $temp_out "$abc . _$_;\n" for 1..3; } else { print $temp_out $line; } }
Thanks

Replies are listed 'Best First'.
Re: How to combine string and digits in perl (updated x2)
by AnomalousMonk (Archbishop) on Mar 17, 2021 at 01:13 UTC

    Win8 Strawberry 5.8.9.5 (32) Tue 03/16/2021 21:10:53 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings my $abc = 'dgrs'; $_ = 1; # print $temp_out "$abc . _$_;\n"; # not this, with extra dot and spa +ces print 'A: ', $abc . "_$_", "\n"; # but this ... print 'B: ', "${abc}_$_", "\n"; # or this. print "C: ${abc}_$_;\n"; # update: or even this. ^Z A: dgrs_1 B: dgrs_1 C: dgrs_1;

    Update 1: In example B or C above, what happens if the {} curly braces are left out of the "${abc}_$_" expression, so it becomes "$abc_$_"? Try it! Do you get the desired or expected result? If not, why not?

    Update 2: Examples of 1nickt's approach++ and one other. Note that the concern about curly braces goes away.

    Win8 Strawberry 5.8.9.5 (32) Tue 03/16/2021 23:02:15 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings my $abc = 'dgrs'; $_ = 1; printf "D: %s_%d;\n", $abc, $_; my $spf = sprintf "E: %s_%d;\n", $abc, $_; print $spf; my $fmt_dgrs = "F: %s_%d;\n"; printf $fmt_dgrs, $abc, $_; print join '', 'G: ', $abc, '_', $_, ';', "\n" ^Z D: dgrs_1; E: dgrs_1; F: dgrs_1; G: dgrs_1;


    Give a man a fish:  <%-{-{-{-<

      If it will be .dgrs(dgrs) then how to change into .dgrs_1(dgrs_1) .dgrs_2(dgrs_2) .dgrs_3(dgrs_3)

        I don't understand your question. However, I have a sneaking suspicion that davido's answer may address it.

        davido's answer is better than mine because it says why you are not getting the output format you want in addition to saying how to get it; I only say how.


        Give a man a fish:  <%-{-{-{-<

Re: How to combine string and digits in perl
by hippo (Archbishop) on Mar 17, 2021 at 09:30 UTC

    See How to ask better questions using Test::More and sample data

    use strict; use warnings; use Test::More tests => 2; my $abc = 'dgrs'; my $n = 1; my $want = 'dgrs_1'; my $have = ''; $have = $abc . '_' . $n; is $have, $want, 'Concatenation operator (perldoc perlintro)'; $have = "${abc}_$n"; is $have, $want, 'String interpolation (perldoc perlop)';

    I also strongly recommend that you pick an indentation scheme and stick to it. Random whitespace is doing you no favours.


    🦛

Re: How to combine string and digits in perl
by 1nickt (Canon) on Mar 17, 2021 at 01:44 UTC

    Also see printf, sprintf.


    The way forward always starts with a minimal test.
Re: How to combine string and digits in perl
by davido (Cardinal) on Mar 17, 2021 at 13:48 UTC

    The concatenation operator (dot), described in perlop, is not intended to be used inside a string. It is expected to join strings. So the following work:

    "foo" . '_' . 123 $abc . '_' . 123 "foo" . '_' . $num

    But the following do not work, because dot inside a string is just a dot, not an operator:

    "$abc . _ . 123" "foo . _ . $num"

    What you want is one of the following:

    "${abc}_$_" # 1 $abc . '_' . $_ # 2 join('_', $abc, $_) # 3 "$abc\_.$_" # 4 $abc . "_$_" # 5

    I prefer option 1, 2 or 3. Option 2 is probably the most legible.

    Perl would probably make a lot more sense if you read perlintro, perlsyn, perlop, perlsub, and perldata. That's a couple hours worth of material, but it would move you forward in your learning curve.

    In perlop you can also read The Gory Details of Parsing Quote and Quote-like Constructs.


    Dave