http://qs321.pair.com?node_id=688432


in reply to Ways to delete start of string

A nit-pick, but the substitution should really be

s/.//s
to be equivalent with the others. Note the s modifier; it makes . match leading newlines as well. Otherwise you remove the first non-newline character from the string.
my %ex = ( "\n" . 'regexp' => sub { s/.// }, "\n" . 'regexp/s' => sub { s/.//s }, "\n" . 'chop' => sub { $_ = reverse; chop; $_ = reverse; }, "\n" . 'substr1' => sub { substr($_,0,1) = '' }, "\n" . 'substr2' => sub { $_ = substr($_,1) }, ); for (sort keys %ex) { $ex{$_}->(); print "[$_]\n"; } __END__ [chop] [ egexp] [regexp/s] [substr1] [substr2]

Update: Added example.

lodin

Replies are listed 'Best First'.
Re^2: Ways to delete start of string (The /s modifier)
by hsmyers (Canon) on May 26, 2008 at 00:23 UTC
    Color me confused. The goal is to eliminate the first character. Given that removing 'the first non-newline character' is precisely what is desired. Please explain what you mean. Unless of course you are thinking that a newline might be the first character in the general case. Then that would be correct. Admittedly that never occurred to me. For no particular reason I tend to thing of newlines occurring at the end of a string and usually removed by chop/chomp at that. Of course there are times when I'm dealing with an entire as is buffer in which case there would be an expectation of embedded newlines but even at that it seems unlikely that they would wind up in a string that I would want to do a reverse chop on. Like I said please explain...

    --hsm

    "Never try to teach a pig to sing...it wastes your time and it annoys the pig."