By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,441 Members | 979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,441 IT Pros & Developers. It's quick & easy.

Change alternate commas to semi colons...

P: n/a
With a string of authors such as:

Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen,
Shawyer, Andrea

I would like to write a function to change every other comma into a
semi colon (thereby defining where one name ends and the next begins).

I could do it by writing a complex (and slow) procedure - but is there
a quick way of doing it using regular expressions, for example?

Many thanks.

Jun 20 '06 #1
Share this Question
Share on Google+
27 Replies


P: n/a

St***********@gmail.com wrote:
With a string of authors such as:

Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen,
Shawyer, Andrea

I would like to write a function to change every other comma into a
semi colon (thereby defining where one name ends and the next begins).

I could do it by writing a complex (and slow) procedure - but is there
a quick way of doing it using regular expressions, for example?

Many thanks.


Why not just use str_replace() intrinsic function?

$str = "Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev,
Karen, Shawyer, Andrea";
$str = str_replace(",", ";", $str);
echo $str;

Bernhard Enders.

Jun 20 '06 #2

P: n/a
Rik
St***********@gmail.com wrote:
With a string of authors such as:

Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen,
Shawyer, Andrea

I would like to write a function to change every other comma into a
semi colon (thereby defining where one name ends and the next begins).

I could do it by writing a complex (and slow) procedure - but is there
a quick way of doing it using regular expressions, for example?

Regular expressions can be used, I'd think this is faster:

$array = explode(',', $authors);
$new_authors = '';
$i = 0;
while(!empty($array)){
$delimter = (($i % 2)==0) ? ',' : ';';
$new_authors .= array_shift($array).$delimiter;
$i++;
//or, to get an array:
//$new_authors[] = array_shift($array).','.array_shift($array);
}
If you want a regular expression (which almost certainly will be slower):
$authors = preg_replace('/([^,]*),([^,]*),/s','$1,$2;',$authors);

Similar, to get them directly in an array:
preg_match_all('/([^,]*,){2}/s',$authors,$matches);
Grtz,
--
Rik Wasmus
Jun 20 '06 #3

P: n/a
Rik
bgeneto wrote:
Why not just use str_replace() intrinsic function St***********@gmail.com wrote:
I would like to write a function to change every other comma into a

----------------------------------------------------^^^^^

Grtz,
--
Rik Wasmus
Jun 20 '06 #4

P: n/a
bgeneto wrote:
St***********@gmail.com wrote:
With a string of authors such as:

Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen,
Shawyer, Andrea

I would like to write a function to change every other comma into a
semi colon (thereby defining where one name ends and the next begins).

I could do it by writing a complex (and slow) procedure - but is there
a quick way of doing it using regular expressions, for example?

Many thanks.


Why not just use str_replace() intrinsic function?

$str = "Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev,
Karen, Shawyer, Andrea";
$str = str_replace(",", ";", $str);
echo $str;

Bernhard Enders.


Because he wants to change every *other* comma into a semi-colon (i.e.
change the second, fourth etc).
Your function would change *every* comma into a semi-colon.

I don't think you can easily do it with regex as you can't really
differentiate between comma's.
I would suggest something along the lines of:

$array = explode ( ', ', $string );
$arraycount = count($array);
$new_string = '';

for( $i=0; $i<$arraycount; $i++; ) {
if ((1&$i)) {
//$i is odd
$new_string .= $array[$i] . ', ';
}
elseif (!(1&$num)) {
//$i is even
$new_string .= $array[$i] . '; ';
}
}
$string = $new_string;
unset($new_string);

Hope this helps,
Juliette
Jun 20 '06 #5

P: n/a
Juliette wrote:
I don't think you can easily do it with regex as you can't really
differentiate between comma's.
I would suggest something along the lines of:

$array = explode ( ', ', $string );
$arraycount = count($array);
$new_string = '';

