Current location: Hot Scripts Forums » Programming Languages » Perl » Help with split please


Help with split please

Reply
  #1 (permalink)  
Old 09-27-06, 04:25 PM
vbsaltydog vbsaltydog is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Help with split please

I am trying to split the results of an sql query but I am having some issues.

I am trying to use the results of the query as a conditional argument in an if statement and it works for the most part however, some of the results from the query have comma delimited values and some do not. When I apply the if statement later in the program it works when the results of this code show a single value but it does not work when the results of this query show a comma seperated result so I am trying to split the results of this query on any commas and return the results comma free and as seperate array elements.

__________________________________________________ ____________
Code:
$sql_statement ="SELECT product_xcat FROM product";

@cm_xcat = database_call('product','SELECT',$sql_statement);

foreach $row(@cm_xcat) {

($cm_xcat) = @$row; 

@cm_xcat_results = split(/,/, $cm_xcat);

} ######## End of foreach statement.
__________________________________________________ __________

This currently only removes the commas but does not return the comma delimited values as seperate array elements.

Thanks to everyone for your assistance,

-vbsaltydog

Last edited by nico_swd; 09-27-06 at 04:33 PM.
Reply With Quote
  #2 (permalink)  
Old 09-27-06, 05:35 PM
Drunken Perl Coder Drunken Perl Coder is offline
Wannabe Coder
 
Join Date: Aug 2006
Posts: 110
Thanks: 0
Thanked 0 Times in 0 Posts
instead of :

Code:
@cm_xcat_results = split(/,/, $cm_xcat);
maybe:

Code:
push @cm_xcat_results,[ split(/,/, $cm_xcat) ];
that should create an array of annonymous arrays.

Or maybe:

Code:
push @cm_xcat_results, split(/,/, $cm_xcat);
which should create one long array instead of an array of arrays.

if you need to keep adding to an array you have to use the "push" function, otherwise each time you assign values to an array with "=" you overwrite the existing array.
Reply With Quote
  #3 (permalink)  
Old 09-27-06, 06:46 PM
vbsaltydog vbsaltydog is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Those did not work very well. I am thinking that since my array output is something like:

element_1element_2element_3element_4part1,element_ 4part2element_5element_6

and I need the array to have no commas and no grouping (every item pulled from the db query to be its own array element even if they came from the same db field then perhaps it would be smart to join the array elements on a comma and then split the array elements on a comma and store in a new array?

This should eliminate all commas and grouping in the final output array shouldnt it?

-vbsaltydog
Reply With Quote
  #4 (permalink)  
Old 09-27-06, 08:10 PM
Drunken Perl Coder Drunken Perl Coder is offline
Wannabe Coder
 
Join Date: Aug 2006
Posts: 110
Thanks: 0
Thanked 0 Times in 0 Posts
run your code and after this line:

($cm_xcat) = @$row;

add:

print "$cm_xcat\n";

so I can what this data looks like. If you are running this as a CGI print an appropriate HTTP header first.
Reply With Quote
  #5 (permalink)  
Old 09-27-06, 08:22 PM
vbsaltydog vbsaltydog is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
I was digging around a bit and hope to have found an easier way to fix the issue but I dont know how to alter the info.

I am pulling db info based on conditions in the WHERE clause however, the field that the WHERE clause is looking is the problem field. If the field holds a single category name then my code works great but if the field holds more than one category name seperated by commas then it breaks my code. I cant edit the field values directly in the db as the rest of the cart uses this field and its values seperated by commas if more than one value is present.

So the question is....how do I alter the contents of the field product_xcat before the WHERE clause reads it in the code below?

Code:
$sql_statement =" 

SELECT product.product_id, product.product_name, product.product_imgsm,
product.product_xcat, category.category_header_name,
category.category_imgsm FROM product JOIN category ON
product.product_xcat=category.category_id 
WHERE product_xcat like $dbins_fd_ref ORDER BY RAND($product_name) limit 3 

";

my @header = database_call('product','SELECT',$sql_statement);
Reply With Quote
  #6 (permalink)  
Old 09-28-06, 12:55 AM
Drunken Perl Coder Drunken Perl Coder is offline
Wannabe Coder
 
Join Date: Aug 2006
Posts: 110
Thanks: 0
Thanked 0 Times in 0 Posts
That I don't know.
Reply With Quote
  #7 (permalink)  
Old 09-28-06, 03:05 PM
Rando Rando is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
WHERE product_xcat LIKE "%$dbins_fd_ref%" ORDER BY RAND($product_name) limit 3
__________________
Full root access starting at $7.95/mo VPSLink
Reply With Quote
  #8 (permalink)  
Old 10-01-06, 12:48 PM
vbsaltydog vbsaltydog is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Rando, tried this ...no results returned. Tried it with double and single quotes.

I know how to get this working but I dont know the method to get my variable. Let me explain. The db field product_xcat is the field where the comma separated values live. The $dbins_fd_ref value is always a single value.
I need to pull the value of the product_xcat field and store this in a scalar variable prior to running my select statement that this thread is built on, then I should be able to split the scalar on the commas and use the array elements to match my $dbins_fd_ref against.

So my question is...How do I run a select statement and store the results into a variable that is available to another select statement within the same subroutine or same block of code?

Any help is appreciated.
Reply With Quote
  #9 (permalink)  
Old 10-01-06, 03:18 PM
vbsaltydog vbsaltydog is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
I resolved this issue. It was a problem with a compound sql query. If you are reading this thread and need the solution please email me. I would write out the solution but it is lengthy and detailed.

Thanks for the help guys!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
After 5 rows in MySQL, split the table *more to it* Optimal PHP 4 04-21-06 12:07 PM
Help needed to split a text into words based on their meaning.. srik4u Script Requests 2 04-17-06 01:44 PM
Split crmpicco Visual Basic 0 05-12-05 05:48 AM
Split categories list in half mp04 Perl 0 04-17-05 07:51 PM
what is the meaning of Split session? Han84 ASP 1 08-25-03 10:45 PM


All times are GMT -5. The time now is 04:42 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.