Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Using variables to store statements/formulae in perl
Hello Perl Beginners,

I'm writing a program in perl that collects data about calls into a
telephone system and presents some statistics based on it.  There could
come a time in the future where different data needs to be used and
different statistics need to be reported on so I'm trying to keep the
entire thing as generic as possible.  I have a conf file telling me what
data represents what statistics and what formula needs to be used to get
that data.

Then I began to wonder if I store the formulae in variables then how do
I get them out again and get perl to use them as statements?

Here is some example code I wrote while trying to figure this out, any
help would be appreciated.  Is this even a good way to do this?=20

-- Code Starts --

use strict;
use diagnostics;

# Data and Formula will eventually be read from a conf file and there
may be multiple=20
# instances of them that need to stay grouped and indexed so they're in
a hash.

my (%data, %formula);

# Dummy Test Data

$data{"ext1"} =3D 12;
$data{"ext2"} =3D 9;
$data{"ext3"} =3D 10;

# Dummy Test Formula

$formula{"Addition"} =3D "ext1 + ext2";
$formula{"Subtraction"} =3D "ext3 - ext2";
$formula{"Brackets"} =3D "(ext2 + ext3) - ext1";

# I can quite easily print these out and I could put the $data{"extX"}
in with a=20
# regular expression but how do I get it to evaluate the variable as if
it=20
# were an expression?

print $formula{"Addition"} . "\n";
print $formula{"Subtraction"} . "\n";
print $formula{"Brackets"} . "\n";

-- Code Ends --

Thanks,

Anthony Murphy

Report this thread to moderator Post Follow-up to this message
Old Post
Anthony Murphy
09-29-04 09:58 AM


Re: Using variables to store statements/formulae in perl
> Hello Perl Beginners,
>
> I'm writing a program in perl that collects data about calls into a
> telephone system and presents some statistics based on it.  There could
> come a time in the future where different data needs to be used and
> different statistics need to be reported on so I'm trying to keep the
> entire thing as generic as possible.  I have a conf file telling me what
> data represents what statistics and what formula needs to be used to get
> that data.
>
> Then I began to wonder if I store the formulae in variables then how do
> I get them out again and get perl to use them as statements?
>
> Here is some example code I wrote while trying to figure this out, any
> help would be appreciated.  Is this even a good way to do this?
>
> -- Code Starts --
>
> use strict;
> use diagnostics;
>
> # Data and Formula will eventually be read from a conf file and there
> may be multiple
> # instances of them that need to stay grouped and indexed so they're in
> a hash.
>
> my (%data, %formula);
>
> # Dummy Test Data
>
> $data{"ext1"} = 12;
> $data{"ext2"} = 9;
> $data{"ext3"} = 10;
>
> # Dummy Test Formula
>
> $formula{"Addition"} = "ext1 + ext2";
> $formula{"Subtraction"} = "ext3 - ext2";
> $formula{"Brackets"} = "(ext2 + ext3) - ext1";
>
> # I can quite easily print these out and I could put the $data{"extX"}
> in with a
> # regular expression but how do I get it to evaluate the variable as if
> it
> # were an expression?
>

perldoc -f eval

Depending on where your data is coming from it might be advisable to
read through

perldoc perlsec


> print $formula{"Addition"} . "\n";
> print $formula{"Subtraction"} . "\n";
> print $formula{"Brackets"} . "\n";
>
> -- Code Ends --
>
> Thanks,
>
> Anthony Murphy

HTH,

http://danconia.org

Report this thread to moderator Post Follow-up to this message
Old Post
Wiggins d Anconia
09-29-04 03:45 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:39 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.