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

I'm a perl newbie. I'm trying to make a script in order to strip something of the form x: from the begining of each line of a file (where x is an integer). I can't figure out how to do that. Any hint? Thanks

Replies are listed 'Best First'.
Re: Path to enlightment
by KM (Priest) on Jan 23, 2001 at 23:21 UTC
    perldoc perlre
    perldoc -q strip

    Cheers,
    KM

      OK, I ++'d KM for his answer, since it gives what was asked for -- a hint. Slightly more than a hint:

      # FILE is open for read, OUTPUT is open for writing while (<FILE>) { s/^(\d+:)//; # take $_, replace one or more # digits at the beginning of the line #followed by a : with #nothing print OUTPUT; }

      Note: that may not be exactly what you want; for how to change it, see KM's first reference. 'Course, there's some one-liner goodness to be had here too (you can do this on the command line with one -- albeit complex -- command. For that, see:

      perldoc perlrun

      Specifically, the -p switch).

      Good luck!

      Philosophy can be made out of anything. Or less -- Jerry A. Fodor

        s/^(\d+:)//;

        Minor nit.. you don't need to do the grouping. And, although he didn't say it he may also want the whitespace removed after the colon.

        s!^\d+:\s+!!;

        Cheers,
        KM

      Hey KM you can link up the documentation like this:
      perldoc perlre

      Which I did with [perlman:perlre|perldoc perlre]. I think it is cool, anyway...

      Update Hmm, KM has a very valid point below... /me ponders

      --
      $you = new YOU;
      honk() if $you->love(perl)

        Hey KM you can link up the documentation like this:

        I have said in the past, no :) Why? I think you should view the documentation provided with your installed version of Perl. But, if I remember to next time, maybe I will.

        Cheers,
        KM

(adamsj) Re: Path to enlightment
by adamsj (Hermit) on Jan 23, 2001 at 23:29 UTC
    What KM said above--and another hint, of sorts: Don't think integer, think /digit(s)?/ (which is probably a misleading hint--but one that might tell you something anyway).

    Note: This node has been repeatedly slightly diddled.

      Note: This node has been repeatedly slightly diddled.

      Diddling nodes is illegal in most states. :-)

      Cheers,
      KM

Re: Path to enlightment
by dsb (Chaplain) on Jan 24, 2001 at 02:08 UTC
    You really want to to some research on regular expressions. If you are just starting out I'd suggest Learning Perl from O'reilly Assoc. For now though, try this:
    while ( <FH1> ) { chomp; $_ =~ s/^\d+//; print FH2 $_, "\n"; # to get it into a new file }
    - kel -
Re: Path to enlightment
by Chady (Priest) on Jan 23, 2001 at 23:33 UTC
    You should be reading about RegEx (Regular Expressions) search on that topic and it should get you started
    Chady | http://chady.net/