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

Hi,

I have inherited a old perl script which I need to modify. The old script had two sets of files but in a single directory ("EDS/CUCP/SIP";). Also, the Source ($SRC_DIR) and Destination dir ($DST_DIR ) was the same. But in the new, the files are from two diff directories ("$VTIERBASE/pvc/ohdr/topology","$VTIERBASE/pvc/ohdr/instance-0/preprocess"). Also, the destination is different. How do I combine the files from two dir in the $SRC dir? I tried it as my@dir my @dir = ("$VTIERBASE/pvc/ohdr/topology","$VTIERBASE/pvc/ohdr/instance-0/preprocess"); foreach my $SRC_DIR(@dir) { But it is not working. The files are from /opt/app/vcc/pvc/ohdr/topology and /opt/app/vcc/pvc/ohdr/instance-0/preprocess for example, aaa.txt, bbb.txt are from the first dir and PM_aaa.txt, PM_bbb.txt are fro second dir.

my $VTIERBASE = "/opt/app/vcc"; ................................... #my $SRC_DIR = "EDS/CUCP/SIP"; # must prepen +d $VTIERBASE/Data/Ready if not fully specified my @dir = ("$VTIERBASE/pvc/ohdr/topology","$VTIERBASE/pvc/ohdr/ins +tance-0/preprocess"); foreach my $SRC_DIR(@dir) { my $DONE_DIR = "$VTIERBASE/pvc/ohdr/instance-0/cucp"; + # must prepend $VTIERBASE/Data/Done + if not fully specified #my $DST_DIR = "EDS/CUCP/SIP"; # must prepen +d $VTIERBASE/Data/Ready if not fully specified my $DST_DIR = "$VTIERBASE/pvc/ohdr/instance-0/processed"; my $LOG_DIR; # must prepend + $VTIERBASE if not fully specified my $LOG_DIR = "$VTIERBASE/logs/DCCC/ohdr"; my $EMAIL_DL; #my $CONFIG_FILE; my $CONFIG_FILE = "$VTIERBASE/config/ohdr"; my @feed_files; .............................
Thanks, Pragov

Replies are listed 'Best First'.
Re: To read files from two dir
by GrandFather (Saint) on Apr 11, 2022 at 20:55 UTC

    What do you mean by "combine"? Do you mean copy them into the same directory, or do you mean concatenate them into one file and put the result in some directory, or do you mean process first one then the other in the same operation?

    What do you mean by "not working". Are there error messages (if so, what - be specific), does the processor in your computer melt, or does something else happen or not happen? I can't tell where you are having trouble, but you might like to start from here:

    use strict; use warnings; my @srcDirs = ("newDir1", "newDir2"); for my $SRC_DIR (@srcDirs) { print "Source dir: '$SRC_DIR'\n"; }

    and expand that code just enough to demonstrate the issue. Don't do any more work than adding file name variables and manipulating them, then printing the result so you can see, and can show us, how you expect the file names to be handled.

    For reference, the code above prints:

    Source dir: 'newDir1' Source dir: 'newDir2'

    Show us what your equivalent code prints and tell us what you expected to get.

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

      I tried putting all the files together in one single dir,

      my $SRC_DIR         =        "/pvc/ohdr/topology";

      I did not get any errors such ” Global symbol "%lookup_F3" requires explicit package name…..” But when I tried with

      my @dir = ("$VTIERBASE/pvc/ohdr/topology","$VTIERBASE/pvc/ohdr/instanc +e-0/preprocess"); foreach my $SRC_DIR(@dir) {

      I get any errors such ” Global symbol "%lookup_F3" requires explicit package name…..” Also, I do not see any output on print statement

        You persist in giving us snippets which show none of the variables you're getting in your error messages. You were told this two days ago (Re^3: To read files from two dir).

        On what planet do you believe that repeatedly posting unrelated truncated code samples could be useful in diagnosing your problems? It's possible that because of the block structure in SOME OTHER CODE YOU HAVEN'T SHOWN that adding that new for with its BLOCK could be changing the scoping somewhere down the line and invalidating or hiding a variable declaration. But we can't know that because no one has seen enough code with supporting context so we're stuck with generalities and handwaving guesses.

        Help us help you.

        /me mutters something about porcine singing . . .

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

        A reply falls below the community's threshold of quality. You may see it by logging in.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: To read files from two dir
by graff (Chancellor) on Apr 12, 2022 at 03:00 UTC
    I know what it's like to "inherit" old code that needs updating so it can handle new conditions. And when I see error messages (like the ones you showed in your first reply to GrandFather) referring to "line 1601" and "line 1657", this tells me that the old code probably needs to be split up into smaller pieces so that it can be maintained more easily -- i.e. a relatively small "main" (executable) script that takes command-line parameters, and one or more modules containing subroutines that are used by the main script.

    Reorganizing the code that way can be a lot of work, but it'll generally be worth the effort.

    Apart from that... Do you still have the original version of the script, which presumably worked at some point in the past (and would work again if the directories and files were set up they way they used to be)? If so, check the differences between that original version and the modified version that causes the errors. The problem might be something simple, like adding declarations for variables that are being reported as not declared.

    It won't do you much good to post thousands of lines of messy old code here, so don't do that. See if you can work out for yourself what the original script was doing -- write this out as a sequence of fairly simple step-by-step sentences. Then work out how that differs from what the new version needs to do. Figure out the lines of code that handle each sentence/step, and arrange each set of lines so that you can treat it as a subroutine. (Did the original script use subroutines? Do those make sense to you?)

    Once you have an organized relation between the steps and the groups of lines that implement those steps, you'll be able to focus attention on one relatively small chunk at a time, and if you still need help, it'll be easier to show us just the part that's causing trouble. Good luck.

Re: To read files from two dir
by Ratazong (Monsignor) on Apr 12, 2022 at 07:13 UTC

    Hi Pragovnj,

    Will there be only one additional source? If so, the easiest way would be to duplicate the code accessing the directory ...

    # original part my $SRC_DIR = "EDS/CUCP/SIP"; # put real path h +ere my $DST_DIR = "EDS/CUCP/SIP"; # put real path h +ere ... all the code working on that directories # copy $SRC_DIR = "EDS/CUCP/SIP2"; # put real path +here $DST_DIR = "EDS/CUCP/SIP2"; # put real path +here ... copy of all the code working on that directories

    Of course code duplication is usually a bad idea (if you need to change something, that change needs to be done in all copies), so use it carefully. Especially, if you forsee the requirement to have more than two sources in the future.

    HTH, Rata

Re: To read files from two dir
by cavac (Prior) on Apr 12, 2022 at 11:28 UTC

    Wild guess: Quality/safety monitoring of PVC production using high pressure boilers?

    perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'