for( $i=0; $i<$arraycount; $i++; ) {
if ((1&$i)) {
//$i is odd
$new_string .= $array[$i] . ', ';
}
elseif (!(1&$num)) {
//$i is even
$new_string .= $array[$i] . '; ';
}
}
$string = $new_string;
unset($new_string);


I would use temporary arrays and the implode() function, and a switch
statement instead of the if/elseif:

<?php
$string = "Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev,
Karen, Shawyer, Andrea";
$array = explode ( ', ', $string );
$tmp = array();
$tmp2 = array();
for( $i=0; $i<count($array); $i++ ) {
switch ($i % 2) {
case 0:
$tmp2[] = trim($array[$i]);
break;
case 1:
$tmp2[] = trim($array[$i]);
$tmp[] = implode(', ',$tmp2);
$tmp2 = array();
break;
}
}
echo 'Before: ' . $string . "\n";
$string = implode('; ',$tmp);
echo 'After: ' . $string;
?>

Ken

Jun 20 '06 #6

P: n/a
Rik (lu************@hotmail.com) wrote:
: St***********@gmail.com wrote:
: > With a string of authors such as:
: >
: > Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen,
: > Shawyer, Andrea
: >
: > I would like to write a function to change every other comma into a
: > semi colon (thereby defining where one name ends and the next begins).
: >
: > I could do it by writing a complex (and slow) procedure - but is there
: > a quick way of doing it using regular expressions, for example?
: Regular expressions can be used, I'd think this is faster:

I wouldn't assume that a block of php code is faster than simply calling
preg_replace. In fact I wouldn't be surprised if it's the other way round
- preg_replace may be faster.

preg_replace maps to a function written in C that does some string
manipulations (all code written in C). All php does internally is to set
up the parameters and call that underlying function.

The block of php code, on the other hand, maps to a whole bunch of
manipulations of php data structures driven by the php interpretter, each
one of which likely involves setting up parameters and calling some
underlying function to do the work.

I would be interested to see the result of a proper comparison between the
two.

Jun 20 '06 #7

P: n/a
St***********@gmail.com wrote:
With a string of authors such as:

Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen,
Shawyer, Andrea

I would like to write a function to change every other comma into a
semi colon (thereby defining where one name ends and the next begins).

I could do it by writing a complex (and slow) procedure - but is there
a quick way of doing it using regular expressions, for example?

Many thanks.

Something like the following should do:

$names = ereg_replace("\([^,]*,[^,]*\),", "\\1;", $names)
Jun 20 '06 #8

P: n/a
Thanks for all the excellent suggestions!

Best regards,
Steve

Jun 20 '06 #9

P: n/a
On Tue, 20 Jun 2006 14:10:48 -0800, Malcolm Dew-Jones wrote:
: Regular expressions can be used, I'd think this is faster:

I wouldn't assume that a block of php code is faster than simply calling
preg_replace. In fact I wouldn't be surprised if it's the other way round
- preg_replace may be faster.

I would be interested to see the result of a proper comparison between the
two.


Ask and ye shall receive:

I've done a small script that runs each version 5000 times (never guess
which is faster, always test). And the results are:

Function call: 0.0749678611755 seconds
PREG Replace: 0.0623338222504 seconds

So the preg version is faster (it completes in 83% of the time).

Now, there's another upside. In the way that Rik coded them, there's also
a subtle difference in the two outputs. The Function call version puts an
extra ; on the end (which would result in an empty element if exploded and
would therefore need to be tested for before creating a DB row or the like).

Also, that test version used the original authors string. If I make this
string 10 times as long, the times are as follows:

Function call: 0.81055188179 seconds
PREG Replace: 0.196034908295 seconds

So, the preg version absolutely whips the function call (completes in 24%
of the time).

As much as I'd advocate testing over guesswork, I agree with your
understanding of why it's much faster.

Hope this is interesting to someone.

Cheers,
Andy

--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Jun 21 '06 #10

