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:
Converting strings

 



ad65
Deleted

May 19, 2001, 6:29 AM

Post #1 of 8 (1861 views)
Converting strings Can't Post

Hi, i've got a string $news, which is what I fill in on a form page. Each time it's submitted, it creates a page according to the first letter of $news, and subsequent submittions starting with the same letter are added to that page. So I have links to # A B C D E etc... The # page is 0.html
The problem is, say $news starts with a 1, or 2, 3 etc it isn't going to show up on the # page(0.html).

Therefore, IF $news starts with a number, how can I add a 0 to the start of $news, in other words converting it to 0$news for the rest of the script. This would mean the entry '1000 people killed by explosive fart gas' would go to '01000 people etc'... And still, if it was 'Lots of people killed etc'... then it wouldn't affect it. Any ideas?

Second question is, if someone can tell me how to do that first one, how do I strip the 0 off the start and make another string:
Something like
$newswo = $news without a leading 0

So in the end I will have $news, which is 0$news if it started with a number, and $newswo, which is 0$news but without the zero. Deary me..I don't know how to explain it further than that. Any other suggestions as to how I can get around this problem are welcome, it doesn't necessarily have to be this way of doing things.

Third question - How do I get the form to recognise spaces inbetween lines, instead of just shoving all the text together with single spaces? Here's the accept form data stuff:

if ($ENV{'REQUEST_METHOD'} eq 'POST') {

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}

So I enter this

With

Gaps

And it comes out 'So I enter this With Gaps', when I want it with the spaces inbetween and new lines.

Anyone that can - please help. Thanx

(This post was edited by ad65 on May 19, 2001, 7:04 AM)


mhx
Enthusiast / Moderator

May 19, 2001, 8:15 AM

Post #2 of 8 (1856 views)
Re: Converting strings [In reply to] Can't Post

Hi!

I think I've got your point, although I don't believe that it is an elegant solution to the underlying problem.
But, anyway, you've got your string $news. I'd propose the following:


Code
$newswo = $news = "My news string!"; 
$news =~ s/^(?=\d)/0/;

This will save the original $news in $newswo and put a zero in front of $news if $news starts with a digit.
The (?=\d) specifies a positive lookahead condition. Of course, you can do it in this way, too:


Code
$news = "My news string!"; 
$news =~ s/^(?=\d)/0/;
...
$newswo = $news;
$newswo =~ s/^0//;

But really, I'd prefer the first one.

A suggestion on how you can avoid such tricky things:


Code
$news = "My news string!"; 

if( $news =~ /^([a-zA-Z])/ ) { # A-Z as first letter ?
$html_file = lc($1).".html"; # => (lowercase first letter).html
}
else { # All other characters
$html_file = "0.html"; # => 0.html
}

This will create the name of your file directly from the $news string. You could also shrink this to:


Code
$news = "My news string!"; 
$html_file = ($news=~/^([a-zA-Z])/?lc($1):'0').'.html';

-- Marcus



ad65
Deleted

May 19, 2001, 8:53 AM

Post #3 of 8 (1855 views)
Re: Converting strings [In reply to] Can't Post

I'm glad one of us knew what I meant! It was a bit tricky to explain, and a late night yesterday didn't help my thinkin...anyway I just tried the first bit you gave me, and it works a treat, thanx a lot you are a little genius lol.



BarisDe
New User

Feb 10, 2002, 9:53 PM

Post #4 of 8 (1823 views)
Re: [ad65] Converting strings [In reply to] Can't Post

Why can I reply to a post but not write a new post? This is really interesting.


Jasmine
Administrator / Moderator

Feb 11, 2002, 1:04 PM

Post #5 of 8 (1779 views)
Re: [BarisDe] Converting strings [In reply to] Can't Post

Are you still having problems? I've made some corrections that should have fixed this.

Jasmine


Baris
Novice

Feb 11, 2002, 1:07 PM

Post #6 of 8 (1777 views)
Re: [Jasmine] Converting strings [In reply to] Can't Post

Wow, you've made the right changes. Now it's working. I hope that it will stay like this for the future. Thanks Jasmine.


MouldyGoat
stranger

Feb 11, 2002, 3:10 PM

Post #7 of 8 (1771 views)
Re: [ad65] Converting strings [In reply to] Can't Post

About the third question, I don't think this is necessarily your script's fault - if you check the HTML source code produced by your script I think you'll find it probably is putting in the formatting you require - only you'll need something like <BR> tags instead of newlines to represent this in the browser.

For this you could try something like:

$FORM{'input'} =~ s/\n/<BR>/gs;

Hope that helps.


Baris
Novice

Feb 11, 2002, 8:17 PM

Post #8 of 8 (1765 views)
Re: [MouldyGoat] Converting strings [In reply to] Can't Post

Yes yes, I found it later, thanks alot for replying. It was my fault not to write that I solved it, too.

 
 


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

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