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

Joseph Mack NA3T jmack at wm7d.net
Sun Aug 16 14:25:20 EDT 2009


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!



More information about the TriLUG mailing list