chomp ($&) to remove specific \n 
Author Message
 chomp ($&) to remove specific \n

I'm using Perl 5.001 on Win32. I need to strip out selected \n
characters from a text file using inplace editing, where the \n is
preceded by a specific match pattern.

I tried this:

s/matchpattern.*\n/chomp($&)/egi

However chomp() returns the no. of characters removed, not
the string minus \n, so that won't work. I tried chop(), but
got an error saying "modification of a read-only value attempted".

I ultimately used something like this, which worked but seems
unwieldy:

m/matchpattern.*\n/i;
$matchedstring = $&;
chomp($matchedstring);
s/matchpattern.*\n/$matchedstring/gi;
print;

This works OK, but I spent a lot of time looking for a syntactically
simpler method. I've checked the FAQ, various books, dejanews, etc.,
and was curious if I missed something obvious?

-- Joe Marler (Remove nospam to reply)



Fri, 16 Jun 2000 03:00:00 GMT  
 chomp ($&) to remove specific \n


: I tried this:

: s/matchpattern.*\n/chomp($&)/egi

: However chomp() returns the no. of characters removed, not
: the string minus \n, so that won't work. I tried chop(), but
: got an error saying "modification of a read-only value attempted".

: I ultimately used something like this, which worked but seems
: unwieldy:

: m/matchpattern.*\n/i;
: $matchedstring = $&;
: chomp($matchedstring);
: s/matchpattern.*\n/$matchedstring/gi;
: print;

It's best to avoid using $& if you can; using it greatly slows down *all*
regex matching in your script.  Fortunately, there's a simple way to do
what you want:

 s/(matchpattern.*)\n/$1/i;

if $_ contains only one newline.  If it contains more than one newline,
use the g and s modifiers as well and put a ? after the *.

This makes uses of the $n facility for remembering matched portions of a
string; you can find out more about it by reading perlre.



Sat, 17 Jun 2000 03:00:00 GMT  
 chomp ($&) to remove specific \n

Geez, and I thought chomp returned the character chomp-ed.

$& is read-only.  You canna chomp 'er, cap'n, she'll fly apart!

        -joseph

Quote:

> I'm using Perl 5.001 on Win32. I need to strip out selected \n
> characters from a text file using inplace editing, where the \n is
> preceded by a specific match pattern.

> I tried this:

> s/matchpattern.*\n/chomp($&)/egi

> However chomp() returns the no. of characters removed, not
> the string minus \n, so that won't work. I tried chop(), but
> got an error saying "modification of a read-only value attempted".



Sat, 17 Jun 2000 03:00:00 GMT  
 chomp ($&) to remove specific \n

: I'm using Perl 5.001 on Win32. I need to strip out selected \n
: characters from a text file using inplace editing, where the \n is
: preceded by a specific match pattern.

: I tried this:

: s/matchpattern.*\n/chomp($&)/egi

[snip]

: This works OK, but I spent a lot of time looking for a syntactically
: simpler method. I've checked the FAQ, various books, dejanews, etc.,
: and was curious if I missed something obvious?

   s/(matchpattern.*)\n/$1/gi;

--
    Tad McClellan                          SGML Consulting

    Fort Worth, Texas



Sat, 17 Jun 2000 03:00:00 GMT  
 chomp ($&) to remove specific \n

Thanks -- I'd tried $1, but didn't think to put only the match pattern
minus the \n in parens.

================

Quote:

>It's best to avoid using $& if you can; using it greatly slows down *all*
>regex matching in your script.  Fortunately, there's a simple way to do
>what you want:

> s/(matchpattern.*)\n/$1/i;

>if $_ contains only one newline.  If it contains more than one newline,
>use the g and s modifiers as well and put a ? after the *.

>This makes uses of the $n facility for remembering matched portions of a
>string; you can find out more about it by reading perlre.



Sat, 17 Jun 2000 03:00:00 GMT  
 chomp ($&) to remove specific \n

I would really get length of substring and rindex the beginning/end, as the
case may be :-)

But, chomp will ONLY chomp off the last \n

So, if there are more than x number of  '\n' you'll probably want to
$returnstring = s/matchpattern.*\n/$_/\/egi

Something like that?  MY RegEx is not all that great :-)

HTH,
Bill

Quote:

>Geez, and I thought chomp returned the character chomp-ed.

>$& is read-only.  You canna chomp 'er, cap'n, she'll fly apart!

> -joseph


>> I'm using Perl 5.001 on Win32. I need to strip out selected \n
>> characters from a text file using inplace editing, where the \n is
>> preceded by a specific match pattern.

>> I tried this:

>> s/matchpattern.*\n/chomp($&)/egi

>> However chomp() returns the no. of characters removed, not
>> the string minus \n, so that won't work. I tried chop(), but
>> got an error saying "modification of a read-only value attempted".



Mon, 19 Jun 2000 03:00:00 GMT  
 chomp ($&) to remove specific \n

Suppose you want to get rid of newlines when they are
preceded by "Boo!".  Then:

  s/(Boo!)\n+/$1/g;

        -joseph
         http://www.effectiveperl.com

Quote:

> I would really get length of substring and rindex the beginning/end, as the
> case may be :-)

> But, chomp will ONLY chomp off the last \n



Thu, 22 Jun 2000 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. PERLFUNC: chomp - remove a trailing record separator from a string

2. PERLFUNC: chomp - remove a trailing record separator from a string

3. Removing specific strings in a text file

4. Remove a specific member of an array

5. Extracting specific text from specific files in a list

6. remove system menu & buttons, Win95

7. s///, chomp, and matching //

8. Problems with chomp and chop

9. chomp not chomping w/ new $/ char

10. Question about using chomp and other functions together

11. Trying to Learn Chomp

12. Help with "Chomp" Command

 

 
Powered by phpBB® Forum Software