in reply to Chomping most of a _long_ text string

index works just fine here, no need to use regex.
use strict; use warnings; my $u = 70; # Number of underscores my $t = join '',<DATA>; substr($t,0,index($t,"\n".'_'x$u."\n")+$u+2) = ''; print $t; __DATA__ Junk data goes here ______________________________________________________________________ Useful data goes here

Replies are listed 'Best First'.
Re^2: Chomping most of a _long_ text string
by ihb (Deacon) on May 29, 2005 at 18:37 UTC

    I pondered whether I prefered index() or $+[0] and I concluded $+[0]. The index() expression becomes so cluttered, and it has a problem if the substring isn't found. Then you'll destroy the beginning of the string anyway, and remove $u-1 chars from the beginning. I figured that most likely, if the marker isn't there, it's already removed. If not you still need to perform some check, and for me the regex version is nicer.

    I'd like to flip the coin and say "matching works find here, no need to use index()", but if one likes index() one should use index(). :-)

    ihb

    See perltoc if you don't know which perldoc to read!