Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

appending a variable

by pragovnj (Acolyte)
on Apr 28, 2022 at 15:36 UTC ( [id://11143394]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

Based on a reply to my earlier question, I have an issue. I have a a variable, $FN_OUTPUT

$FN_OUTPUT = "$DST_DIR/XXX_SIPABCD_5MIN_OHR_${YYYYmmddHHMMSS} +_${INSTANCE}_$$"; # Suffixes added later: .processing .comple +ted

I need to modify the above as

$FN_OUTPUT          = "$DST_DIR/XXX_SIPABCD_5MIN_OHR_${YYYYmmddHHMMSS}ss_${INSTANCE}_$$";         # Suffixes added later: .processing .completed

where ss will be the VM instance added This is my code, thanks to previous tips on this site

my $vmstr = "zldcdyh1bdce2d14-xxx-vccpohr-correlator-0"; my ($opstr) = $vmstr =~ /-(\d\d)$/; #using the value of $vmstr of t +he VM but with an added 0 my $OPinst = “/PMOSS_SIPNBVQM_5MIN_OHDR_${YYYYmmddHHMMSS}ss”; But I am not sure how to use the $opstr in $FN_OUTPUT = "$DST_DIR/XXX_SIPABCD_5MIN_OHR_${YYYYmmddHHMMSS} +ss_${INSTANCE}_$$"; Is it as $FN_OUTPUT = "$DST_DIR/XXX_SIPABCD_5MIN_OHR_${YYYYmmddHHMMSS} +$opstr_${INSTANCE}_$$";

if it is VM=0 then FN_OUTPUT          = "$DST_DIR/XXX_SIPABCD_5MIN_OHR_${YYYYmmddHHMMSS}00_${INSTANCE}_$$"; or if VM =1, then FN_OUTPUT          = "$DST_DIR/XXX_SIPABCD_5MIN_OHR_${YYYYmmddHHMMSS}01_${INSTANCE}_$$";

Replies are listed 'Best First'.
Re: appending a variable
by hippo (Bishop) on Apr 28, 2022 at 16:16 UTC

    You need to use braces as you have with the other variables to detach the trailing underscore from the variable name.

    use strict; use warnings; my $opstr = '01'; my $foo = "a_${opstr}_b"; print "$foo\n";

    If you remove the braces even this short snippet will not compile.


    🦛

      Yes, braces are one way to disambiguate where the variable name ends.
      Often I will use the concatenate operator to avoid the need for braces.
      This probably runs slower, but you can space out the var names to be substituted in order to make them more obvious (YMMV).

      my $opstr = '01'; my $foo = "a_${opstr}_b"; my $bar = 'a_'. $opstr .'_b'; print "$foo\n"; print "$bar\n";
      Also, OP probably meant: my ($opstr) = $vmstr =~ /-(\d+)$/; in the case of one digit (e.g. 0)?

        Aktchually they compile down to the same thing under the hood (because the former is just syntactic sugar for the latter more or less):

        $ perl -MO=Deparse,-p,-q -E 'my $foo = qq{a_${blah}_b}; my $bar = q{a_ +} . $blah . q{_b};' use feature 'current_sub', 'bitwise', 'evalbytes', 'fc', 'postderef_qq +', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; (my $foo = 'a_' . $blah . '_b'); (my $bar = 'a_' . $blah . '_b'); -e syntax OK

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

Re: appending a variable
by GrandFather (Saint) on Apr 28, 2022 at 21:20 UTC

    You still haven't understood, or are ignoring, the plethora of advice you were given in To read files from two dir that amounts to:

    • Always use strictures (use strict; use warnings;)
    • Write a snippet of code that runs demonstrating the issue
    • Show us what the code prints for you
    • Tell us what you expected
    • Show us any error messages generated

    We aren't interested in distracting problem domain specific identifiers and other cruft that is not related to the issue at hand. Write some code that demonstrates the problem so it is trivial for us to see it too, and so that you can easily play around with the code to see what works and what breaks. With Perl it is trivial to write a little test script and it is a really good way to explore an issue without having all the baggage of the original script you are trying to fix. This is really useful even if you have no intention of asking anyone else for help. It is vital when you do intend to ask for help so that you don't waste our time and you don't waste your own time.

    Remember: being truly lazy is getting the job done with the least overall effort. Often that means more effort up front to save a lot of effort later.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

      Unfortunately this has the hallmarks (as happens here time to time) of someone who's in over their head and is trying to muddle through getting random people on the internet to do the work they've represented themselves as being capable of doing. As has played out they were able to get the first thing "working" now they're hitting another block because they don't know the basics and aren't able to apply what they don't know (i.e. the persistent syntax errors with multiple braces or no braces edit and not understanding variable scoping /edit; not knowing how to delimit variable names in interpolation or thinking there's multiple levels of interpolation as in the last "attempt" above; edit and again, not bothering to try the FAQ they're pointed at (cue the music) /edit).

      The best thing would be for the OP to go back to their manager / supervisor / whomever and own up and explain they're in over their head, they're not the person to make these changes, and to be that person they need more training / to read more / to take time to actually learn how to program.

      Edit: Personally, I'm at the last phase mentioned here WRT "You can't just make s<tuff> up . . .", and I'll just be kibitzing tangentially like with the concatenation vs interpolation deparse rather than trying to inculcate porcine singing.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        I agree completely, which is why I continue to beat on the process and best practice drum rather than feed the OP fish.

        It's tempting, but meaner than I feel at the moment, to down vote the fish feeding replies, even though most of them are great replies and in other contexts highly deserve an up vote.

        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11143394]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-24 18:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found