Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^5: To read files from two dir

by graff (Chancellor)
on Apr 14, 2022 at 01:43 UTC ( [id://11142973]=note: print w/replies, xml ) Need Help??


in reply to Re^4: To read files from two dir
in thread To read files from two dir

Just for fun, I deleted one of the two open-curly brackets at line 125, just to see what happens.

Naturally, when I ran "perl -cw" on the script, it still gave up, because the stuff you posted ends in the middle of an "if" block, and before giving up, it reported dozens of "'my' variable ... masks ..." warnings.

But this time it also reported a bunch of "Global symbol '$SRC_DIR' requires explicit package name...", between lines 255 and 354. This is because two different lines above (123 and 128, near that f***ed up "foreach" line), which declare / assign to $SRC_DIR, are both commented out, so $SRC_DIR is never declared.

(Presumably, you should uncomment just one of those lines, and maybe change what's being assigned to it, in order to avoid the compile-time errors. As for what needs to be done to make this rats-nest do anything properly, I have no idea.)

UPDATED TO ADD: By the way, one more thing you need to know about that f***ed up "foreach" line:

foreach my $SRC_DIR (@dir) {{
When you use "my" to declare the looping variable in a foreach loop, that declaration is only valid inside the scope of that loop. That would explain why any use of $SRC_DIR later in the script would be an error. Consider the following:
use strict; use warnings; for my $foo (1,2,3) { print $foo; } # The next line is a compile-time error: $foo is not declared here: print "Scalar variable foo now contains: $foo";

Replies are listed 'Best First'.
Re^6: To read files from two dir
by GrandFather (Saint) on Apr 14, 2022 at 02:12 UTC

    We should emphasize for pragovnj's sake that using my in for loops to localize the loop variable is extremely highly recommended. Failing code as shown by you is almost always a logic error as well as a syntax error because the loop variable is aliased to each of the loop values and any variable of the same name outside the loop is not affected by the machinations of the loop!

    In particular a Perl for loop variable is not like a C for loop variable (where the loop variable retains its last value).

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re^6: To read files from two dir
by pragovnj (Acolyte) on Apr 22, 2022 at 20:33 UTC
    Hi,

    I had been away for a while from work due to some reasons and this issue is still pending. I will try to explain. In the original script, the topology files and the PM files were placed in the same directory, $VTIERBASE/pvc/ohdr/topology and the $SRC_DIR = $VTIERBASE/pvc/ohdr/topology. But in the new script, topology files are in $VTIERBASE/pvc/ohdr/topology and PM files are $VTIERBASE/pvc/ohdr/instance-0/preprocess. I need the $SRC_DIR to read both set of files from these two directories. I tried with the below

    ................. my @dir = ("$VTIERBASE/pvc/ohdr/topology","$VTIERBASE/pvc/ohdr/instanc +e-0/preprocess"); foreach my $SRC_DIR(@dir) ............................. #--------------------------------------------------------------------- +- # Validate the parameters #--------------------------------------------------------------------- +- my $file_cnt= scalar(@feed_files); logNdie "$PROGNAME: ERROR: Invalid number of feed files specified +($file_cnt). 8 are required!\n" unless $file_cnt == 8; # Build the complete path if not fully specified $DONE_DIR = "$VTIERBASE/pvc/ohdr/instance-0/cucp/$DONE_DIR" unles +s $DONE_DIR =~ m|^/|; $SRC_DIR = "$VTIERBASE/pvc/ohdr/topology/$SRC_DIR" unless $SRC_D +IR =~ m|^/|; <p> I am not sure how to use $SRC_DIR here</p> $DST_DIR = "$VTIERBASE/pvc/ohdr/instance-0/processed/$DST_DIR" u +nless $DST_DIR =~ m|^/|; # DST_DIR is where we output results ......................... exit 0; }

    Please let me know if you need details

    Thanks
      I'll pester you some more (you've earned it). The not-quite-helpful code snippets in this last reply of yours raises some more questions:
      1. We told you about the apparent problem with that "foreach" line having two open-curly brackets; in the snippet above, it looks like you removed both of them. What happened as a result?
      2. We told you about the fact that when you use "foreach my $variable (...) { ... }", the $variable's scope is limited to the loop, so if you want to use that same variable name anywhere outside the loop, you need to declare it with "my" outside the loop. Have you done that?
      3. Based on the too-lengthy-but-still-incomplete code that you posted a while ago, it seems like the overall design of the script is based on handling just one $SRC_DIR value per run, and (it's not clear, but) it seems like the one value could be a command-line parameter, so you could just run the original script twice (assuming it ever worked), once for each directory. Is there some reason you can't do that?
      Well, no point going any further until you show us that you can at least answer some questions.
      It seems that you are not aware of the fact that we have been asking you questions and offering suggestions. You have not answered any of our questions, and you have not mentioned anything about our suggestions (like: whether you understood them, or tried them out, or ran into new problems after trying them out).

      So... that would be the sort of "details" we would "need" if you really expect to make any progress based on help from us.

      Here's another suggestion: PLEASE -- PLEASE -- PLEASE: Edit that terribly long post of yours above -- the one with nearly 1200 lines of code in it -- and put a <readmore> tag above the beginning of the code, and a </readmore> tag at the end of the code. I'm SERIOUS!! DO IT!!! Because it is an awful PAIN IN THE A$$ to have to scroll over all 1200 lines every time I look at this thread. Thanks in advance.

        Hi,

        I was able to fix the issue to read from two directories. I added a variable for the second directory and used that throughout my code. That simplified the effort. I have another issue. Currently, I have this directory as

        my $TOP_DIR = "$VTIERBASE/pvc/ohdr/instance-00

        where the 00 in front of instance is derived from the hostname of the VM (zldcdabcdef14-vos-fxohr-correlator-00). digits after correlator- for example, the VMs may start from 00 to 99 I need to pass this value , /instance-xx dynamically How to do this?

        Thanks,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11142973]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-03-28 12:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found