[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] reading variables from files




>
>
>Maybe you could define your own tokens and substitute them in while
>you read the text.
>
>template file:
>I'm going to print out %%myVar%%
>
>code:
>%replacementtext = ('myVar', 'Hi there', 
>                    'replace_this', 'something else');
>
>open(MYINPUT,$myFile) || die "Can't open $myFile: $!\n";
>while(<MYINPUT>) {
>  s/%%(\w+)%%/$replacementtext{$1}/g;
>  print;
>}
>
>or with a little bit of error checking
>while(<MYINPUT>) {
>  while(s/%%(\w+)%%/$replacementtext{$1}/) {
>    warn "What in the world is  %%$1%%\n" unless exists $replacementtext{$1};
>  }
>  print;
>}

This thread in the list applies exactly to something that
I'm working on right now. However, instead of having a
template file and code (as above), I have a template file,
a variable file and code.

In other words, all this stuff above works great, but I
also need to know how to read a file with keys and
values into an associative array (as well as write back
to the file).

For example, the task I'm trying to do is keep track of
the number of people who score certain amounts on a
test. Each time another person takes the test (it is
web based), they get their score, in addition to a list
of the ratings and the number of people who have scored
in each rating to date.

The data file looks like this (though the format could
be different if necessary):

carnivore,12,white meat only,23,vegitarian,10,vegan,2

How do I read this in and turn it into an associative
array? My current code looks like this, but it doesn't
seem to create anything in the array:


$countfile = "testcount";

open (COUNTFILE, "$countfile") || die "Content-Type: text/html\n\nError!
Can't open $countfile! Consult the administrator!\n";


# Parse through countfile, reading data into
# associative array

while (<COUNTFILE>)

        {
        

# Since all the data should be in one line,
# chop off the trailing EOF character and
# read this in as an assoc. array

chop;


%countArray = $_;



 }


But I get no luck...the array %countArray is
empty!

Sigh.

Thoughts?

Brant


   . . . . . . . . . . . . . . . . . . . . . .
   .                                         .
   . Imagesmith                              .
   . Makers of Interesting Stuff             .
   .                                         .
   . http://www.imagesmith.com/imagesmith/   .
   . . . . . . . . . . . . . . . . . . . . . .