creating variables on the fly (newbie question)

Joshua Keroes jkeroes at eli.net
Thu Jan 11 17:57:33 CST 2001


++ 11/01/01 13:35 -0800 - Tom Keller:
> I would like to create a new array for each grouping of numbers within a simple file:
> The file looks like this:
> [snip]
> So I want @item1 = (0.1, 0.12, 0.14, 0.16, 0.1  @item2=(0.13, 0.14, 0.1, 0.15)  etc.
> Note the number of items and the number of values for each item are unknown.

I would use an AoA (see perldoc perllol)

@AoA = (
	[ .1, .12, .14, .16, .1 ],
	[ .13, .14, .1, .15],
	...etc...
);

Once this is generated in memory, you can dump it to your simple file
using Data::Dumper. Reloading is trivial, just "do $file" and a little
error checking. (perldoc -f do)

While you could create variables with differing names as in your example
above, this is actually a bad idea. The reasons are illustrated at:

http://www.plover.com/~mjd/perl/varvarname.html
http://www.plover.com/~mjd/perl/varvarname2.html
http://www.plover.com/~mjd/perl/varvarname3.html

+--------------------------------------------------------------------+
|   Joshua Keroes, Sr. Software Engineer,  Electric Lightwave, Inc   | 
+--------------------------------------------------------------------+
  "To be creative, it's not necessary that you're seeing a new thing,
       but that you're looking at the same things in a new way."
       Dieter Gruen, ultrananocrystalline diamond film inventor
TIMTOWTDI



More information about the Pdx-pm-list mailing list