creating variables on the fly (newbie question)

Curtis Poe cp at onsitetech.com
Thu Jan 11 18:45:37 CST 2001


How you populate variables depends greatly upon how you intend to use them.
Here's one example that reads your file into a hash of arrays.  An array of
arrays might be more appropriate, depending upon what you want to do with
the data.
_______________________________
use strict;
use warnings;

my %input_data;
my $file = "somedata.txt";
my $item;

open DATA, "<$file" or die "Can't open $file: $!";
while (<DATA>) {
    chomp;
    if ( /^item/ ) {
        $item = $_;
        $input_data{ $item } = []; # Create an array ref
        next;
    }
    # Note that the regex here is just a rough hack.  Some
    # of your data has a period at the end and I'm
    # assuming that you don't want that.
    push @ { $input_data{ $item } }, $1 if /^(0\.\d+)/;
}
close DATA;
_______________________________

Then, to access the second element of 'item 1', you would use something like
this:

    $input_data{ 'item 1' }->[1];

Of course, this is just a rough guide and may not be suitable for your
needs.  Also, note that if more than one process is likely to use that file
at a time, you should use 'flock'.

Cheers,
Curtis


-----Original Message-----
From: owner-pdx-pm-list at pm.org [mailto:owner-pdx-pm-list at pm.org]On
Behalf Of Tom Keller
Sent: Thursday, January 11, 2001 1:35 PM
To: pdx-pm-list at pm.org
Subject: creating variables on the fly (newbie question)


I would like to create a new array for each grouping of numbers within a
simple file:
The file looks like this:
item 1
0.1
0.12.
0.14
0.16
0.1

item 2
0.13
0.14.
0.1
0.15

item 3
0.04
0.03
0.05
0.04
--------------------
So I want @item1 = (0.1, 0.12, 0.14, 0.16, 0.1  @item2=(0.13, 0.14, 0.1,
0.15)  etc.
This seems so simple, but I'm stuck.
Note the number of items and the number of values for each item are unknown.
Thanks,
Tom Keller

Thomas J. Keller, Ph.D.
Oregon Health Sciences University
MMI Core Facility
503-494-2442
kellert at ohsu.edu
http://www.ohsu.edu/core
TIMTOWTDI

TIMTOWTDI



More information about the Pdx-pm-list mailing list