in reply to How do I delete the first n characters on a line

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

Replies are listed 'Best First'.
Re: Answer: How do I delete the first n characters on a line
by I0 (Priest) on Jul 20, 2001 at 03:37 UTC
    #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