Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!wuarchive!emory!gatech!usenet.ins.cwru.edu!ncoast!allbery From: allbery@NCoast.ORG (Brandon S. Allbery KB8JRR) Newsgroups: comp.lang.perl Subject: Re: apply in Perl? Message-ID: <1991Feb17.172151.20509@NCoast.ORG> Date: 17 Feb 91 17:21:51 GMT References: Reply-To: allbery@ncoast.ORG (Brandon S. Allbery KB8JRR) Followup-To: comp.lang.perl Distribution: comp Organization: North Coast Public Access Un*x (ncoast) Lines: 35 As quoted from by victor@arnor.uucp: +--------------- | foreach (1..$limit) {$a[$_ -1] = "foobar.$f";} | | Of course I'd like to produce this array without giving it a name. It | seems that grep("foobar.$_",(1..$limit)) actually evaluates these | things, and then throws them away. Is there some hack to use grep (or | maybe something else) to actually produce the array? +--------------- It's not clear to me what you're trying to accomplish. Maybe grep(push(@a, "foobar.$_"), (1 .. $limit)); which creates the same array as your foreach example. Or, more concisely, @a = grep("foobar.$_", (1 .. $limit)); (Grep returns all "true" values, meaning anything that is not either numeric 0 or an empty string, generated by the expression.) If you want to built it and act on it immediately, just use the value of the grep: foreach (grep("foobar.$_", (1 .. $limit))) { # this executes for each generated value, with the value in $_ } ++Brandon -- Me: Brandon S. Allbery VHF/UHF: KB8JRR on 220, 2m, 440 Internet: allbery@NCoast.ORG Packet: KB8JRR @ WA8BXN America OnLine: KB8JRR AMPR: KB8JRR.AmPR.ORG [44.70.4.88] uunet!usenet.ins.cwru.edu!ncoast!allbery Delphi: ALLBERY