PDA

View Full Version : splitting a variable more than one way


zenweezil
02-07-2004, 03:24 AM
I currently use the following code to split a text file up so the first paragraph appears on the home page and then the user can click in to read the rest:

@display = split(/<BR><BR>/, $small_story);
$showtext = $display[0];

Unfortuantely the text file sometimes has <BR> <BR> instead of <BR><BR> -- so how can I set the split up to use one or the other?

Or do I have to take the $showtext and split it again looking for a <BR> <BR>?

Paul-Ecchi
02-10-2004, 07:55 PM
I am not sure if this will work, but try:
@display = split(/<BR><BR>|<BR> <BR>/, $small_story);

kidd
02-11-2004, 08:25 PM
Or you can just do this:


@display = split(/<BR>\s*<BR>/, $small_story);


The \s* tells perl that it may or not be a space between.

Cheers

zenweezil
02-18-2004, 03:07 AM
Sometimes it's the simplest things that elude me.

dswimboy
02-20-2004, 02:20 AM
Originally posted by kidd

@display = split(/<BR>\s*<BR>/, $small_story);

The \s* tells perl that it may or not be a space between.
[/B]
\s* actually tells perl to match any number (including zero) of white space characters surrounded by <BR>.

Paul-Ecchi's way may be better, just for the sake of perfection.
@display = split(/<BR><BR>|<BR> <BR>/, $small_story); @display = split(/<BR>(| )<BR>/, $small_story); would also work.

so many ways! if you want some more...look up the General Quantifiers and other Metacharacters