CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
removing newline characters

 



jacqui
Novice

Apr 5, 2000, 1:27 AM

Post #1 of 8 (1467 views)
removing newline characters Can't Post

This one is driving me scatty...

I have a file called "config.dat" full of common data in the form..

key:value

e.g.

target_url:www.mysite.com/target_dir


Which I assign to variables using..

open (CONFIG, "<config.dat")

@config = <CONFIG>;

close (CONFIG);

foreach $config(@config) {
@data = split(/:/, $config);

@data[1] =~ tr/\n //d;

if (@data[0] eq 'target_url') {
$target_url = @data[1]
}
}

I then add a file name and e-mail this info to the user

e.g.

open(MAIL,"|$mailprog -t");

print MAIL "To: $user\n";
print MAIL "From: $me\n";
print MAIL "Subject: File location\n\n";

print MAIL "Your file is at:\n\n";
print MAIL "$target_url/yourfile.html\n";

close (MAIL);

The problem is that the line breaks and arrives in the form..

Your file is at
www.mysite.com/target_dir
/yourfile.html


When I test the routine and print "$target_url/yourfile.html\n" to screen it works OK.

I have tried chop (@config), @data[1] =~ tr/\n//d and just about everything else I can think off but I cannot stop the line breaking when I print to an e-mail.

Any help/Valium greatly appreciated.


darian
Deleted

Apr 5, 2000, 2:22 AM

Post #2 of 8 (1467 views)
Re: removing newline characters [In reply to] Can't Post

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

open (CONFIG, "<config.dat")
while (<CONFIG> ) {
chomp; #gets rid of the newline
($first,$second) = split(/:/,$_);
if($first) eq 'target_url') {
$target_url = $second;
}
}
close (CONFIG);</pre><HR></BLOCKQUOTE>

I didn't use your foreach because it reads in the whole file and can cause problems on the server. Doing the while loop reads each record one by one. Saves on system resources.

The chomp is used to get rid of the newline.

Hope this helps.


jacqui
Novice

Apr 5, 2000, 11:55 AM

Post #3 of 8 (1467 views)
Re: removing newline characters [In reply to] Can't Post

Thanks but it's still the same.

It's really got me going this one. It's just when I....

print MAIL "$target_url/yourfile.html\n";

that it seems to go to pot.


Jasmine
Administrator / Moderator

Apr 5, 2000, 1:08 PM

Post #4 of 8 (1467 views)
Re: removing newline characters [In reply to] Can't Post

Did you try darian's code? If so, did you remove the close paren after the if($first) eq (a little booboo).

Try this updated code below.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open (CONFIG, "<config.dat")
while (<CONFIG> ) {
chomp; #gets rid of the newline
($first,$second) = split(/:/,$_);
if($first eq 'target_url') {
$target_url = $second;
last; # no need to keep looping
# unless you want the last
# match of the file
}
}
close (CONFIG);
</pre><HR></BLOCKQUOTE>

If it doesn't work, please post the actual datafile that you have and change the names to protect the innocent Smile

Here's what I'm thinking... the problem may be that the data in the datafile used backslashes instead of forward slashes in the url.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


whatever.com\newline.html
whatever.com\formfeed.html
whatever.com\rreturn.html
</pre><HR></BLOCKQUOTE>

Notice anything here?



darian
Deleted

Apr 5, 2000, 11:55 PM

Post #5 of 8 (1467 views)
Re: removing newline characters [In reply to] Can't Post

Oops guess I did get carried away with that paren huh? I tried it with the format you described in your post

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">quote:</font><HR>target_url:www.mysite.com/target_dir<HR></BLOCKQUOTE>
Minus my typo with the paren it works fine.

Also good call on the "last;" line Jasmine. Always forget to add that one.


jacqui
Novice

Apr 6, 2000, 2:43 AM

Post #6 of 8 (1467 views)
Re: removing newline characters [In reply to] Can't Post

Thankds for all your help but still there :-(

I've extracted the offending bits and incuded them below.

######## CONFIG DATA "test.dat" ##

target_url:www.mysite.com/target_dir
mailprog:/usr/lib/sendmail
admin_mail:admin@mysite.com
etc:etc

######## THE SCRIPT "test.pl" ########

#!/usr/bin/perl

$email = "name\@theirdomain.com";

$user = "your-name";

## Load config info

&get_config;

## Send e-mail to user

open(MAIL,"|$mailprog -t");

print MAIL "To: $email\n";
print MAIL "From: $admin_mail\n";
print MAIL "Subject: File location\n\n";

print MAIL "Dear $user\n\n";
print MAIL "Thank you for visiting our web site.\n\n";

print MAIL "Your file is at:\n\n";

print MAIL "$target_url/yourfile.html\n\n";

print MAIL "Thank you.\n\n";

print MAIL "-" x 25 . "\n\n";

print MAIL "If you have any problems please contact us at $admin_mail.\n";

close (MAIL);

#### print "$target_url/yourfile.html\n\n";

## Show thanks page

&print_header;

print "Thank you - we have e-mailed you with the info.</P>\n";
print "<P ALIGN=center>Experience a favorable diurnal instance :-)";

&print_footer;

exit;


## Get config details

sub get_config {

open (CONFIG, "<test.dat");

while (<CONFIG> ) {

chomp; #gets rid of the newline

($first,$second) = split(/:/,$_);

if($first eq 'target_url') {
$target_url = $second;
}

if($first eq 'mailprog') {
$mailprog = $second;
}

if($first eq 'admin_mail') {
$admin_mail = $second;
}

}

close (CONFIG);

}

## PRINT HTML HEADER

sub print_header {

print "Content-type: text/html\n\n";

print <<HEAD;

<HTML><HEAD><TITLE>Thanks</TITLE></HEAD>
<BODY><P ALIGN=CENTER>
HEAD

}

## PRINT HTML FOOTER

sub print_footer {

print <<FOOT;

</P></BODY></HTML>

FOOT

}

######## THIS GETS DELIVERED AS... ###########

To: name@theirdomain.com
From: admin@mysite.com
Subject: File location

Dear your-name

Thank you for visiting our web site.

Your file is at:
www.mysite.com/target_dir
/yourfile.html

Thank you.

-------------------------

If you have any problems please contact us at admin@mysite.com
.

##########

(Hadn't noticed before, but the same thing is happening with...

print MAIL "If you have any problems please contact us at $admin_mail.\n";

- the "." is on a new line as well.

Oooooh!! Sometimes ironing doesn't seem so bad :-)


japhy
Enthusiast

Apr 6, 2000, 3:01 AM

Post #7 of 8 (1467 views)
Re: removing newline characters [In reply to] Can't Post

Your data file probably has \r characters (they sometimes appear as ^M) at the end of the line, just before the \n.

To test this theory, I suggest you try doing:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


tr/\r\n//d; # instead of chomp
($first,$second) = split /:/;
# etc...
</pre><HR></BLOCKQUOTE>


jacqui
Novice

Apr 6, 2000, 7:53 AM

Post #8 of 8 (1467 views)
Re: removing newline characters [In reply to] Can't Post

That's got the little beastie !!

Thanks for all your help.

Jac
xxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxx

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives