Operating System - HP-UX
1745883 Members
4363 Online
108723 Solutions
New Discussion юеВ

A little improvement to my webinput perl scripts

 
SOLVED
Go to solution
Steven E. Protter
Exalted Contributor

A little improvement to my webinput perl scripts

I have some perl scripts that take input from a web page and process them into valid html documents.

It works pretty well right now.

I strip out the line feeds as follows so I can put them in with print statements later:

chop ($filedata) if ($filedata =~/\n$/);


In the output file get the following results

this the data^M

This ^M is a single character and I'd like to strip it out.

I know I can do it after the script run with the dos2unix command but I'd rather strip it in the program.

I imagine its another chop statement. I can't begin to figure out what it should be.

Bunny for tested code or an explanation as to why I can't do it.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
19 REPLIES 19
Mel Burslan
Honored Contributor

Re: A little improvement to my webinput perl scripts

Steven,

This is not recently tested but I remember running into a situtaion like this in a 7 line perl code (this is the extent of my perl capacity goes to tell you the truth) but instead of chop, I remember using chomp to eliminate the trailing carriage return character.

Hope it helps.

________________________________
UNIX because I majored in cryptology...
curt larson_1
Honored Contributor

Re: A little improvement to my webinput perl scripts

i would guess why this isn't working is that ms-dos end of line is \r\n$. and just doing a chop is leaving the \r.

you can use chomp to remove a substring at the end of a line. $\ specifies the substring. or $INPUT_RECORD_SEPARATOR if you use English module.
Steven E. Protter
Exalted Contributor

Re: A little improvement to my webinput perl scripts

Attaching a sample output file.

A bunny for a working chomp command.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Steven E. Protter
Exalted Contributor

Re: A little improvement to my webinput perl scripts

Attaching a sample output file.

A bunny for a working chomp command.

attaching a sample

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Ken Penland_1
Trusted Contributor

Re: A little improvement to my webinput perl scripts

^M is considered whitespace to perl, you can strip it off by doing a:

$filedata =~ s/\s+$//;
'
Geoff Wild
Honored Contributor

Re: A little improvement to my webinput perl scripts

Will something like this work?

open(INF,"../links.txt");
@data = ;
close(INF);
foreach $i (@data) {
chomp($i);
($name,$heading,$text) = split(/\|/,$i);
print "$heading\n";
}

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Mark Grant
Honored Contributor

Re: A little improvement to my webinput perl scripts

Hi SEP,

How about (as suggested by curt)

open FILE, "filename" or die "oh no, not again\n";

$/="^M";

while(){
chomp;
print;
}
Never preceed any demonstration with anything more predictive than "watch this"
Steven E. Protter
Exalted Contributor

Re: A little improvement to my webinput perl scripts

These ideas look wonderful.

I will be trying them late this afternoon.

I'm concerned that Ken's code will remove all whitespaces including the spaces between words.

Since the program is already runnning, my perference if possible is to add a chomp command to the existing statement so I don't have to execute a secondd program.

Perhaps change:
chop ($filedata) if ($filedata =~/\n$/);
to
chop ($filedata) if ($filedata =~/^M\n$/);

except I know its not carat M, its a single character taht I don't know the escape code for.

Thanks. If there are other ideas, I'll be happy to try them.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
curt larson_1
Honored Contributor

Re: A little improvement to my webinput perl scripts

what does

$\="\r\n";
chomp(filedata);


do for you