P: n/a
On Tue, 20 Jun 2006 21:41:52 +0200, Juliette wrote:
Because he wants to change every *other* comma into a semi-colon (i.e.
change the second, fourth etc).
Your function would change *every* comma into a semi-colon.

I don't think you can easily do it with regex as you can't really
differentiate between comma's.
Hi Juliette,

As Rik already posted, you certainly can do it with regexes and as I
posted, they're much faster than the function call.

There's a syntax error in your script:
for( $i=0; $i<$arraycount; $i++; ) {
should be:
for( $i=0; $i<$arraycount; $i++ ) {


And a bug that actually gives the incorrect output. The first part of the
string from your function is:

Carson; David, Milne; Rebecca,

And it should be:

Carson, David; Milne, Rebecca;

For reference, against the larger data set from my timing post, your
function completes in 0.5357 seconds (compared to 0.8106 for Rik's
function version and 0.1960 for Rik's regex).

Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Jun 21 '06 #11

P: n/a
On Wed, 21 Jun 2006 08:09:39 +0000, Andy Jeffries wrote:
And a bug that actually gives the incorrect output. The first part of the
string from your function is:

Carson; David, Milne; Rebecca,

And it should be:

Carson, David; Milne, Rebecca;


And it also has the same deficiency as Rik's function version in that it
adds an extra delimiter on the end.

Cheers,
Andy

--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Jun 21 '06 #12

P: n/a
On Tue, 20 Jun 2006 13:14:20 -0700, Ken Robinson wrote:
I would use temporary arrays and the implode() function, and a switch
statement instead of the if/elseif:


Hi Ken,

OK, your function is the slowest of those posted so far, completing the
larger data set from my timing post in 0.9498 seconds (compared to 0.5357
seconds for Juliette, 0.8106 for Rik's function version and
0.1960 for Rik's regex).

But yours also has no errors and produces the correct output (with the
only other one being Rik's regex version).

Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Jun 21 '06 #13

P: n/a
Andy Jeffries wrote:
On Tue, 20 Jun 2006 14:10:48 -0800, Malcolm Dew-Jones wrote:
: Regular expressions can be used, I'd think this is faster:

I wouldn't assume that a block of php code is faster than simply calling
preg_replace. In fact I wouldn't be surprised if it's the other way
round - preg_replace may be faster.

I would be interested to see the result of a proper comparison between
the two.
[snip] Also, that test version used the original authors string. If I make this
string 10 times as long, the times are as follows:

Function call: 0.81055188179 seconds
PREG Replace: 0.196034908295 seconds

So, the preg version absolutely whips the function call (completes in 24%
of the time).

As much as I'd advocate testing over guesswork, I agree with your
understanding of why it's much faster.


Indeed preg_replace has the advantage of doing its looping in compiled code,
but throwing around with arrays is not cheap, so I think the methods are
too different to make a fair comparison.

I would think that in this case, since the logic is quite simple, doing a
straightforward character by character sweep, just like preg_replace would
do for its pattern matching, ought to be faster, unless PHP really really
*really* stinks at doing loops in phpcode of course.

Can you run the same test with this code:

$alternate_comma = false;
$length = strlen($authors);
for ($i=0; $i<$length; $i++) {
if ($authors[$i]==',') {
if ($alternate_comma) $authors[$i] = ';';
$alternate_comma = !$alternate_comma;
}
}

Usually I would use strpos for this sort of thing, which could be even
faster, since it will shorten the looping in php, but still would depend on
how well php does it.

--
/Bent
Jun 21 '06 #14

P: n/a
Message-ID: <pa****************************@andyjeffries.co.uk > from
Andy Jeffries contained the following:
As much as I'd advocate testing over guesswork, I agree with your
understanding of why it's much faster.

Hope this is interesting to someone.


It is to me, I'd always thought regexs were slower. Bugger, that means
I will /really/ have to get round to learning them.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jun 21 '06 #15

P: n/a
Rik
Bent Stigsen wrote:
Andy Jeffries wrote:
On Tue, 20 Jun 2006 14:10:48 -0800, Malcolm Dew-Jones wrote:
Regular expressions can be used, I'd think this is faster:

I wouldn't assume that a block of php code is faster than simply
calling preg_replace. In fact I wouldn't be surprised if it's the
other way round - preg_replace may be faster.

I would be interested to see the result of a proper comparison
between the two.
[snip]
Also, that test version used the original authors string. If I make
this string 10 times as long, the times are as follows:

Function call: 0.81055188179 seconds
PREG Replace: 0.196034908295 seconds

So, the preg version absolutely whips the function call (completes
in 24% of the time).

As much as I'd advocate testing over guesswork, I agree with your
understanding of why it's much faster.


Yup, I should've tested it, somtimes lazyness will make you look like a fool
:-)

Testing here, 5000 times, string 4 times as long:
Regex does 1.0872719287872
Can you run the same test with this code:

$alternate_comma = false;
$length = strlen($authors);
for ($i=0; $i<$length; $i++) {
if ($authors[$i]==',') {
if ($alternate_comma) $authors[$i] = ';';
$alternate_comma = !$alternate_comma;
}
}
Does it in 6.8535070419312
Usually I would use strpos for this sort of thing, which could be even
faster, since it will shorten the looping in php, but still would
depend on how well php does it.


strpos() example:
$a = 0;
$bool = false;
while(strpos($authors,',',$a)!== false){
$a = strpos($authors,',',$a);
if($bool) $authors[$a] = ';';
$a++;
$bool = !$bool;
}

This manages it in 2.1639029979706

Regex still seems to win this.
I thought about using array_reduce(explode(',',$authors),
'some_custom_function'), but that also doesn't even get close.

Grtz,
--
Rik Wasmus
Jun 21 '06 #16

P: n/a
Rik
Geoff Berrow wrote:
Message-ID: <pa****************************@andyjeffries.co.uk > from
Andy Jeffries contained the following:
As much as I'd advocate testing over guesswork, I agree with your
understanding of why it's much faster.

Hope this is interesting to someone.


It is to me, I'd always thought regexs were slower. Bugger, that
means I will /really/ have to get round to learning them.


Yup, I'm quite capable to produce some regexes, but in all testcases I saw
where it could be done without, regexes seemed slower... Time to test some
past code.

Grtz,
--
Rik Wasmus
Jun 21 '06 #17

P: n/a
Rik wrote:
Bent Stigsen wrote:
Andy Jeffries wrote:
On Tue, 20 Jun 2006 14:10:48 -0800, Malcolm Dew-Jones wrote:
> Regular expressions can be used, I'd think this is faster:

I wouldn't assume that a block of php code is faster than simply
calling preg_replace. In fact I wouldn't be surprised if it's the
other way round - preg_replace may be faster.

I would be interested to see the result of a proper comparison
between the two. [snip]
Also, that test version used the original authors string. If I make
this string 10 times as long, the times are as follows:

Function call: 0.81055188179 seconds
PREG Replace: 0.196034908295 seconds

So, the preg version absolutely whips the function call (completes
in 24% of the time).

As much as I'd advocate testing over guesswork, I agree with your
understanding of why it's much faster.
Yup, I should've tested it, somtimes lazyness will make you look like a
fool
:-)


Acceptable tradeoff :)

Testing here, 5000 times, string 4 times as long:
Regex does 1.0872719287872
Can you run the same test with this code:

$alternate_comma = false;
$length = strlen($authors);
for ($i=0; $i<$length; $i++) {
if ($authors[$i]==',') {
if ($alternate_comma) $authors[$i] = ';';
$alternate_comma = !$alternate_comma;
}
}
Does it in 6.8535070419312


Bugger. I'm a bit surprised it's slower, but that much. Useless piece of
[bleep].

Did you run it more than once. I mean, it couldn't have been some other app.
hogging the cpu, at that moment.

Usually I would use strpos for this sort of thing, which could be even
faster, since it will shorten the looping in php, but still would
depend on how well php does it.


strpos() example:
$a = 0;
$bool = false;
while(strpos($authors,',',$a)!== false){
$a = strpos($authors,',',$a);
if($bool) $authors[$a] = ';';
$a++;
$bool = !$bool;
}

This manages it in 2.1639029979706


You do strpos twice, and it can bee squeezed a bit.

Try this instead:

$a = -1;
$bool = true;
while (($a=strpos($authors, ',', $a+1))!==false)
if ($bool=!$bool) $authors[$a] = ';';

Regex still seems to win this.


There could allways be made a AlternateCommaReplacer module.

[snip]

--
/Bent
Jun 21 '06 #18

P: n/a
On Wed, 21 Jun 2006 13:32:50 +0200, Rik wrote:
So, the preg version absolutely whips the function call (completes in
24% of the time).

As much as I'd advocate testing over guesswork, I agree with your
understanding of why it's much faster.
Yup, I should've tested it, somtimes lazyness will make you look like a
fool
:-)


I agree with your sentence, but don't think it's the case here - your
regex is still the fastest on the block! And it's provoked an interesting
discussion (and maybe given a few people reason to eventually learn
regexes).
Can you run the same test with this code:


Does it in 6.8535070419312


Using my machine (for comparison with all the previous timings):

1.8554 seconds (with correct output).
Usually I would use strpos for this sort of thing, which could be even
faster, since it will shorten the looping in php, but still would depend
on how well php does it.


strpos() example:

This manages it in 2.1639029979706


I make it: 0.5243 seconds (within a margin of error the same as Juliette's
bit testing version, but with the correct output).

Cheers,
Andy

--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Jun 21 '06 #19

P: n/a
On Wed, 21 Jun 2006 12:10:34 +0100, Geoff Berrow wrote:
As much as I'd advocate testing over guesswork, I agree with your
understanding of why it's much faster.

Hope this is interesting to someone.


It is to me, I'd always thought regexs were slower. Bugger, that means I
will /really/ have to get round to learning them.


AFAIK, ereg* is slower than preg*, that may be where you got this from,
but the PCRE library is lightning fast.

I'd recommend Mastering Regular Expressions by Jeffrey Friedl (O'Reilly) -
it gets a bit mind-numbing in places (talking about the mechanics of how
different regex engines work) but it's a great reference and is quite
readable (given the subject matter).

