Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!bu.edu!nntp-read!composer From: composer@chem.bu.edu (Jeff Kellem) Newsgroups: comp.lang.perl Subject: Re: Learning perl -- generlized file loading w/var assignment Message-ID: Date: 16 Feb 91 15:14:23 GMT References: <16785@venera.isi.edu> Sender: news@bu.edu.bu.edu Reply-To: composer@chem.bu.edu Distribution: comp Organization: Boston University Chemistry Department Lines: 75 In-reply-to: jas@ISI.EDU's message of 15 Feb 91 19:04:40 GMT In article <16785@venera.isi.edu> jas@ISI.EDU (Jeff Sullivan) writes: > To that end, I have created a set of files which each contain a list > of commands or terms which I want the user to be able to abbreviate. > I then want to be able to read in this list of files and assign each > file its own associative array filled out by abbrev.pl. However, my > test doesn't seem to work. > > Here's a sample of the code: [ ..deleted.. ] > #Set abbreviation eval line (run for each abbr file.). > #$filestub will be set to the root of the file name, which is always > #of the form -LIST. > $eval_line = "%$filestub = ();\n&abbrev(*$filestub, @tmp);\n"; First off, there are a few problems with the above $eval_line. Since you've enlosed the RHS of the $eval_line assignment in double quotes, the $filestub, and @tmp will be evaluated immediately. Since you haven't assigned anything to $filestub yet (and you want to use the name @tmp as an arg) they get replaced by their current contents, which happen to be nothing at this point. When creating code on the fly, it's always a good idea to add: print $eval if $debug; before the actual eval. Anyways, you probably want to move the $eval_line assignment into the "foreach $filename" loop after the $filestub assigment. Also, you don't need the newlines in this case. So, with double quotes, the above should be written as: $eval_line = "%$filestub = (); &abbrev(*$filestub, \@tmp);"; You want the "@tmp" to stay, as is, so you need to escape the @-sign. > #Load abbreviation files. > > foreach $filename ("ATTR-LIST", "HELP-LIST", "TYPE-LIST", "CMD-LIST") { > [ ..deleted.. ] > $filename =~ /(\w+)-LIST/; > $filestub = $1; [ ..deleted.. ] > while (<>) { > chop; > $long = $cmd{$_}; > print "$long\n" if length($long); > } > > The last bit is pulled vrom the example using abbrev.pl. The $cmd{} > array (should) be created by the eval_line when $filename is > "CMD-LIST" and $filestub is "CMD". Right? Except that variable names are case-sensitive. So, assuming the code worked, you would be producing an associative array named %CMD, instead of %cmd. If you want to lowercase the part of $filename you're trying to grab, there are a bunch of ways to do it. Here's one: ($filestub = $filename) =~ s/^(\w+)-list/(($lc = $1) =~ tr|A-Z|a-z|),$lc/ie; Okay, maybe you want something a little easier to read for the beginner.. ;-} Following your start: $filename =~ /(\w+)-LIST/i; ($filestub = $1) =~ tr/A-Z/a-z/; You may want the i on the end of the match, //i, so you can match CMD-LIST and CMD-list and so on... in otherwords, the match ignores case. Hope that helps a little...Enjoy Perl! -jeff Jeff Kellem Internet: composer@chem.bu.edu