in reply to How to combine string and digits in perl

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:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: How to combine string and digits in perl (updated x2)
by suvendra123 (Initiate) on Mar 17, 2021 at 09:11 UTC
    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:  <%-{-{-{-<