Gossamer Forum
Home : General : Perl Programming :

Reg Exp help

Quote Reply
Reg Exp help
Still muddled with regular expressions. I need to convert a string that is delimited by carriage returns into an array. For example:

Convert (where <cr> denotes a carriage return)

apples<cr>oranges<cr>peaches<cr>

to

fruit(1) = apples
fruit(2) = oranges
fruit(3) = peaches

Thanks... Dan Smile
Quote Reply
Re: Reg Exp help In reply to
I'm not brilliant with them either, but assuming your fruits are in $fruits, this works:

@fruits = split("\n", $fruits);

Cheers,
adam
Quote Reply
Re: Reg Exp help In reply to
 Smile

Actually, I'm not sure that it is specifically Windows that adds the carriage return (\r) character. But I know that it does if you are adding data through a form in a browser.

A little story. I was working on something where I wanted to be able to enter the elements of an array into a textarea, one element on each line. When I submitted the form, I wanted it to split the string on the \n character and then join the array back together with a comma. (I could just has easily have done a substitution, but there was some other stuff I was doing with the array in the middle.)

When I entered

apples
oranges
peaches

In the text area, the string I ended up with after everything was done looked like

apples,
oranges,
peaches

Not what I wanted. I pulled my hair out for a long time until I remembered about different linefeeds. I added a line like the one I gave Dan above -- $fruits =~ s/\r//g; -- and everything was fine.

If you're entering this kind of thing in DBMan, you won't have the problem because the parse_form subroutine removes the \r characters when the data is first entered. But, if you are going to want to split a field into an array and you've used the "Enter" key between the elements, it's a good idea to have a line where you strip out the \r character.

Does that help?

------------------
JPD





Quote Reply
Re: Reg Exp help In reply to
If you've entered the elements through a Windows application, there will also be another character added with the newline character. (I just went through all sorts of problems trying to figure this out.)

Before you split your string, use

$fruits =~ s/\r//g;

And, just picking a few nits, the resulting array (after the "split") would be:

$fruit[0] = apples
$fruit[1] = oranges
$fruit[2] = peaches

------------------
JPD





Quote Reply
Re: Reg Exp help In reply to
Thanks to the both of you! Both put together solve the problem.

Thanks again... Dan Smile
Quote Reply
Re: Reg Exp help In reply to
Hi Carol,

Could you explain that to me? Windows applications use a different line break to Unix right? Alex went into it recently, with all this stuff about CTCR or something. (My knowledge of the inner workings of computers, whether it's Unix, Windows, or anything else, leaves a lot to be desired. It's like a car. I get in, it works, I'm happy... Smile

The reason I ask is purely curiosity. When Dan asked the question, I wanted to check how the split function works on a line breaks before I opened me big gob. So I tested it. I created a file in Wordpad with three fruits, one per line. I opened it with Perl, made an array of the contents, wrote that array to a string with the line breaks intact, and then split the string on \n. Which worked perfectly...

So should it have? I seem to have a very forgiving machine when it comes to Perl stuff, it doesn't complain when I make boo-boo's and doesn't generate errors when people say they will. But my scripts also run perfectly on my Unix server.

It's about time the machine was nice to me anyway, with the amount of crashes I have to put up with. Damn Bill Gates and all his products and services! Smile

adam
Quote Reply
Re: Reg Exp help In reply to
Uh-huh. Thanks very much Carol, most helpful.

adam