saving and loading variables to/from a file

Can someone suggest the best way to save and load variables with files in PERL? I have to admit I'm furstrated- I know just how "I" would do it in C/C++, but PERL is new to me. Here's what I'm looking for:

THere'd be a series of these, i.e. an array of this type that had both a counter name and associated value.

First I need a way to create the following:

Counter[0]->name = "main counter"
Counter[0]->value = 4587
Counter[1]->name = "free site"
Counter[1]->value = 10562

I'm not sure how to create a "user defined" type array like I have above in PERL.

Second a way to save this and then re-load it. I can read text from a file, but I can't figure out how to "parse" it into the appropriate variables.

I'm happy to help with any questions you may have on C/C++ or VB development- I'm well versed in those areas, but need some help with PERL.

thanks a lot,
-Andrew

Comments

  • My C/C++ knowledge == 0, but I think what you are after is a named hash:

    %Counter = (
    'main counter' => 4587,
    'free site' => 10562
    );

    You access the values like so:

    $Counter{'main counter'}
    $Counter{'free site'}

    You have to quote the key names because they have spaces in them.
    If you wanted to increment them just use the standard ++

    $Counter{'main counter'}++; (now equals 4588)

    if you had to find the correct value to increment and had the key name alreay in a variable:

    $string = 'main counter';
    $Counter{$string}++

    and so on :)
  • : Can someone suggest the best way to save and load variables with files in PERL? I have to admit I'm furstrated- I know just how "I" would do it in C/C++, but PERL is new to me. Here's what I'm looking for:
    :
    : THere'd be a series of these, i.e. an array of this type that had both a counter name and associated value.
    :
    : First I need a way to create the following:
    :
    : Counter[0]->name = "main counter"
    : Counter[0]->value = 4587
    : Counter[1]->name = "free site"
    : Counter[1]->value = 10562
    :
    : I'm not sure how to create a "user defined" type array like I have
    : above in PERL.
    :
    Use a hash data structure to map names to values, then hold an array of hashes. Note that you don't specify the keys "up front" like in a struct.

    # Create array.
    my @counter;

    # Initialise each array entry to an empty hash
    $counter[$_] = {} for 0..1;

    # Store stuff like this:-
    $counter[0]->{name} = "main counter";
    $counter[0]->{value} = 123;
    # etc

    : Second a way to save this and then re-load it. I can read text from a
    : file, but I can't figure out how to "parse" it into the appropriate
    : variables.
    :
    The easiest way to store a complex data structure of any kind is to serialize it. Check out the Storable module.
    http://search.cpan.org/~ams/Storable-2.13/Storable.pm

    : I'm happy to help with any questions you may have on C/C++ or VB
    : development- I'm well versed in those areas, but need some help with
    : PERL.
    :
    You're coming from a bunch of statically typed languages to a dynamically typed one. That alone may feel weird for a while. :-)

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • : : Can someone suggest the best way to save and load variables with files in PERL? I have to admit I'm furstrated- I know just how "I" would do it in C/C++, but PERL is new to me. Here's what I'm looking for:
    : :
    : : THere'd be a series of these, i.e. an array of this type that had both a counter name and associated value.
    : :
    : : First I need a way to create the following:
    : :
    : : Counter[0]->name = "main counter"
    : : Counter[0]->value = 4587
    : : Counter[1]->name = "free site"
    : : Counter[1]->value = 10562
    : :
    : : I'm not sure how to create a "user defined" type array like I have
    : : above in PERL.
    : :
    : Use a hash data structure to map names to values, then hold an array of hashes. Note that you don't specify the keys "up front" like in a struct.
    :
    : # Create array.
    : my @counter;
    :
    : # Initialise each array entry to an empty hash
    : $counter[$_] = {} for 0..1;
    :
    : # Store stuff like this:-
    : $counter[0]->{name} = "main counter";
    : $counter[0]->{value} = 123;
    : # etc
    :
    : : Second a way to save this and then re-load it. I can read text from a
    : : file, but I can't figure out how to "parse" it into the appropriate
    : : variables.
    : :
    : The easiest way to store a complex data structure of any kind is to serialize it. Check out the Storable module.
    : http://search.cpan.org/~ams/Storable-2.13/Storable.pm
    :
    : : I'm happy to help with any questions you may have on C/C++ or VB
    : : development- I'm well versed in those areas, but need some help with
    : : PERL.
    : :
    : You're coming from a bunch of statically typed languages to a dynamically typed one. That alone may feel weird for a while. :-)
    :
    : Jonathan
    :
    : ###
    : for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    : (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    : /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
    :
    :

    THanks so much, that's a lot of great information, I really appreciate it.

    -Andrew
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories