Re: Adding '#' in the beginning of each line
by davorg (Chancellor) on Aug 16, 2006 at 08:47 UTC
|
You should only have to add one line and modify another.
This strongly implies that you have been given an existing program to modify. It's hardly fair to expect us to do the work without giving us the same head start that you had.
Use the $" variable.
This is perhaps the most puzzling part of the question. I'm not sure how using $" is ever going to make this task easier. The obvious solution is something like:
print "# $_" while <>;
And even a solution that uses $" like:
my @file = <>;
$" = '#';
print "@file";
will omit the # from the first line of the output.
I strongly suspect that whoever set this homework doesn't really know that they are doing.
Unexpected things can happen with files, so you may find it helpful to use the -w option as mentioned in the section on running Perl programs.
This is more evidence that the question was written by someone with very little knowledge of Perl. The -w option was surplanted by "use warnings" six years ago. You should only be taught about -w as an item of historical interest.
If you really want to learn Perl then give up this course and buy a good book.
--
< http://dave.org.uk>
"The first rule of Perl club is you do not talk about
Perl club." -- Chip Salzenberg
| [reply] [d/l] [select] |
|
my @file = <>;
$" = '#';
print "#@file";
# ^^^
but that just adds obscurity to an already obscure solution. So I agree with your assessment of the homework-giver.
On a funny sidenote, something like the following also uses $" :)
$" = '#';
print $", $_ while <>;
| [reply] [d/l] [select] |
Re: Adding '#' in the beginning of each line
by Corion (Patriarch) on Aug 16, 2006 at 07:56 UTC
|
You've been pointed to nodes that detail that homework questions are not liked here.
I found it a neat challenge to write a program that does what you want but is a bit opaque in what it does:
#!/opt/perl/bin/perl -w
no strict;
no warnings;
# Handle data by prefixing
# the string with "#". The string
# is passed in implicitly via $_
sub handle_data { do{eval}->() };
$/ = "";
while (<DATA>) {
handle_data;
};
__DATA__
# This program was written by Corion
for # Anonymous Monk - http://www.perlmonks.org
( # see http://perlmonks.org/?node_id=567615
sub { $/ = "\n" },
sub { *DATA = *ARGV; $(})
{$_->()};
sub { *::handle_data = sub { substr $_, $(,$), "#"; print }
};
| [reply] [d/l] |
Re: Adding '#' in the beginning of each line
by Sartak (Hermit) on Aug 16, 2006 at 07:19 UTC
|
Well, in the real world, I'd do it like so:
perl -ple 's/^/#/'
Whether your instructor has other goals in mind is something else entirely...
| [reply] [d/l] |
|
perl -pe "print '#'"
| [reply] [d/l] |
Re: Adding '#' in the beginning of each line
by GrandFather (Saint) on Aug 16, 2006 at 07:23 UTC
|
| [reply] |
Re: Adding '#' in the beginning of each line
by castaway (Parson) on Aug 16, 2006 at 09:25 UTC
|
This seems like a fairly pointless exercise, here's how I'd solve it:
- Open the file in the emacs editor
- Hit ctrl-home to get to the top
- Hit ctrl-space to start marking a region
- Hit ctrl-end to get to the end
- Hit ESC-x to enter a command
- type comment-region and hit return
I bet someone'll tell me theres a "comment-buffer" now!
Caveat, only works on modes where emacs thinks the comment-char is "#".
C. | [reply] |
|
Using Komodo I'd:
although that may not place the # as the first character of each line - it tends to follow indentation and will be the first non-whitespace character on each line
Update Actually ctrl-3 does place a comment character at the start of each selected line. I was confusing the behaviour with Visual Studio which tends to follow indentation when you use ctrl-k ctrl-c.
DWIM is Perl's answer to Gödel
| [reply] |
|
:%s//#/
s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
| [reply] [d/l] |
|
ctrl-x h # marks the whole buffer
ctrl-x r t # runs the command string-rectangle. Useful when you want to insert something thats not starts a comment
| [reply] |
|
I've not used the string-rectangle command before - thanks for a great tip in an unexpected place!
| [reply] |
Re: Adding '#' in the beginning of each line
by GrandFather (Saint) on Aug 16, 2006 at 10:09 UTC
|
Now, here are some interesting links:
The top link is likely the original.
The last is possibly the best of them. At least it is nicely formatted. It has an attribution at the top which mentions "Nik Silver, at the School of Computer Studies, University of Leeds, UK".
DWIM is Perl's answer to Gödel
| [reply] |
|
| [reply] |
Re: Adding '#' in the beginning of each line
by SadPenguin (Novice) on Aug 17, 2006 at 02:21 UTC
|
My biggest concern here is that someone was lazy enough to not only not figure out this (trivial) task on their own, but also not even to rephrase the question, but rather, paste it verbatim from their assignment. I am a university student (and a lazy one, at that) and this is an insult to our kind. For shame, anonymous monk, for shame. | [reply] |
|
open(INFO, "$file");
@line = <INFO>;
close(INFO);
$" = '#';
print "#@line";
| [reply] |