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","0 016","0017","01 00","0121","100 0");
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 8 2733
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","0 016","0017","01 00","0121","100 0"); 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
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","0 016","0017","01 00","0121","100 0");
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
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","0 016","0017","01 00","0121","100 0");
$arr = array(15,16,17, 100,121,1000);
addleadingzeros _arr($arr);
function addleadingzeros _arr(&$arr) {
foreach ($arr as &$val) $val=substr("00 0$val",-4); }
Csaba Gabor from Vienna
"TheTeapot" <pe********@gma il.com> kirjoitti
viestissä:11*** *************** ***@g44g2000cwa .googlegroups.c om... 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","0 016","0017","01 00","0121","100 0");
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($arra y, $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,1 21,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.ber keley.edu/>
Kimmo Laine <et************ ****@5P4Mgmail. com>
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","0 016","0017","01 00","0121","100 0");
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)>$max x) $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.
Thanks all, problem solved, used a variation on Ewoud Dronkert's
suggestion.
Thanks for the help,
TheTeapot
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","0 016","0017","01 00","0121","100 0");
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*******@attgl obal.net
=============== ===
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","0 016","0017","01 00","0121","100 0");
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: david |
last post by:
Hi,
I have an application as follows:
MySQL database Back-Eend linked to MS Access Front-End and ASP Web
Application.
I require users to enter Serial Numbers such as:
0105123567 (10 digits), the first 4 being the month and year (mmyy)
|
by: samik_tanik |
last post by:
I need to export a datagrid to Excel.
I could did this. But, also need to keep the leading
zeros in the data.
How can I acheive this?
Any help would be appreciated.
--
Thanking you in anticipation,
Regards,
|
by: mmmgood1 |
last post by:
Help, I'm linking an excel spreadsheet in access and I have datafields
with leading zeros (01021). When the file is linked in access, I get
a #num in the field with the leading zeros. The zeros are needed.
I've formated the field to be a text field, general number, and number
and still I get the #num! error, or the leading zero is dropped. Can
someone help before I pull more of my hair.
Thanks a bunch.
|
by: Clint Stowers |
last post by:
Using A2k
Exporting a query to a CSV file. The problem is any text fields (i.e. 000345)
lose any leading zeros.
Exporting to an excel file this problem does not exist.
Tried to create a SpecificationName via the Export Wizard without success.
Obviously doing something wrong.
|
by: Rich Raffenetti |
last post by:
How can one format an integer into a hex string with leading zeros? Suppose
an integer is 512 which in Hex is 200. I wish to print the 4-byte integer
as 0200 or even 0x0200. The HEX function doesn't create leading zeros. The
Format function (format(value,"X") doesn't create leading zeros.
I believe there should be a simple way that doesn't involving measuring and
padding the string.
| |
by: GarryJones |
last post by:
I have code numbers in 2 fields from a table which correspond to month
and date.
(Month, Code number)
Field name = ml_mna
1
2
3
etc up to 12
(Data is entered without a leading zero)
|
by: Tak |
last post by:
Some days ago, I written a mini program like this:
#include <stdio.h>
int main()
{
char c;
int n;
scanf("%d",&n);
c = getchar();
|
by: JimmyKoolPantz |
last post by:
Task: Customer wants a script of the data that was processed in a
"CSV" file.
Problem: Zip-Code leading zeros are dropped
Basically we have a client that has requested a custom script for each
file that he has us process. He wants this in a Comma Delimited
Format.
|
by: Monty |
last post by:
Hi All,
I am having a problem with leading zeros being stripped from fields in a CSV
file when I bring them in using Jet/OleDB. In VB.Net/VS 2008, I am accessing
a CSV file like so:
sSQL = "SELECT * FROM "
sConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Microsoft.VisualBasic.FileIO.FileSystem.GetParentPath(msFile) &
_
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |