[TriLUG] perl: keeping blank when split'ing string on a blank

Scott Lambdin lopaki at gmail.com
Sun Aug 16 14:52:48 EDT 2009


The wheel has been invented:
http://www.perlmonks.org/?node=Text%3A%3AParseWords

On Sun, Aug 16, 2009 at 2:25 PM, Joseph Mack NA3T <jmack at wm7d.net> wrote:

> I'm writing a webform to querying a database. Let's say I want all books
> with the string "Tom Sawyer" and the string "adventures" in the title. The
> user will enter
>
> "Tom Sawyer" adventures
>
> in the text box and the cgi will get this string as a parameter. To query
> the database I need to separate "adventures" and "Tom Sawyer" but not split
> "Tom Sawyer".
>
> Running split (/ /, $param_title) gives me 3 words rather than the 2 words
> (strings) I want.
>
> Any ideas?
>
> Here's my attempts so far
>
> #! /usr/bin/perl
>
> #both of these give the same result
> #$param_title = '"Tom Sawyer" adventures';
> $param_title = "'Tom Sawyer' adventures";
>
> print $param_title, "\n";
>
> @words = split (/ /,$param_title);
>
> print $words[0], "\n";
> print $words[1], "\n";
> print $words[2], "\n";
> print @words, "\n";
>
> #----------------
>
> output is
>  ./string_stuff.pl
> 'Tom Sawyer' adventures
> 'Tom
> Sawyer'
> adventures
> 'TomSawyer'adventures
>
> Attempts to use qw to produce an array, also split the quoted string.
>
> Thanks Joe
>
> --
> Joseph Mack NA3T EME(B,D), FM05lw North Carolina
> jmack (at) wm7d (dot) net - azimuthal equidistant map
> generator at http://www.wm7d.net/azproj.shtml
> Homepage http://www.austintek.com/ It's GNU/Linux!
> --
> TriLUG mailing list        : http://www.trilug.org/mailman/listinfo/trilug
> TriLUG FAQ  : http://www.trilug.org/wiki/Frequently_Asked_Questions
>



-- 
There's a box?



More information about the TriLUG mailing list