converting numbers to ascii values [message #183889] |
Sun, 24 November 2013 16:58 |
Mr Oldies
Messages: 241 Registered: October 2013
Karma: 0
|
Senior Member |
|
|
In BASIC I would run a for loop to print out the corresponding characters
for a given value.
for x=65 to 90
print chr$(x)
next x
would give ABCDEF......
how is this done in php?
|
|
|
Re: converting numbers to ascii values [message #183890 is a reply to message #183889] |
Sun, 24 November 2013 17:06 |
Lew Pitcher
Messages: 60 Registered: April 2013
Karma: 0
|
Member |
|
|
On Sunday 24 November 2013 11:58, in comp.lang.php, "richard"
<noreply(at)example(dot)com> wrote:
> In BASIC I would run a for loop to print out the corresponding characters
> for a given value.
>
> for x=65 to 90
> print chr$(x)
> next x
>
> would give ABCDEF......
>
> how is this done in php?
for ($x = 65; $x <=90; ++$x)
printf("%c",$x);
HTH
--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request
|
|
|
|
Re: converting numbers to ascii values [message #183892 is a reply to message #183890] |
Sun, 24 November 2013 17:42 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 24/11/13 17:06, Lew Pitcher wrote:
> On Sunday 24 November 2013 11:58, in comp.lang.php, "richard"
> <noreply(at)example(dot)com> wrote:
>
>> In BASIC I would run a for loop to print out the corresponding characters
>> for a given value.
>>
>> for x=65 to 90
>> print chr$(x)
>> next x
>>
>> would give ABCDEF......
>>
>> how is this done in php?
>
> for ($x = 65; $x <=90; ++$x)
> printf("%c",$x);
>
>
> HTH
>
for ($x = 65; $x <91; $x++)
echo($x);
--
Ineptocracy
(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
|
|
|
Re: converting numbers to ascii values [message #183895 is a reply to message #183891] |
Sun, 24 November 2013 18:15 |
Mr Oldies
Messages: 241 Registered: October 2013
Karma: 0
|
Senior Member |
|
|
On Sun, 24 Nov 2013 18:21:43 +0100, Luuk wrote:
> On 24-11-2013 17:58, richard wrote:
>> In BASIC I would run a for loop to print out the corresponding characters
>> for a given value.
>>
>> for x=65 to 90
>> print chr$(x)
>> next x
>>
>> would give ABCDEF......
>>
>> how is this done in php?
>>
>
> http://us1.php.net/manual/en/function.chr.php
thanks that was what I was looking for.
In my array the key is one letter.
"A"=>array()
"B"=>array()
"C"=>array()
I need to step through each character to get the list.
What is a good way to do that?
BASIC style
x=65
while x<91
a$=chr$(x)
print a$
x=x+1
wend
|
|
|
Re: converting numbers to ascii values [message #183896 is a reply to message #183892] |
Sun, 24 November 2013 18:39 |
Lew Pitcher
Messages: 60 Registered: April 2013
Karma: 0
|
Member |
|
|
On Sunday 24 November 2013 12:42, in comp.lang.php, "The Natural
Philosopher" <tnp(at)invalid(dot)invalid> wrote:
> On 24/11/13 17:06, Lew Pitcher wrote:
>> On Sunday 24 November 2013 11:58, in comp.lang.php, "richard"
>> <noreply(at)example(dot)com> wrote:
>>
>>> In BASIC I would run a for loop to print out the corresponding
>>> characters for a given value.
>>>
>>> for x=65 to 90
>>> print chr$(x)
>>> next x
>>>
>>> would give ABCDEF......
>>>
>>> how is this done in php?
>>
>> for ($x = 65; $x <=90; ++$x)
>> printf("%c",$x);
>>
>>
>> HTH
>>
> for ($x = 65; $x <91; $x++)
> echo($x);
>
Nope. That just gets the numeric values of $x, not the ASCII (or other
characterset) equivalent.
~ $ echo '<?php for ($x = 65; $x < 91; $x++) echo($x); echo PHP_EOL ?>' |
php
6566676869707172737475767778798081828384858687888990
~ $
The alternative I suggested gives
~ $ echo '<?php for ($x = 65; $x <= 90; ++$x) printf("%c",$x);
echo PHP_EOL ?>' | php
ABCDEFGHIJKLMNOPQRSTUVWXYZ
~ $
HTH
--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request
|
|
|
Re: converting numbers to ascii values [message #183897 is a reply to message #183895] |
Sun, 24 November 2013 20:53 |
J.O. Aho
Messages: 194 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 24/11/13 19:15, richard wrote:
> On Sun, 24 Nov 2013 18:21:43 +0100, Luuk wrote:
>
>> On 24-11-2013 17:58, richard wrote:
>>> In BASIC I would run a for loop to print out the corresponding characters
>>> for a given value.
>>>
>>> for x=65 to 90
>>> print chr$(x)
>>> next x
>>>
>>> would give ABCDEF......
>>>
>>> how is this done in php?
>>>
>>
>> http://us1.php.net/manual/en/function.chr.php
>
> thanks that was what I was looking for.
> In my array the key is one letter.
> "A"=>array()
> "B"=>array()
> "C"=>array()
>
> I need to step through each character to get the list.
> What is a good way to do that?
>
> BASIC style
>
> x=65
> while x<91
> a$=chr$(x)
> print a$
> x=x+1
> wend
>
>
for-loop works fine, you can also use foreach-loop if you have a list of
keys stored in a variable.
--
//Aho
|
|
|
Re: converting numbers to ascii values [message #183898 is a reply to message #183890] |
Sun, 24 November 2013 21:46 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma: 0
|
Senior Member |
|
|
Lew Pitcher wrote:
> "richard" wrote:
>> In BASIC I would run a for loop to print out the corresponding characters
>> for a given value.
>>
>> for x=65 to 90
>> print chr$(x)
>> next x
>>
>> would give ABCDEF......
>>
>> how is this done in php?
>
> for ($x = 65; $x <=90; ++$x)
> printf("%c",$x);
There is a probably more efficient alternative to this shell-script/C like
way in PHP:
for ($i = 65; $i <= 90; ++$i)
{
echo chr($i);
}
And finally:
$ php -r 'var_dump(implode("", array_map("chr", array_keys(array_fill(65,
26, true)))));'
string(26) "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
PointedEars
--
> If you get a bunch of authors […] that state the same "best practices"
> in any programming language, then you can bet who is wrong or right...
Not with javascript. Nonsense propagates like wildfire in this field.
-- Richard Cornford, comp.lang.javascript, 2011-11-14
|
|
|
Re: converting numbers to ascii values [message #183899 is a reply to message #183898] |
Sun, 24 November 2013 22:01 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma: 0
|
Senior Member |
|
|
Thomas 'PointedEars' Lahn wrote:
> Lew Pitcher wrote:
>> "richard" wrote:
>>> In BASIC I would run a for loop to print out the corresponding
>>> characters for a given value.
>>>
>>> for x=65 to 90
>>> print chr$(x)
>>> next x
>>>
>>> would give ABCDEF......
>>>
>>> how is this done in php?
>
> […]
> $ php -r 'var_dump(implode("", array_map("chr", array_keys(array_fill(65,
> 26, true)))));'
> string(26) "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
That was a quick hack; this is better:
$ php -r 'var_dump(implode("", array_map("chr", range(65, 90))));'
string(26) "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Note how PHP's range() differs from, e.g., Python's: <http://php.net/range>
PointedEars :)
--
When all you know is jQuery, every problem looks $(olvable).
|
|
|
|
|
|
Re: converting numbers to ascii values [message #183905 is a reply to message #183903] |
Mon, 25 November 2013 03:36 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 11/24/2013 9:50 PM, Doug Miller wrote:
> "J.O. Aho" <user(at)example(dot)net> wrote in news:bff7adFecqpU1(at)mid(dot)individual(dot)net:
>
>>
>> for-loop works fine, you can also use foreach-loop if you have a list of
>> keys stored in a variable.
>>
> You don't understand. RtS isn't asking for someone to tell him how to write it -- he's asking for
> someone to write it for him.
>
Doug,
J.O. understands Richard quite well. But for others reading the
newsgroup, J.O.'s update can be helpful.
And I see Richard has his usual (lack of) intelligent response to you.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
|
Re: converting numbers to ascii values [message #183908 is a reply to message #183905] |
Mon, 25 November 2013 05:47 |
J.O. Aho
Messages: 194 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 25/11/13 04:36, Jerry Stuckle wrote:
> On 11/24/2013 9:50 PM, Doug Miller wrote:
>> "J.O. Aho" <user(at)example(dot)net> wrote in
>> news:bff7adFecqpU1(at)mid(dot)individual(dot)net:
>>
>>>
>>> for-loop works fine, you can also use foreach-loop if you have a list of
>>> keys stored in a variable.
>>>
>> You don't understand. RtS isn't asking for someone to tell him how to
>> write it -- he's asking for
>> someone to write it for him.
>>
>
> Doug,
>
> J.O. understands Richard quite well. But for others reading the
> newsgroup, J.O.'s update can be helpful.
Thanks for replying for me, you are spot on as usually.
> And I see Richard has his usual (lack of) intelligent response to you.
It's his problem.
--
//Aho
|
|
|
|
Re: converting numbers to ascii values [message #183915 is a reply to message #183895] |
Mon, 25 November 2013 12:16 |
Luuk
Messages: 329 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 24-11-2013 19:15, richard wrote:
> On Sun, 24 Nov 2013 18:21:43 +0100, Luuk wrote:
>
>> On 24-11-2013 17:58, richard wrote:
>>> In BASIC I would run a for loop to print out the corresponding characters
>>> for a given value.
>>>
>>> for x=65 to 90
>>> print chr$(x)
>>> next x
>>>
>>> would give ABCDEF......
no, it gives:
A
B
C
D
......
>>>
>>> how is this done in php?
> What is a good way to do that?
>
> BASIC style
>
> x=65
> while x<91
> a$=chr$(x)
> print a$
> x=x+1
> wend
>
>
this also gives:
A
B
C
D
......
What about trying to copy this to PHP:
$x=65;
while ($x<91) {
a$=chr($x);
print $a;
$x=$x+1;
}
RTFM:
http://us2.php.net/while
o, BTW, i left (at least) 1 error in above code,
maybe you can find it ???
;)
|
|
|