Amoe has asked for the wisdom of the Perl Monks concerning the following question:
That, however, feels horrible, and doesn't take account of blank lines (ideally they would not be added to the array at all). (Aside: Also, isn't map supposed to return a list, so shouldn't I be able to do @all_lines = map { s/^#// } @conditional_lines and that would copy the array with the comments removed in one go, rather than having to explicitly = copy it?)my @conditional_lines = <DATA>; my @all_lines = @conditional_lines; chomp(@all_lines); chomp(@conditional_lines); map { s/^#// } @all_lines; map { s/#.*$// } @all_lines; map { s/#.*$// } @conditional_lines; print @all_lines; print @conditional_lines; __DATA__ #my commented out line a standard line a line with a # comment at the end
But that feels horrible also. How would you do it?while (<DATA>) { chomp(); my $copy = $_; $copy =~ s/^#//; $copy =~ s/#.*$//; push(@all_lines, $copy); s/#.*$//; next unless ($_); push(@conditional_lines, $_); } print @all_lines; print @conditional_lines; __DATA__ #my commented out line a standard line a line with a # comment at the end
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: duplicate $_ while reading files; my compactness; code structure
by merlyn (Sage) on Oct 03, 2001 at 20:22 UTC | |
|
Re: duplicate $_ while reading files; my compactness; code structure
by merlyn (Sage) on Oct 03, 2001 at 20:30 UTC | |
|
Re: duplicate $_ while reading files; my compactness; code structure
by Fletch (Bishop) on Oct 03, 2001 at 20:46 UTC | |
|
Re: duplicate $_ while reading files; my compactness; code structure
by broquaint (Abbot) on Oct 03, 2001 at 20:50 UTC | |
by merlyn (Sage) on Oct 03, 2001 at 21:33 UTC | |
|
Re: duplicate $_ while reading files; my compactness; code structure
by BrentDax (Hermit) on Oct 05, 2001 at 11:13 UTC |