Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Traversing through files to read a variable

by anirudh.agarwal (Novice)
on Dec 18, 2013 at 05:38 UTC ( #1067588=perlquestion: print w/ replies, xml ) Need Help??
anirudh.agarwal has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am stuck up with an issue. I have a variable $path which has the path to a file, say /usr/hello.txt Now /usr/hello.txt file has path to other files. Example: The contents of /usr/hello.txt is as follows
view_cfg_dir = /usr/TextPass/cfg/ url = http://localhost/tpx user = statview pwd_md5 = 93211cdee930eb4e1f7d36558a361145
Now there is another file in

/usr/TextPass/cfg/

namely export.cfg whose contents are as below
pid = /var/hello/pid report_cfg = /var/_export/config report_tmp = /var/_export/tmp
I want the value of report_cfg to be stored in a variable. How do I implement this functionality. Your help is highly appreciated.

Comment on Traversing through files to read a variable
Select or Download Code
Re: Traversing through files to read a variable
by NetWallah (Abbot) on Dec 18, 2013 at 06:12 UTC
            How do I implement this functionality.

    Smells like homework.

    You can implement this by writing a program, preferably in perl, to OPEN a file, READ the contents, and PARSE it (the split function is useful here).

    Once parsed, you can assign the components to variables as necessary. Open additional files based on the information in the variables.

    If, after you have read the documentation, and tried to implement this, you have difficulty with some portion of it, please return here , and explain what you have tried, post the code, even if it does not work, explain what you expect it do do, and we will be glad to assist you with it.

                 When in doubt, mumble; when in trouble, delegate; when in charge, ponder. -- James H. Boren

Re: Traversing through files to read a variable
by marinersk (Chaplain) on Dec 19, 2013 at 00:42 UTC

    Step 1: Write a script to read all the lines from /usr/hello.txt. Keywords to research: open, close, while, chomp. Recommend using syntax like while (my $inputLine = <INPUT_FILE>) and chomp $inputLine;

    Step 1.9: Decide if you want to act on each line as you read it, or if you want to slurp them all into memory and then loop through those. Note: Slurp technique will require familiarity with arrays and foreach.

    Step 2: Write the code to read all the lines from the secondary (apparently export.cfg) files.

    Step 2.9: Decide if you want to act on each line of these secondary files as you read them, or if you want to slurp them all into memory and then loop through those. Note: Slurp technique will require familiarity with arrays and foreach.

    Step 3: As you go through the lines of those secondary files, extracting the report_cfg information is a Perl speciality (it's more or less what the 'E' stands for in the name "Perl", incidentally). As noted by others, use of the split statement is likely to be helpful here.

    Is that enough to get you going?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1067588]
Approved by NetWallah
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-09-17 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (61 votes), past polls