in reply to Re: Foreach Loop Optimization
in thread Foreach Loop Optimization

Thanks for your code suggestions!

The code is not calling time in the loop, only formatting time value from the database.

Unfortunately, I have to use /approv/i because it has to match approv in the string when the string could be anything.

Is $localtime = $moduleField[2] ? FormatTime($moduleField[2]) : "n/a"; more efficient than my two lines of code?

I don't think changing $_ is dangerous. The perdoc does it a lot.
For some reason, I was thinking =~ would change the value of $headers[$dataCount].
Thanks for that code, I bet its faster than changing the value of $_.

Replies are listed 'Best First'.
Re^3: Foreach Loop Optimization
by ikegami (Patriarch) on Aug 01, 2007 at 16:27 UTC

    I don't think changing $_ is dangerous. The perdoc does it a lot.

    Sometimes it won't cause any problems. Sometime it creates hard to debug action-at-a-distance problems because it's common for code to rely on $_ being left unchanged by the functions it calls.

    If you modify $_, localize it. Of course, local $_ = $val; is buggy so local *_ = \$val; or for ($val) should be used instead. Or better yet, just don't assign to $_.

    In this particular case, the only thing the assignment to $_ does is obfuscate, so it's a bad idea regardless of all the problems and complexity I just mentioned.

    For some reason, I was thinking =~ would change the value of $headers[$dataCount].

    All =~ does is specify the operand for the following (m//, s///, tr///, etc) operator.