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: <%-{-{-{-<
In reply to Re: How to combine string and digits in perl (updated x2)
by AnomalousMonk
in thread How to combine string and digits in perl
by suvendra123
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |