http://www.perlmonks.org?node_id=97571

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

How do I delete the first n characters on a line

Originally posted as a Categorized Question.

  • Comment on How do I delete the first n characters on a line

Replies are listed 'Best First'.
Re: how do i delete the first n characters on a line
by jmcnamara (Monsignor) on Jul 18, 2001 at 12:28 UTC
    Use substr:
    # substr EXPR,OFFSET,LENGTH,REPLACEMENT my $n = 3; my $str1 = "1234567890\n"; my $str2 = substr($str1, $n); print $str1; # 1234567890 print $str2; # 4567890

    John.
    --

Re: How do I delete the first n characters on a line
by ar0n (Priest) on Jul 18, 2001 at 12:28 UTC
    substr($str, 0, $n) = "";
Re: How do I delete the first n characters on a line
by tachyon (Chancellor) on Jul 19, 2001 at 05:43 UTC

    You could also use a regex although this is not as fast as substr.

    my $n = 3; my $str = "1234567890\n"; $str =~ s/^.{$n}//s; # was: $str =~ s/^.{$n}(.*)$/$1/s; print $str;

    The difference with the regex solution is that if $n > length $str no change will be made. With substr you will get a null string left in $str if you try to delete more chars than exist in $str. You need the /s modifier to make the . match a newline which is a valid char.

    cheers

    tachyon

    To get the same behavior as a substr solution use: $str =~ s/^.{1,$n}//s;

    Edit by tye to incorporate I0's reply

      #a simpler regex is
      $str =~ s/^.{$n}//s #or, if you want a null string left in $str if you try to delete more chars than exist in $str,
      $str =~ s/^.{1,$n}//s
Re: How do I delete the first n characters on a line
by abstracts (Hermit) on Jul 19, 2001 at 06:26 UTC
    # substr EXPR,OFFSET,LENGTH,REPLACEMENT
    
        my $n    = 3;
    
        my $str1 = "1234567890\n";
        print $str1; # 1234567890
        
        substr($str1,0,$n,'');
        print $str1; # 4567890
    

    Aziz,,,

    Originally posted as a Categorized Answer.

Re: How do I delete the first n characters on a line
by bikeNomad (Priest) on Jul 19, 2001 at 07:13 UTC
    Another interesting thing about substr is that it can be assigned to (it's an lvalue). So if you really wanted to delete the characters, you could do this:

    my $line = "abcdefghijk"; # delete the first 3 characters: substr($line, 0, 3) = ''; print $line; # "defghijk"

    Originally posted as a Categorized Answer.

Re: How do I delete the first n characters on a line
by Anonymous Monk on Jul 19, 2001 at 16:37 UTC
    regexes carry with them a performance penalty.
    there is nothing wront with just:
    $foo = substr($foo,$n) unless(length $foo => $n);
    or
    $substr($foo,0,$n)="" unless(length $foo => $n);
    or
    $substr($foo,0,$n,'') unless(length $foo => $n);
    this is generally faster than a regex ;-)

    Originally posted as a Categorized Answer.

Re: How do I delete the first n characters on a line
by alfie (Pilgrim) on Jul 18, 2001 at 12:28 UTC
    Given that you have the line in $_ and substitute the n with the amount of characters you'd like to delete:
    s/^.{n}//;

    Originally posted as a Categorized Answer.