Gossamer Forum
Home : General : Perl Programming :

I hate newlines!

Quote Reply
I hate newlines!
Man, newlines *really* know how to piss me off! I am trying to get rid of some in a string. The output is something like;

var targetURL="admin.cgi?do=plugin&plugin=Real_Spider&func=spider_run&urls=http://www.google.com/ads/,http://www.google.com/services/&IGNORE_RULES=0&SEARCH_FOR_ALL=jobs
,service&SEARCH_FOR_EXCLUDING=tools"

The code is;

Code:
my $_RULES = $RULES; chomp($_RULES); $_RULES =~ s/\n/,/g;
my $_EXCLUDING = $EXCLUDING; chomp($_EXCLUDING); $_EXCLUDING =~ s/\n/,/g;
$_EXCLUDING =~ s/\s$//; # get rid of triling slashes..
$_RULES =~ s/\s$//; # get rid of triling slashes..
$_EXCLUDING =~ s/^\s//; # get rid of leading slashes..
$_RULES =~ s/^\s//; # get rid of leading slashes..

*why* oh why isn't it removing the newline? (notice the bit after 'services' in the output). I've tried getting rid of \t and \r too, but there the new line STILL remains! Its driving me mad.

Unsure

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] I hate newlines! In reply to
s/\n//g

...works fine for me. Find out what character it is using ord()
Quote Reply
Re: [Paul] I hate newlines! In reply to
The following code;

Code:
my @rules_test = split("",$_RULES);
foreach (@rules_test) {
print "$_\t".ord($_)."\n";
}

....now gives me...

l 108
i 105
n 110
k 107
s 115

13
, 44
s 115
o 111
m 109
e 101
t 116
h 104
i 105
n 110
g 103

I'm assuming it is number 13 that is causing the problem?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] I hate newlines! In reply to
Just had a similar problem again with a custom plugin I was writing. The fix is pretty simple (once you know how to do it). Just use;

$_ =~ s/\W$//;

This will get rid of any non-english charachters at the end of the line. I'm not sure how well this will work if someone has a '1' or % at the end of their query.. but it seems to be working ok at the moment.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] I hate newlines! In reply to
Heres the final code (just tested it, and can't find any problems with it);

Code:
my @cut_commands = split /\n/, $Commands;

my $count = 0;
my $array_entries = $#cut_commands;
foreach (@cut_commands) {

chomp $_;

if ($count < $array_entries) { $_ =~ s/\W$//; } # dont run this ont he last line, cos that screws things up...

# do stuff with array entry here...

$count++;

}

If you have this kind of problem... enjoy :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] I hate newlines! In reply to
Did you check there were no form feeds or returns in there?..

\f

\r
Quote Reply
Re: [chmod] I hate newlines! In reply to
I checked for \r ... but didn't even know \f existed. Wouldn't suprise me if that was the problem, as it was a TEXTAREA I was having the problems with, thus a form feed newline would sound about right. Why can't they just pass normal \n in Crazy

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] I hate newlines! In reply to
>>Why can't they just pass normal \n in

agreed, if only..

If its a textarea then that could well be your problem (especially if people copy and paste from some text editor) as different client o/s use different breaks.

I had that problem once and checking for them all cured it.Sly

I used a regex that swapped them for a meaningful marker like "I'M A FORMFEED" etc to see if they were actually there first.



chmod
Quote Reply
Re: [chmod] I hate newlines! In reply to
So I suppose the only way to get rid of these kind of things, would be;

$var =~ s/\f|\n//;

Ah well, thanks for the info... my fix seems to be working ok at the moment, but I'm definatly going to remember this... as I'm sure it will give me hassle again some time in the near future :(

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!