Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How can I enter the data of different variables from a text file into a perl program?

by supriyoch_2008 (Scribe)
on May 05, 2012 at 19:01 UTC ( #969069=perlquestion: print w/ replies, xml ) Need Help??
supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perlmonks,

I have a text file (data.txt) having the values of a few variables as given below:

x1=3.8; x2=5.8; x3=4.1; y1=10.1; y2=11.6; y3=12.7

I am interested to enter these data in a perl program for doing further calculations like x1*y1, x2*y2, x3*y3 or (x1*y1)/x2 etc. Which perl syntax/chapter should I read so that I can enter the values of the variables from the data file (data.txt) into the perl program for doing further calculations?

Comment on How can I enter the data of different variables from a text file into a perl program?
Download Code
Re: How can I enter the data of different variables from a text file into a perl program?
by davido (Archbishop) on May 05, 2012 at 19:25 UTC
Re: How can I enter the data of different variables from a text file into a perl program?
by moritz (Cardinal) on May 05, 2012 at 19:38 UTC

    I happen to have written a module which understands that syntax. You could use it like this:

    use strict; use warnings; use 5.010; use Math::Expression::Evaluator; my $m = Math::Expression::Evaluator->new; # read all the variables $m->parse("x1=3.8; x2=5.8; x3=4.1; y1=10.1; y2=11.6; y3=12.7")->val; # do the calculations you are interested in: say $m->parse("x1 * x2")->val;

    Not saying it's the best solution, it just happens to work :-)

Re: How can I enter the data of different variables from a text file into a perl program?
by ww (Bishop) on May 05, 2012 at 19:53 UTC

    open; split and on-and-on....

    or, if your learning style is compatible with examples...check Super Search for threads here dealing with the issues you face, and see also http://www.perlmonks.com/index.pl?node=Tutorials#Input-and-the IO tut in our Tutorials section.

    But a couple points about your question:

    1. Is the data sample you posted an example of a single line (record) in data.txt or are each of the label:value pairs on a separate line or something else entirely? (Implicit hint made explicit: your questions will generally get quicker and better help if your problem statement is precise.)
    2. Your question suggests that you might hope to learn Perl by studying the syntax of some functions. That's probably sub-optimal. The "chapters" element is likely a better route, and which text to recommend depends (to some extent) on the extent your prior experience as a programmer.
      But without that info, it's hard to do better than recommending Learning Perl, 6th ed. But beware: the first several hits if you simply Google the title appear to be UNauthorized ... ie, ripoffs of the authors and O'Reilly.

    Edits for fact, spelling and references. Aaagh!

Re: How can I enter the data of different variables from a text file into a perl program?
by JavaFan (Canon) on May 05, 2012 at 22:20 UTC
    use 5.010; use strict; use warnings; $_ = `cat data.txt`; s/\b[a-z]/\$::$&/g; eval; no warnings 'once'; say $::x1 * $::y1; say $::x2 * $::y2; say $::x3 * $::y3; say +($::x1 * $::y1) / $::x2; __END__ 38.38 67.28 52.07 6.61724137931034483
    I bet there will be people complaining about this working program. ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://969069]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-04-19 08:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (478 votes), past polls