Gossamer Forum
Home : General : Perl Programming :

Not stripping something?

Quote Reply
Not stripping something?
For some reason, there is a newline feed not being cleaned out from a variable I have. The code i am using is;

my $new_cat = $_;
chomp $new_cat;
$new_cat =~ s/_/ /g;
$new_cat =~ s,\n,,g;

...if I put an extra bit of Regex in that cleans out \W, then it doesn't show the newline. The problem with that, is it cleans out stuff I don't want removing, such as / and \.

Anyone got any ideas why it would do this? Unsure

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] Not stripping something? In reply to
Why use chomp() and then a regex that will wipe out the newline anyway?

As for the answer to your question, RTFM.

perldoc perlre

Specifically....

Quote:
s

Treat string as single line. That is, change "." to match any character whatsoever, even a newline, which normally it would not match.

The /s and /m modifiers both override the $* setting. That is, no matter what $* contains, /s without /m will force "^" to match only at the beginning of the string and "$" to match only at the end (or just before a newline at the end) of the string. Together, as /ms, they let the "." match any character whatsoever, while still allowing "^" and "$" to match, respectively, just after and just before newlines within the string.
Quote Reply
Re: [Paul] Not stripping something? In reply to
Code:
$_=~ s,.$,,g;

Are you saying to use something like this? All that does it remove the last charachter. I just *can't* see there this extra newline is being used. Thats why I used the 'chomp' , and then some regex to get rid of '\n' Unimpressed

ARRRRGGGHH!

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] Not stripping something? In reply to
Erm, did you read "perldoc perlre"?

Note the bolded "s" modifier Crazy