From: "John W. Krahn" <[EMAIL PROTECTED]> > Pavle Lukic wrote: > > Problem > > Given a string and a pattern, construct new string > > by removing part of the string equal to pattern. > > Remove only first occurrence of the pattern. > > > > Problem solutions > > > > Solution #1 ($x = $a) =~ s/\Q$b//; > > > > Solution #2 $x = > > substr($a,0,index($a,$b)).substr($a,index($a,$b)+length($b)); > > Too complicated. :-) > > substr $x = $a, index( $a, $b ), length( $b ), ''; > > John
Too obfuscated. Very few people would have any chance understanding what's going on in this statement. This $x = $a; substr $x, index( $a, $b ), length( $b ), ''; should not be much slower (if at all) yet it's much easier to read. perldoc -f substr substr EXPR,OFFSET,LENGTH,REPLACEMENT substr EXPR,OFFSET,LENGTH substr EXPR,OFFSET Extracts a substring out of EXPR and returns it. First character is at offset "0", or whatever you've set "$[" to (but don't do that). If OFFSET is negative (or more precisely, less than "$["), starts that far from the end of the string. If LENGTH is omitted, returns everything to the end of the string. If LENGTH is negative, leaves that many characters off the end of the string. ... An alternative to using substr() as an lvalue is to specify the replacement string as the 4th argument. This allows you to replace parts of the EXPR and return what was there before in one operation, just as you can with splice(). P.S.: If you really wanted to keep this as a single command I'd recommend adding some parens: substr ($x = $a, index( $a, $b ), length( $b ), ''); or substr (($x = $a), index( $a, $b ), length( $b ), ''); so that it's clear what's being assigned to the $x. Plus I find substr (($x = $a), index( $a, $b ), length( $b )) = ''; slightly easier to read. Jenda ===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz ===== When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]