Cheers,
Andy

--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Jun 21 '06 #20

P: n/a
On Wed, 21 Jun 2006 13:48:00 +0200, Rik wrote:
It is to me, I'd always thought regexs were slower. Bugger, that means
I will /really/ have to get round to learning them.


Yup, I'm quite capable to produce some regexes, but in all testcases I saw
where it could be done without, regexes seemed slower... Time to test some
past code.


I always prefer them as it's generally a lot more readable (a single line
of code compared to 10 lines of iteration). In most cases developer
comprehension is more important than CPU cycles (unless you're working
with masses of data, but that's not normal in a web system).

The obvious comment coming is "regexes are more readable, what a load of
BS" - but they are if you understand regexes, and if you don't - it's not
my fault you are lacking in a skill (with "you" being the developer coming
to look at my code for maintenance).

Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Jun 21 '06 #21

P: n/a
On Wed, 21 Jun 2006 15:00:41 +0200, Bent Stigsen wrote:
Bugger. I'm a bit surprised it's slower, but that much. Useless piece of
[bleep].

Did you run it more than once. I mean, it couldn't have been some other
app. hogging the cpu, at that moment.
I've run it more than once. In my latest set of timings I get:

PREG Replace: 0.1889 seconds
Bent's Function call: 1.8074 seconds
You do strpos twice, and it can bee squeezed a bit.

Try this instead:


Bent's 2nd version Function call: 0.3320 seconds

Much faster, nearly competitive with preg and it gives the correct output.

Cheers,
Andy

--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Jun 21 '06 #22

P: n/a
Andy Jeffries wrote:
On Wed, 21 Jun 2006 08:09:39 +0000, Andy Jeffries wrote:
And a bug that actually gives the incorrect output. The first part of the
string from your function is:

Carson; David, Milne; Rebecca,

And it should be:

Carson, David; Milne, Rebecca;


And it also has the same deficiency as Rik's function version in that it
adds an extra delimiter on the end.

Cheers,
Andy

Andy,

Oops.. I obviously posted too quickly (and yes, I didn't test the code
before posting, just did it off-hand).

Interesting to see the ensuing discussion this has brought. I normally
use preg myself, but just couldn't think of how to do this one. In that
respect I still have a nice challenge (regex) I'm working on myself
which I can't seem to get right.

Just in case someone is interested, you can find the corrected code from
my function below. I've also gotten rid of some of the 'fluff' which I
only put in to make the code easier to understand.
About the extra delimiter: that was not explicite in the requirements
given by the OP, so that's why I didn't take that into account. The
below function does however deal with that correctly, i.e. the way you
suggested it ought to work.

$array = explode ( ', ', $string );
$arraycount = ( count($array)-1 );
$string = '';

for( $i=0; $i<=$arraycount; $i++ ) {
$string .= ( $i === $arraycount ) ? ( $array[$i] ) : ( (1&$i) ? (
$array[$i] . '; ' ) : ( $array[$i] . ', ' ) );
}
unset( $array, $arraycount );

The regex will still win, but I would guess this is slightly faster than
my earlier function.

Grz, Juliette
Jun 21 '06 #23

P: n/a
On Wed, 21 Jun 2006 20:17:25 +0200, Juliette wrote:
Oops.. I obviously posted too quickly (and yes, I didn't test the code
before posting, just did it off-hand).
I tend to do the same thing, it's only because I have a particular
interest in optimising functions/code that I took an interest in this
thread.
Interesting to see the ensuing discussion this has brought. I normally
use preg myself, but just couldn't think of how to do this one. In that
respect I still have a nice challenge (regex) I'm working on myself
which I can't seem to get right.
That's a good point, I think Rik's regex is probably optimal (and indeed
that's the way I'd have done it) but is there a faster regex?
Just in case someone is interested, you can find the corrected code from
my function below. I've also gotten rid of some of the 'fluff' which I
only put in to make the code easier to understand. About the extra
delimiter: that was not explicite in the requirements given by the OP,
so that's why I didn't take that into account. The below function does
however deal with that correctly, i.e. the way you suggested it ought to
work.
Cool, tested - result below :-)
The regex will still win, but I would guess this is slightly faster than
my earlier function.


