Re: [PHP] Breaking up data efficiently
Kevin L'Huillier wrote: ... Create the function split_location to return the associative array or, if it is invalid, throws an exception. If PHP4, replace the exception with similar error handling. If efficiency is really the goal, then the OP probably shouldn't go the route of Exceptions. Just use a simple trigger_error() and your php.ini to direct where the error messages go. Especially in this case... because errors here are likely just because someone added / removed an extra | where it didn't belong. In my opinion you should save exceptions for, well, EXCEPTIONal problems. Either that or you use exceptions for a large system that discriminates against specific types of errors and handles each error type in a totally different way. Can we say bloat? -- NEW? | http://www.catb.org/~esr/faqs/smart-questions.html STFA | http://marc.theaimsgroup.com/?l=php-generalw=2 STFM | http://php.net/manual/en/index.php STFW | http://www.google.com/search?q=php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Breaking up data efficiently
In my opinion you should save exceptions for, well, EXCEPTIONal problems. Either that or you use exceptions for a large system that discriminates against specific types of errors and handles each error type in a totally different way. Can we say bloat? Depending on the system, malformatted data may very well be exceptional. (adj. 1. being an exception; uncommon) Judging by the lack of error-checking in the OP, it seems to be the case. Using trigger_error or return false would both be reasonable solutions as well. Really, how errors are handled is up to the individual. My point was that Wee Keat may consider adding a few lines for validation. Certainly adding any error-checking will reduce efficiency, which is what the original question was regarding. (As an aside, some day i will have to find out how many times the word bloat appears on this list.) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Breaking up data efficiently
On Jun 28, 2005, at 9:40 AM, Kevin L'Huillier wrote: In my opinion you should save exceptions for, well, EXCEPTIONal problems. Either that or you use exceptions for a large system that discriminates against specific types of errors and handles each error type in a totally different way. Can we say bloat? Depending on the system, malformatted data may very well be exceptional. (adj. 1. being an exception; uncommon) Judging by the lack of error-checking in the OP, it seems to be the case. Using trigger_error or return false would both be reasonable solutions as well. Really, how errors are handled is up to the individual. My point was that Wee Keat may consider adding a few lines for validation. Certainly adding any error-checking will reduce efficiency, which is what the original question was regarding. [snip] I would think that error checking would increase efficiency, ie. the script is more likely to work as expected. Eh? ;) Edward Vermillion [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Breaking up data efficiently
Hi All, I'm working on an aircraft booking system and it has multiple origin/destination data, concatenated into a single line: /* BEGIN DATA */ Melbourne, AU, 21-07-2005 14:00:00|Perth, AU, 21-07-2005 18:00:00|Perth, AU, 25-07-2005 14:00:00|Melbourne, AU, 25-07-2005 18:00:00 /* END DATA */ As you can see, each origin/destination is separated by a pipe '|', and then, each origin/destination data has its location, country and datetime of departure/arrival, separated by comma ','. I'm splitting them up into array of location, country and datetime using the following: /* BEGIN CODE */ $itenary = explode('|', $booking-booking_flight_details); $size = count($itenary); for($i=0; $i $size; $i++) { list($path[$i]['location'], $path[$i]['country'], $path[$i]['datetime']) = explode(',', $itenary[$i]); } /* END CODE */ *Question*: Is the above the code an effective way to do it? Or is there a better/faster way? Somehow, it feels like there's lots of things going through the above code. Please advise. Thanks. -- Wee Keat Chin Protocol Networks p: 1300 131 932 e: [EMAIL PROTECTED] h: www.pn.com.au -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Breaking up data efficiently
On Tue, 28 Jun 2005, Wee Keat wrote: Hi All, I'm working on an aircraft booking system and it has multiple origin/destination data, concatenated into a single line: /* BEGIN DATA */ Melbourne, AU, 21-07-2005 14:00:00|Perth, AU, 21-07-2005 18:00:00|Perth, AU, 25-07-2005 14:00:00|Melbourne, AU, 25-07-2005 18:00:00 /* END DATA */ As you can see, each origin/destination is separated by a pipe '|', and then, each origin/destination data has its location, country and datetime of departure/arrival, separated by comma ','. I'm splitting them up into array of location, country and datetime using the following: /* BEGIN CODE */ $itenary = explode('|', $booking-booking_flight_details); $size = count($itenary); for($i=0; $i $size; $i++) { list($path[$i]['location'], $path[$i]['country'], $path[$i]['datetime']) = explode(',', $itenary[$i]); } /* END CODE */ *Question*: Is the above the code an effective way to do it? Or is there a better/faster way? I suppose it depends on how many records you're going to have to split up... here's another way, but I don't know if it's faster -- I'll let you time it -- and it's certainly not as readable... $bits = split([|,], $booking-booking_flight_details); $size = count($itenary); for ( $i = 0; $i $size; $i += 3 ) { $path[$i]['location'] = $bits[$i]; $path[$i]['country'] = $bits[$i + 1]; $path[$i]['datetime'] = $bits[$i + 2]; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Breaking up data efficiently
Hi, Tuesday, June 28, 2005, 9:34:28 AM, you wrote: WK Hi All, WK I'm working on an aircraft booking system and it has multiple WK origin/destination data, concatenated into a single line: WK /* BEGIN DATA */ WK Melbourne, AU, 21-07-2005 14:00:00|Perth, AU, 21-07-2005 18:00:00|Perth, WK AU, 25-07-2005 14:00:00|Melbourne, AU, 25-07-2005 18:00:00 WK /* END DATA */ WK As you can see, each origin/destination is separated by a pipe '|', and WK then, each origin/destination data has its location, country and WK datetime of departure/arrival, separated by comma ','. WK I'm splitting them up into array of location, country and datetime using WK the following: WK /* BEGIN CODE */ WK $itenary = explode('|', $booking-booking_flight_details); WK $size = count($itenary); WK for($i=0; $i $size; $i++) { WK list($path[$i]['location'], WK $path[$i]['country'], WK $path[$i]['datetime']) = explode(',', $itenary[$i]); WK } WK /* END CODE */ WK *Question*: Is the above the code an effective way to do it? Or is there WK a better/faster way? WK Somehow, it feels like there's lots of things going through the above code. WK Please advise. Thanks. This ay work: ? $str = 'Melbourne, AU, 21-07-2005 14:00:00|Perth, AU, 21-07-2005 18:00:00|Perth, AU, 25-07-2005 14:00:00|Melbourne, AU, 25-07-2005 18:00:00'; preg_match_all('/(\w+),\s*(\w+),\s*([0-9-]+)\s([0-9:]+)(?=\|)/s',$str,$match); print_r($match); -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re[2]: [PHP] Breaking up data efficiently
Hi, Tuesday, June 28, 2005, 10:26:12 AM, you wrote: TR This ay work: TR ? TR $str = 'Melbourne, AU, 21-07-2005 14:00:00|Perth, AU, TR 21-07-2005 18:00:00|Perth, AU, 25-07-2005 14:00:00|Melbourne, AU, TR 25-07-2005 18:00:00'; TR preg_match_all('/(\w+),\s*(\w+),\s*([0-9-]+)\s([0-9:]+)(?=\|)/s',$str,$match); TR print_r($match); A different way that will generate what you actually need :) function build($data){ global $path; $i = count($path); $path[$i]['location'] = $data[1]; $path[$i]['country'] = $data[2]; $path[$i]['datetime'] = $data[3]; } $path = array(); $str = 'Melbourne, AU, 21-07-2005 14:00:00|Perth, AU, 21-07-2005 18:00:00|Perth, AU, 25-07-2005 14:00:00|Melbourne, AU, 25-07-2005 18:00:00'; preg_replace_callback('/(\w+),\s*(\w+),\s([0-9-]+\s[0-9:]+)(?=\|)/s','build',$str); print_r($path); -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Breaking up data efficiently -- closed
Hi all, Thanks for all the input. Really an eye opener on all the various ways that could do the same thing! :) Really appreciate it. Off to work with new ideas! Tom Rogers wrote: function build($data){ global $path; $i = count($path); $path[$i]['location'] = $data[1]; $path[$i]['country'] = $data[2]; $path[$i]['datetime'] = $data[3]; } $path = array(); $str = 'Melbourne, AU, 21-07-2005 14:00:00|Perth, AU, 21-07-2005 18:00:00|Perth, AU, 25-07-2005 14:00:00|Melbourne, AU, 25-07-2005 18:00:00'; preg_replace_callback('/(\w+),\s*(\w+),\s([0-9-]+\s[0-9:]+)(?=\|)/s','build',$str); print_r($path); Jasper Bryant-Greene wrote: $itinerary = explode('|', $booking-booking_flight_details); foreach($itinerary as $item) { $item = explode(',', $item); $path[] = array( 'location' = $item[0], 'country' = $item[1], 'datetime' = $item[2] ); } Philip Hallstrom wrote: $bits = split([|,], $booking-booking_flight_details); $size = count($itenary); for ( $i = 0; $i $size; $i += 3 ) { $path[$i]['location'] = $bits[$i]; $path[$i]['country'] = $bits[$i + 1]; $path[$i]['datetime'] = $bits[$i + 2]; } -- Wee Keat Chin Protocol Networks p: 1300 131 932 e: [EMAIL PROTECTED] h: www.pn.com.au -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Breaking up data efficiently
I agree, your implementation is efficient. It does depend on the data being entirely proper, however. If you can not be entirely sure (and you should rarely be), i might add a suggestion that increases code length, but decreases the chance of problems: You might consider creating a function to validate the location as well as list() = explode() it. $locations = explode('|', $booking-booking_flight_details); foreach ($locations as $location) $list[] = split_location($location); Create the function split_location to return the associative array or, if it is invalid, throws an exception. If PHP4, replace the exception with similar error handling. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php