in reply to duplicate $_ while reading files; my compactness; code structure

Yes the map and grep functions are your friends (good friends) -
my @some = grep { !/^\s*$/ } (my @most = map { s/#.*$//; $_ } <FILE>);
Although this does make assumptions like comments not being in the middle of quotes and the like, otherwise you'll want some slightly fancier parsing than the above. Also, did you *just* want to get rid of the hashes (like you say), or the hashes *and* the comments (like you code)?
HTH

broquaint

Update: yes running the code first might be an idea... now works proper.
Update 2: sometimes they come back... got rid of @all as I didn't *really* need it, as merlyn indirectly illustrates below :o)

  • Comment on Re: duplicate $_ while reading files; my compactness; code structure
  • Download Code

Replies are listed 'Best First'.
Re: Re: duplicate $_ while reading files; my compactness; code structure
by merlyn (Sage) on Oct 03, 2001 at 21:33 UTC
    my @all = <FILE>; my @some = grep { !/^\s*$/ } (my @most = map { s/#.*$//; $_ } @all);
    Beware... that last map alters @all. It's very misleading.

    If you're going to "copy and change", a nice idiom (which I've talked about here in the past) is:

    s/#.*$// for my @most = @all;
    Although this isn't actually correct in the context of the original question. Just posting a better way to do what you ended up accidentally doing.

    -- Randal L. Schwartz, Perl hacker