The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2012, 08:49:55 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Trim every element of an array at once?  (Read 1462 times)
KaptainKrayola
Keeper of Pie
Global Moderator
Lifer
*****
Offline Offline

Posts: 994



View Profile WWW
« on: May 29, 2007, 02:57:31 PM »

The Kaptain needs to trim every element of an array at the same time - any suggestions?

Yes, The Kaptain is posting in the correct forum he's actually doing stuff in PHP nowadays. 

<edit>

looking for a non-loop-over-every-element solution if possible

</edit>
« Last Edit: May 29, 2007, 03:01:27 PM by KaptainKrayola » Logged

We can't stop here, this is bat country.
hydra
Rookie
**
Offline Offline

Posts: 15



View Profile
« Reply #1 on: May 29, 2007, 03:12:04 PM »

array_map function -> write either a customised trim or use the standard trim()
Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 9896



View Profile
« Reply #2 on: May 29, 2007, 03:27:44 PM »

Hmmm... I think the fastest method I can imagine is to do a preg_replace, with your array being the third parameter. Consider:

$newArr = preg_replace('/^[\s]*(.*)[\s]*$/', '$1', $inputArr)

Although I'm not certain of my regex syntax, I'm using a regex that looks for anything inside of optional white space on the outsides of the string, and specifying the $1 back references (the inner goop) as the replacement. preg_match has the handy ability to take and return arrays as params, so in this case, that regex will be applied to every element in $inputArr and $newArr will contain the results. Interestingly, you can supply arrays as parameters 1 and 2 as well, so you could have multiple search/replace instances happening against every element in an array... and since this is a single line of code and it's all in complied code nder the hood, this would be just ridiculously fast.

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
hydra
Rookie
**
Offline Offline

Posts: 15



View Profile
« Reply #3 on: May 29, 2007, 04:50:37 PM »

Perk

Apologies in advance <- n00b

Just ran tests > 10000 keywords

<edit> 0 > 10,000 </edit>

Up to 10000 lines array_map seems 4/5x quicker than preg_match (in this instance)? for trimming whitespace at least

Code:
<?php
$var
=file_get_contents("keywords.txt");
$var=explode("\n"$var);
$new = array();
foreach (
$var as $line=>$data)
array_push($new$data); }
$result array_map("randspace",$new);

function 
randspace($var)
{ return 
str_repeat(" ",rand(1,11)).$var.str_repeat(" ",rand(1,11));}

$start microtime();
$res array_map(trim,$new);
$finish microtime();
echo 
"<br />";

echo 
"Array Map : ".($finish $start);
echo 
"<br />";
$start microtime();
$newArr preg_replace('/^[\s]*(.*)[\s]*$/''$1'$result);
$finish microtime();
echo 
"Preg Match : ".($finish $start);
?>


Array Map : 0.014735
Preg Match : 0.057089

<edit>
Original questions was fastest with non-loop - this is kind of 'loopy' unless put in method
</edit>
« Last Edit: May 29, 2007, 06:26:12 PM by hydra » Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 9896



View Profile
« Reply #4 on: May 29, 2007, 04:57:47 PM »

NEVER apologize for a post like that... that's some of the best we've seen here - really nice work Hydra.

And that actually leads to a thought that Dirk had which I will be posting on tonight regarding benchmarking languages and techniques. I'll flesh that out tonight, but the fact that you did this right now is a fantastic segue.

Thanks!
/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #5 on: May 30, 2007, 04:34:23 AM »

And a pretty damn good example of how to profile a PHP app too.

Damn fine work hydra, haven't said G'day yet so...... please allow myself to introduce......... myself.

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
KaptainKrayola
Keeper of Pie
Global Moderator
Lifer
*****
Offline Offline

Posts: 994



View Profile WWW
« Reply #6 on: May 30, 2007, 09:11:21 AM »

w00t! Hydra is the Kaptain's new hero of the moment.  Thanks for that code - works like a charm.

Ok- back to the cave that is our rebuilding process.

The Kaptain will be back next time he wants code help Tongue
Logged

We can't stop here, this is bat country.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 9896



View Profile
« Reply #7 on: May 30, 2007, 09:13:23 AM »

== CodeMooch  ROFLMAO
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
KaptainKrayola
Keeper of Pie
Global Moderator
Lifer
*****
Offline Offline

Posts: 994



View Profile WWW
« Reply #8 on: May 30, 2007, 09:53:24 AM »

You know the Kaptain well.  Why figure it out for yourself when you have THE CACHE?  ROFLMAO
Logged

We can't stop here, this is bat country.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!