Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Perl & COBOL

by kunalrc (Initiate)
on Sep 03, 2013 at 18:01 UTC ( #1052160=perlquestion: print w/replies, xml ) Need Help??
kunalrc has asked for the wisdom of the Perl Monks concerning the following question:

Hi,i have a list of variables and its associated names in a file and I need to change the contents of all the variables with these associated names in abnother file. How can i achieve this. I am new to Perl. Thanks

Replies are listed 'Best First'.
Re: Perl & COBOL
by kcott (Chancellor) on Sep 03, 2013 at 18:52 UTC

    G'day kunalrc,

    Welcome to the monastery.

    Unfortunately, what you've written doesn't make too much sense. For instance, what does "a list of variables and its associated names" mean? A variable, say $x, has the name $x: that's probably not what you're referring to. Perhaps you're talking about hash keys, references ... I'm really just making wild guesses; you need to tell us.

    Read "How do I post a question effectively?", then, following its guidelines, update your original post. [See "How do I change/delete my post?" if you don't know how to do that.]

    The title you used, "Perl & COBOL", seems to have no bearing on the question you're asking; it was certainly no help at all in trying to understand what your question meant. Read "How do I compose an effective node title?" and then, when updating your question, update the title also.

    When you've done all of that, you're much more likely to get useful answers to whatever it is that you're enquiring about.

    -- Ken

Re: Perl & COBOL
by marinersk (Priest) on Sep 03, 2013 at 21:54 UTC
    This is a WAG (Wild Donkey Guess):

    1. You have some COBOL source files
    2. You want to globally change some variable names in these files
    3. You have a list of current and new names for these variables in a file
    4. You want to use Perl to drive the global change-and-replace operation more intelligently than conventional tools can give you

    How close am I?

Re: Perl & COBOL
by NetWallah (Abbot) on Sep 03, 2013 at 23:06 UTC
    One way to clarify what you mean is to give EXAMPLES.

    If you show a few lines from each file (names file, COBOL file), then explain what you want changed, and how the final output should look, we can help you.

    We would be more inclined to help if you actually attempted to write perl code to generate the desired output, and had an actual perl-related problem with it, and explained what that issue was.

                 My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

Re: Perl & COBOL
by Laurent_R (Canon) on Sep 03, 2013 at 18:56 UTC

    If you need to change a series of words by other words in a file, then say it. That is as much as I could guess from your message. If it is not what you want, then I guessed incorrectly, please explain further. And, yes, what is the relation with COBOL?

Re: Perl & COBOL
by marinersk (Priest) on Sep 04, 2013 at 10:14 UTC
    Updated to add <readmore> tags.
    Updated to fix <readmore> tags. (Apparently I'm in noob mode today)
    Updated to re-fix <readmore> tags. (Apparently I'm in serious noob mode today)

    Okay, I'll take a stab at bridging the gap.

    To understand this code you'll need to study up on the following Perl subjects:

    • strict (critically useful in disciplined Perl programming)
    • split (this is how I break out the translation table)
    • Arrays (@ARGV is an array)
    • Hashes (%TranslationTable is a hash)
    • File I/O (open, close, print, and reading text files via <FILE>)
    • Regular Expressions (quotemeta, and the use of the s/// substitution feature)
    • Processing the command line (@ARGV contains the command line parameters)

    Presuming my assessment of what you want to do is correct, we have some files:

    Directory of C:\Steve\Dev\PerlMonks\P-2013-09-04@0259-Perl-COBOL-Gues +s 09/04/2013 03:59 AM <DIR> . 09/04/2013 03:59 AM <DIR> .. 09/04/2013 03:37 AM 1,788 TEST1.COB 09/04/2013 03:53 AM 1,301 09/04/2013 03:38 AM 144 varchange.txt 3 File(s) 3,233 bytes 2 Dir(s) 53,636,513,792 bytes free
    The file varchange.txt contains our variable translation data:
    The TEST1.COB file has our COBOL source code:

    And the file has our rather wordy and probably inefficient Perl script:

    #!/usr/bin/perl -w use strict; # For brevity, there is no error checking here. # In Real Life[TM], you should, of course, check for errors. my %TranslationTable = (); # Load Translation Table { open TRANSLATION_FILE, "<varchange.txt"; while (my $trnbuf = <TRANSLATION_FILE>) { chomp $trnbuf; my ($currentVariableName, $newVariableName, @garbageData) = sp +lit /\:/, $trnbuf; $TranslationTable{$currentVariableName} = $newVariableName; } close TRANSLATION_FILE; } # Non-destructively modify files specified on command line foreach my $inputFilename (@ARGV) { my $outputFilename = $inputFilename . "-NEW.COB"; open INPUT_FILE, "<$inputFilename"; open OUTPUT_FILE, ">$outputFilename"; while (my $inputBuffer = <INPUT_FILE>) { my $outputBuffer = $inputBuffer; foreach my $currentVariableName (keys %TranslationTable) { my $newVariableName = $TranslationTable{$currentVariableNa +me}; # Your logic will likely be more intense here # For example, how to confirm you are only modifying varia +ble references? my $translationRegex = quotemeta $currentVariableName; $outputBuffer =~ s/$translationRegex/$newVariableName/ig; } print OUTPUT_FILE $outputBuffer; } close INPUT_FILE; close OUTPUT_FILE; } exit; __END__
    Running the script produces:

    Good luck, and holler if this is nothing like what you were looking for.

    Based on certain grammatical constructs in your original post, I am going to guess English is not your native language, so we might have to ask a lot of questions of each other to get past communication problems. I promise I'll do my best if you promise to be patient with me.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1052160]
Approved by kcott
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2017-10-17 21:06 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (238 votes). Check out past polls.