A puzzle about leading zeros | | |
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 | | | | re: A puzzle about leading zeros
On 11 Aug 2005 00:21:52 -0700, TheTeapot wrote:[color=blue]
> $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");[/color] 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 | | | | re: A puzzle about leading zeros
TheTeapot schrieb:[color=blue]
> 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);
>[/color]
You can create every function ;-)
function addleadingzeros_arr($arr) {
}
[color=blue]
> // and have the output look like:[/color]
output?
you're talking of the return value ...
[color=blue]
> // array("0015","0016","0017","0100","0121","1000");[/color]
loop through $arr;
eg. foreach($arr as $value) {
use sprintf
}
[color=blue]
> 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"[/color]
sprintf is what you ar looking for
[color=blue]
> I'd prefer the first one though.[/color]
Hope it help
Jo | | | | re: A puzzle about leading zeros
TheTeapot wrote:[color=blue]
> $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");[/color]
$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 | | | | re: A puzzle about leading zeros
"TheTeapot" <peteraward@gmail.com> kirjoitti
viestissä:1123744912.492061.82180@g44g2000cwa.goog legroups.com...[color=blue]
> 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.
>[/color]
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 <eternal.erectionN0@5P4Mgmail.com> | | | | re: A puzzle about leading zeros
TheTeapot wrote:
[color=blue]
> 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
>[/color]
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. | | | | re: A puzzle about leading zeros
Thanks all, problem solved, used a variation on Ewoud Dronkert's
suggestion.
Thanks for the help,
TheTeapot | | | | re: A puzzle about leading zeros
TheTeapot wrote:[color=blue]
> 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
>[/color]
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. jstucklex@attglobal.net
================== | | | | re: A puzzle about leading zeros
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:[color=blue]
> TheTeapot wrote:
>[color=green]
>> 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
>>[/color]
>
> One thing to think about. "0121" as an integer is equivalent to 81.
> Numbers with leading zeros are octal.
>[/color] |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,327 network members.
|