Indeed it is, this function runs in 0.4208 seconds making it the second
fasted non-regex function.

Cheers,
Andy

--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Jun 21 '06 #24

P: n/a
Andy Jeffries wrote:
On Wed, 21 Jun 2006 20:17:25 +0200, Juliette wrote:
The regex will still win, but I would guess this is slightly faster than
my earlier function.


Indeed it is, this function runs in 0.4208 seconds making it the second
fasted non-regex function.


Cool ! ;-)
I've looked at Andy's to see what he does and bleeping heck, that's a
hard one to get my head round, but I can see why that would be faster.
Changing my function to a while loop would probably take yet some more
of the timing, but I haven't really got the time to do so now.

Regex wins the show.

Grz, Juliette
Jun 21 '06 #25

P: n/a
Juliette wrote:
Andy Jeffries wrote:
On Wed, 21 Jun 2006 20:17:25 +0200, Juliette wrote:
The regex will still win, but I would guess this is slightly faster than
my earlier function.
Indeed it is, this function runs in 0.4208 seconds making it the second
fasted non-regex function.


Thanks for doing all the stats so far, hope you have time for two more :)
Cool ! ;-)
I've looked at Andy's to see what he does and bleeping heck, that's a
hard one to get my head round, but I can see why that would be faster.
I think you can save a few jiffies by skipping the last entry in the array,
and just add it in the end. Saves you a comparison in the loop.

