Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.
  • Comment on Adding '#' in the beginning of each line

Replies are listed 'Best First'.
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

      davorg++

      Concerning the usage of $", one could augment your solution by doing

      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 <>;

      -- Hofmator

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 } };
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...

Re: Adding '#' in the beginning of each line
by GrandFather (Saint) on Aug 16, 2006 at 07:23 UTC
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.

      Using Komodo I'd:

      • ctrl-a
      • ctrl-3

      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
        In /g?vim/:
        :%s//#/

        s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      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
        I've not used the string-rectangle command before - thanks for a great tip in an unexpected place!
Re: Adding '#' in the beginning of each line
by GrandFather (Saint) on Aug 16, 2006 at 10:09 UTC

      The first of those is the original authored by Nik Silver. As stated at the top of the first page of the tutorial it was written for Perl 4 and is really only of historical interest. Given that however it is a bit of worry that someone is using it as current teaching material.

      /J\

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.
      open(INFO, "$file"); @line = <INFO>; close(INFO); $" = '#'; print "#@line";