Using variables to store statements/formulae in perl

From: Anthony Murphy (AMURPHY_at_cpstg.com)
Date: 09/29/04

  • Next message: Gavin Henry: "Re: Becoming Disenheartened - Everyone talks about Python and says Perl is old news."
    Date: Wed, 29 Sep 2004 16:16:15 +1000
    To: <beginners@perl.org>
    
    

    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?

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

    -- Code Ends --

    Thanks,

    Anthony Murphy


  • Next message: Gavin Henry: "Re: Becoming Disenheartened - Everyone talks about Python and says Perl is old news."

    Relevant Pages

    • Re: Using variables to store statements/formulae in perl
      ... > Hello Perl Beginners, ... > telephone system and presents some statistics based on it. ...
      (perl.beginners)
    • Re: From a seriers of data (x,y) to bell curve (normal distribution)
      ... want to get a bell curve from this data with perl. ... first you want to analyze the data and determine the bell curve ... You can draw a bar chart with off-the-shelf modules on ... statistics, the statistics experts are busy elsewhere, reading their ...
      (perl.beginners)
    • Re: Perl - Statistics and AI
      ... I think these two categories will inspire excellent Perl programmers ... I'm an engineer, BS. ... aspect of statistics, numerical methods, or artificial intelligence do ...
      (comp.lang.perl.misc)
    • Perl - Statistics and AI
      ... It would be nice to see, and draw in, some expertise in statistics and AI ... I think these two categories will inspire excellent Perl programmers ... I'm an engineer, BS. ... bought the white ROM KERNEL manuals to with it. ...
      (comp.lang.perl.misc)