Like:

$array = explode ( ', ', $string );
$arraycount = ( count($array)-1 );
$string = '';
for( $i=0; $i<$arraycount; $i++ ) {
$string .= ((1&$i) ? ( $array[$i] . '; ' ) : ( $array[$i] . ', ' ));
}
$string .= $array[$i];

Changing my function to a while loop would probably take yet some more
of the timing, but I haven't really got the time to do so now.


$array = explode ( ', ', $string );
$string = current($array);
$x1 = null;
$x2 = next($array);
while ($x1=next($array)) {
$string .= ', '. $x2 . '; ' . $x1;
$x2 = next($array);
}
$string .= ', ' . $x2;
So many ways. The thread should have been called "50 ways to change..."
--
/Bent
Jun 21 '06 #26

P: n/a
On Wed, 21 Jun 2006 21:39:26 +0200, Bent Stigsen wrote:
On Wed, 21 Jun 2006 20:17:25 +0200, Juliette wrote:
The regex will still win, but I would guess this is slightly faster
than my earlier function.

Indeed it is, this function runs in 0.4208 seconds making it the second
fasted non-regex function.
Thanks for doing all the stats so far, hope you have time for two more :)
Sue :-)
I think you can save a few jiffies by skipping the last entry in the
array, and just add it in the end. Saves you a comparison in the loop.

