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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^5: To read files from two dir
by graff (Chancellor) on Apr 14, 2022 at 01:43 UTC
    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";

      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
      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.

Re^5: To read files from two dir
by graff (Chancellor) on Apr 14, 2022 at 01:22 UTC
    That REALLY DID NOT HELP. Apart from the fact that you tried to do exactly what I suggested that you NOT do (post thousands of lines of messy code), it appears that you did not post the entire script -- it ends abruptly in the middle of an "if" block that's nested inside a "for" loop.

    And before the perl interpreter gives up at line 1199 with the syntax error ("missing right curly..."), it reports dozens of warnings about "'my' variable ... masks earlier declaration in same scope ...".

    Note that the 1199 lines you managed to post DO NOT INCLUDE any of the line numbers cited in the error messages you've mentioned so far in this thread (1497, 1601, 1657).

    I did notice something that looks like it could be a mistake at line 125:

    foreach my $SRC_DIR(@dir) {{
    Note the two open-curly brackets -- kinda looks like a typo. Funny thing... there's a close curly bracket at line 127 (that finishes the block that starts with the second open-curly). But there's no matching close-curly for that first open-curly -- across the nearly 1000 following lines (until the end of what's posted).

    You're obviously out of your depth here. Consider some simple questions:

    • Who did you "inherit" this from? Any chance they could help?
    • Is there an earlier version that compiles (i.e. starts running, even it if crashes with some run-time error, instead of complaining about undeclared variables)?
    • If so, what's the difference between that version and the version you posted unsuccessfully here? (HINT: use the unix/linux "diff" command)

    You may think we're ganging up on you and giving you a hard time, but we're just trying to get you to do something with some hint of competence.

Re^5: To read files from two dir
by GrandFather (Saint) on Apr 14, 2022 at 01:27 UTC
    foreach my $SRC_DIR(@dir) {{

    looks dubious.

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