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

A puzzle about leading zeros

P: n/a
Hi all,

Here's a puzzle:
// Say I have an array of numbers set up like so:
$arr = array(15,16,17,100,121,1000);

// How can I create a function so that I can use it like so:
addleadingzeros_arr($arr);

// and have the output look like:
// array("0015","0016","0017","0100","0121","1000");

Or, if the function only does one value like shown, so I can loop the
array through, modifying each seperately.
addleadingzeros_int(32);
// Outputs: "0032"

I'd prefer the first one though.

Thanks,
TheTeapot

Aug 11 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 11 Aug 2005 00:21:52 -0700, TheTeapot wrote:
$arr = array(15,16,17,100,121,1000);

// How can I create a function so that I can use it like so:
addleadingzeros_arr($arr);

// and have the output look like:
// array("0015","0016","0017","0100","0121","1000");


http://php.net/sprintf
http://php.net/foreach

function f( $a )
{
$b = array();
foreach ( $a as $v )
$b[] = sprintf( '%04d', $v );
return $b;
}

Or using http://php.net/array-map

function f( $i )
{
return sprintf( '%04d', $i );
}
$b = array_map( 'f', $a );

Or your own implementation of sprintf using http://php.net/substr

$padded = substr( '000'.$int, -4 );

--
E. Dronkert
Aug 11 '05 #2

P: n/a
TheTeapot schrieb:
Hi all,

Here's a puzzle:
// Say I have an array of numbers set up like so:
$arr = array(15,16,17,100,121,1000);

// How can I create a function so that I can use it like so:
addleadingzeros_arr($arr);
You can create every function ;-)

function addleadingzeros_arr($arr) {

}
// and have the output look like: output?

you're talking of the return value ...
// array("0015","0016","0017","0100","0121","1000");
loop through $arr;

eg. foreach($arr as $value) {
use sprintf
}
Or, if the function only does one value like shown, so I can loop the
array through, modifying each seperately.
addleadingzeros_int(32);
// Outputs: "0032" sprintf is what you ar looking for

I'd prefer the first one though.

Hope it help
Jo
Aug 11 '05 #3

P: n/a
TheTeapot wrote:
$arr = array(15,16,17,100,121,1000);

// How can I create a function so that I can use it like so:
addleadingzeros_arr($arr);

// and have the output look like:
// array("0015","0016","0017","0100","0121","1000");


$arr = array(15,16,17,100,121,1000);
addleadingzeros_arr($arr);
function addleadingzeros_arr(&$arr) {
foreach ($arr as &$val) $val=substr("000$val",-4); }
Csaba Gabor from Vienna

Aug 11 '05 #4

P: n/a
"TheTeapot" <pe********@gmail.com> kirjoitti
viestissä:11*********************@g44g2000cwa.goog legroups.com...
Hi all,

Here's a puzzle:
// Say I have an array of numbers set up like so:
$arr = array(15,16,17,100,121,1000);

// How can I create a function so that I can use it like so:
addleadingzeros_arr($arr);

// and have the output look like:
// array("0015","0016","0017","0100","0121","1000");

Or, if the function only does one value like shown, so I can loop the
array through, modifying each seperately.
addleadingzeros_int(32);
// Outputs: "0032"

I'd prefer the first one though.


Some other examples were already mentioned, but I'll throw in just one more,
str_pad(). It padds the given string to given length and fills the empty
space with given string. A function using str_pad would be:

zero_fill($array, $length=4, $fill='0') {
$zf_arr = is_array($array) ? $array : array($array);
foreach($zf_arr as $key => $val)
$$zf_ret[$key] = str_pad($val, $length, $fill, STR_PAD_LEFT);
return is_array($array) ? $zf_ret : $zf_ret[0];
}

var_dump( zero_fill(32) ); // It can convert both single parameters...
var_dump( zero_fill(array(15,16,17,100,121,1000)) ); // or entire arrays.
var_dump( zero_fill(15023, 8) ); // you can also modify the length of the
output.
var_dump( zero_fill(23, 4, '-') ); // or the fill character.

I could not test it as I have no access to my usual testing server at the
moment, so I can't say if it works or not, but that's basicly something you
could use.

--
SETI @ Home - Donate your cpu's idle time to science.
Further reading at <http://setiweb.ssl.berkeley.edu/>
Kimmo Laine <et****************@5P4Mgmail.com>
Aug 11 '05 #5

P: n/a
TheTeapot wrote:
Hi all,

Here's a puzzle:
// Say I have an array of numbers set up like so:
$arr = array(15,16,17,100,121,1000);

// How can I create a function so that I can use it like so:
addleadingzeros_arr($arr);

// and have the output look like:
// array("0015","0016","0017","0100","0121","1000");

Or, if the function only does one value like shown, so I can loop the
array through, modifying each seperately.
addleadingzeros_int(32);
// Outputs: "0032"

I'd prefer the first one though.

Thanks,
TheTeapot

function addleadingzeros_arr($arr) {
$res = array();
foreach($arr as $x)
if(abs($x)>$maxx) $maxx = abs($x);
mult = 1;
len = 0;
while(mult<maxx) {
mult *= 10;
len += 1;
}
foreach($arr as $k => $x)
$res[$k] = $x<0?"-":"" . right(mult+abs(x),len);

return $res;
}

Will do the trick without all the overhead of sprintf. If you know how
many leading zeroes you want, the first two loops can be omitted and the
correct values (10000 and 4 for instance, in your specified case). The
conditional and the abs can be omitted if you'll always have non
negative numbers in $arr. The code can be simplified if $arr is
subscripted by 0-n.

Untested, off the top of my head. Try before committing real data. YMMV.
Aug 12 '05 #6

P: n/a
Thanks all, problem solved, used a variation on Ewoud Dronkert's
suggestion.

Thanks for the help,
TheTeapot

Aug 12 '05 #7

P: n/a
TheTeapot wrote:
Hi all,

Here's a puzzle:
// Say I have an array of numbers set up like so:
$arr = array(15,16,17,100,121,1000);

// How can I create a function so that I can use it like so:
addleadingzeros_arr($arr);

// and have the output look like:
// array("0015","0016","0017","0100","0121","1000");

Or, if the function only does one value like shown, so I can loop the
array through, modifying each seperately.
addleadingzeros_int(32);
// Outputs: "0032"

I'd prefer the first one though.

Thanks,
TheTeapot


One thing to think about. "0121" as an integer is equivalent to 81. Numbers
with leading zeros are octal.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 22 '05 #8

P: n/a
Not if you just append two zeros in "string" form to them.

So what you need to do in your function is:

1) Count how many characters the number is.
2) Depending on the above append a certain number of zeros to the front:

if($digitCount == 2)
"00" . "23"

3) Put that into a for each loop.

foreach($arr as $value)
{
if(strlen(trim($value)) == 2)
"00" . "23";
}

Or even better use a case statement inside the for each.

Hope that helps,

D

Jerry Stuckle wrote:
TheTeapot wrote:
Hi all,

Here's a puzzle:
// Say I have an array of numbers set up like so:
$arr = array(15,16,17,100,121,1000);

// How can I create a function so that I can use it like so:
addleadingzeros_arr($arr);

// and have the output look like:
// array("0015","0016","0017","0100","0121","1000");

Or, if the function only does one value like shown, so I can loop the
array through, modifying each seperately.
addleadingzeros_int(32);
// Outputs: "0032"

I'd prefer the first one though.

Thanks, TheTeapot


One thing to think about. "0121" as an integer is equivalent to 81.
Numbers with leading zeros are octal.

Aug 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.