Like:

$array = explode ( ', ', $string );
$arraycount = ( count($array)-1 );
$string = '';
for( $i=0; $i<$arraycount; $i++ ) {
$string .= ((1&$i) ? ( $array[$i] . '; ' ) : ( $array[$i] . ', ' ));
}
$string .= $array[$i];
OK, you had the fastest non-regex function before this one.... and....

this one is slower (0.3638 seconds).
Changing my function to a while loop would probably take yet some more
of the timing, but I haven't really got the time to do so now.


And the while loop version takes 0.3587 seconds. So your 2nd non-regex
version still stands as the fastest non-regex version.
So many ways. The thread should have been called "50 ways to change..."


LOL! :-)

Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Jun 21 '06 #27

P: n/a
Rik
Andy Jeffries wrote:
On Wed, 21 Jun 2006 13:48:00 +0200, Rik wrote:
It is to me, I'd always thought regexs were slower. Bugger, that
means I will /really/ have to get round to learning them.
Yup, I'm quite capable to produce some regexes, but in all testcases
I saw where it could be done without, regexes seemed slower... Time
to test some past code.


I always prefer them as it's generally a lot more readable (a single
line of code compared to 10 lines of iteration). In most cases
developer comprehension is more important than CPU cycles (unless
you're working
with masses of data, but that's not normal in a web system).


It's true that most often it's not the bottleneck. Opening files/databases
etc. are usually terrible more consuming (hey, there is a reason why we had
to loop this code 5000 times to get a reasonable result to compare.
Nevertheless, iot never hurts to make is as efficient as possible.
The obvious comment coming is "regexes are more readable, what a load
of BS" - but they are if you understand regexes, and if you don't -
it's not my fault you are lacking in a skill (with "you" being the
developer coming to look at my code for maintenance).


Regexes can be perfectly commented, which is rarely used unfortunately.
Using /x to newline/indent (and thus make a regex more readable) should also
be used more often. Then, instead of 1 line >400 characters long with some
code you've got a nice regex that you comprehend in a lot less time. No more
searching for the opening or closing parenthesis.

Grtz,
--
Rik Wasmus
Jun 21 '06 #28

This discussion thread is closed

Replies have been disabled for this discussion.