Path: utzoo!attcan!uunet!convex!convex.COM From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.lang.perl Subject: Re: constructing tables (was alarm()) Message-ID: <109679@convex.convex.com> Date: 1 Dec 90 20:23:59 GMT References: <3294@medusa.informatik.uni-erlangen.de> <109564@convex.convex.com> Sender: usenet@convex.com Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX Lines: 114 In article emv@ox.com (Ed Vielmetti) writes: >In article <109564@convex.convex.com> tchrist@convex.COM (Tom Christiansen) writes: > > Speaking of frequently asked questions, here are the new ones > for this month (I'll post RSN).... > Any other candidates or suggestions? > >A nice example of how to load data into an associative array with some >kind of <in there). Or several associative arrays, for that matter. I'd like >to have some way of taking a table > >news/readers rn,nn,trn >news/nntp nntp,nntpxmit,nntplink >gnu binutils,flex,fileutils >x11/games xtrek >lwall rn,patch,perl >ENDOFTABLE > >and turning it into > >$category{"rn"} -> "news/readers,lwall" >$category{"nntpxmit"} -> "news/nntp" >$category{"binutils"} -> "gnu" I don't think this has been asked very often, so I don't think I'll put it in the FAQ unless I see it a lot more often. I don't really envision the FAQ has a list of how to do neat things in Perl; there'll be plenty of that in the Book. It's supposed to be answers to questions about Perl that people ask again and again. What you're asked about is, nonetheless, a nice thing to know how to do, so I'll it answer here. In general, you can load associative arrays this way: %assoc = ( 'key1', 'value1', 'key2', 'value2', 'key3', 'value3', ... 'keyN', 'valueN', ); If you would like this in a <. # init while () { ($value, $keys) = split; for $key (split(/,/, $keys)) { $category{$key} = $value; } } # rest of program # blah blah exit 0; __END__ news/readers rn,nn,trn news/nntp nntp,nntpxmit,nntplink gnu binutils,flex,fileutils x11/games xtrek lwall rn,patch,perl Does that answer your question? --tom Brought to you by Super Global Mega Corp .com