Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need split function help

Status
Not open for further replies.

DRapper

Programmer
Nov 25, 2003
18
0
0
US
Hello All,

I'm using split(/&/, $buffer). I discovered that $buffer may contain something like this ?company=Bob & Jane&firstname=John&lastname=smith. When I use the above split function it splits Bob & Jane which I do not want it to do. Is there anyway to get around this and still use the & as my delimiter.

Thanks for you help

DRapper
 
Thanks for your quick responce, but I'm still having the same issue. I'm only getting Bob and I need Bob & Jane. Is there something else you can suggest?

DRapper
 
The split command needs an array. Each name is now an index of the array. Whitespace is also part of the names, so you'll have to remove that when dealing with each name.
If you use $names it will just give you bob, the first index of the array, in the below case $names[0] which is "Bob ".

$string = "Bob & Jane";

@names = split /\&/, $string;

print @names;
 
Thanks again for your quick responce, but my $buffer string that I'm trying to split contains the following

?company=Bob & Jane&firstname=John&lastname=smith

I need company=Bob & Jane in one variable and
firstname=John in another and so on.

DRapper
 
ho11ywood, I think you misunderstood what DR's problem is, he/she is trying to split a string unsing & as delimiter but there are parts of the string that contains & that he doesn't want to split. That said, I dont think there is a one shot way to do that: I assume that info is coming from a form and your only problem will be with the 'company' value, so i suggest you do a substitution on the string before you do the split,
$forename = "company=Bob & jane&firstname=John&lastname=smith";
$forename =~ s/ & / and /; #notice the spaces
print $forename;

That will substitute and '&' with white space before and after and replace it with 'and'
 
@names = split (/\b\&\b/,$string);

this seems to work as long as there's always a space before and after the part you don't want to split.
 
Ahh, i see the problem now with the '&.'

Your substitution will only work, in the case they enter the company "Bob & Jane"

What if its: "D&D Dishwashers" we have another problem.

DR: Change your delimiter
 
I realize there isn't a great way to do this. Any delimiter can possibly end up in a field. If this is coming from a form use CGI's param() function, and grab the fields that way.
 
yep that's true or get the person managing the web page sending the info to manipulate the company name (ie check for any &) and substitute it with something else before sending
 
i have ammended the sub as follows but my there is still always two fullstops at the end i.e. J.L.R..

sub shorten_initial()
{
@names = (split /\,/, $_[0]);
$init = "";
foreach $name (@names){
$init = $init . substr($name,0,1) . ".";}
$init=~s/\.\./\./g;
return $init;
}

Thanks for trying though
 
Thanks everybody for your great solutions. I was able to get it working by using substitution. For some reason we replace all spaces with the + sign so I also had to use tr/ to get rid of the + signs.

Thanks Again

DRapper
 
You all missed the entire point.

For some reason he has a URL string that should be properly encoded.

Had it been properly encoded none of this would be an issue. This is specifically why spaces are translated to '+' and '+' and '&' are escaped as per the RFC specification.

1) Whatever is generating this query string is bogus and incorrect
2) Use CGI.pm to parse the proper string.

This tr and substitution stuff is really the wrong way to be going about it. Get your input straight, use some solid tools like CGI.pm and save a ton of time.
 
Here's a perl subroutine I found that is really good at handling query strings and form POST and GET input. Place

Code:
&ReadParse(*in);

at the top of the script somewhere, then somewhere else, like at the bottom, put in this subroutine:

Code:
sub ReadParse {
 local (*in) = @_ if @_;  local ($i, $loc, $key, $val);
 if ($ENV{'REQUEST_METHOD'} eq "GET") { $in = $ENV{'QUERY_STRING'};} 
 elsif ($ENV{'REQUEST_METHOD'} eq "POST")
      {read(STDIN,$in,$ENV{'CONTENT_LENGTH'});}
 @in = split(/&/,$in);
 foreach $i (0 .. $#in) {
  $in[$i] =~ s/\+/ /g;    ($key, $val) = split(/=/,$in[$i],2);
  $key =~ s/%(..)/pack("c",hex($1))/ge;
  $val =~ s/%(..)/pack("c",hex($1))/ge;
  $in{$key} .= "\0" if (defined($in{$key})); 
  $in{$key} .= $val;  
  }  
 return 1; 
 }

When you want to get a value, say where the query string is
Code:
"?company=MyCompany&name=John Smith"
, the variable
Code:
$in{company}
would equal
Code:
MyCompany
, and the variable
Code:
$in{name}
would equal
Code:
John Smith
. It's a really easy way to deal with this without having to split each part individually.
 
This would not help him, his source string is corrupted.

It would give him the exact same results.
 
Siberian,

You are right in the fact if it had been written correctly I would not have to jump through hoops to make it work. Someone else wrote the code and I had very little time to fix it.

Thanks again for your advice.

DRapper
 
Sometimes its worth taking the hit to rewrite crap code, because if its landed at your door this time, people remeber who's good at dealing with this crap, and it keeps on coming, whereas if you take your time to rewrite it to a supportable standard, it won't keep coming back, and if it does it'll be for good reason

Just a thought
--Paul
 
Paul nailed this big time, this problem will keep biting you in the rear end everytime you have to deal with this code. Better to fix the real problem today then have to work around it for the next 2 years.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top