Three questions in one node for you wise old owls today, so make sure you upvote this three days running ;)
1) Say I want to read a file which may have some lines commented out. I want one array with every line in it, whether it's commented out or not (and the '#' removed if it is), and one array with every line which isn't commented out in it, with any comments after the data being removed. This would be explained better like so:
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
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?)
A while loop is probably the sensible solution:
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
But that feels horrible also. How would you do it?
2) I have this line that I was rather proud of:
warn("$0 started running at ", scalar(localtime()), ' with switches ', defined(my $switches_text = join ', ', keys %switches) ? $switches_text : "(none)", "\n");
Fails, though. Apparently I need to declare $switches_text with my earlier, because it works if I put a
my $switches_text on the line above. Anyone got any idea why? I was hoping I could fit it all onto one line.
3) What's the best way to structure the code in the main scope. Should it be a series of subroutine calls, which then abstract further? Or should there be a lot of code in the main scope? I feel it might help maintainability if most of the code was in subs.
Thanks for any aid.
--
my @words = grep({ref} @clever);
sub version { print "I cuss your capitalist pig tendencies bad!\n" }
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.