Why Doesn't This Code Simply Print Letters a to Z

Why doesn't this code simply print letters A to Z?

From the docs:

PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's.

For example, in Perl 'Z'+1 turns into 'AA', while in C 'Z'+1 turns into '[' ( ord('Z') == 90, ord('[') == 91 ).

Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.

From Comments:-

It should also be noted that <= is a lexicographical comparison, so 'z'+1 ≤ 'z'. (Since 'z'+1 = 'aa' ≤ 'z'. But 'za' ≤ 'z' is the first time the comparison is false.) Breaking when $i == 'z' would work, for instance.

Example here.

Why does my code print out text and not the value of the variable?

Though I have no idea why you do this, here's a sample (demo):

$firstName = '_SERVER'; // Note that `$firstName` has no `$`
$secondName = 'REMOTE_ADDR';

echo ${$firstName}[$secondName];

What you need to read is variable variables.

PHP Character Iteration In For Loop Issue

Straight from the PHP Manual:

PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's. For example, in PHP and Perl $a = 'Z'; $a++; turns $a into 'AA', while in C a = 'Z'; a++; turns a into '[' (ASCII value of 'Z' is 90, ASCII value of '[' is 91). Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported. Incrementing/decrementing other character variables has no effect, the original string is unchanged.

The reason it keeps going is because you're attempting a numerical comparison (<= and AA < Z, ZA == Z (when doing <=)) instead of a literal value comparison (==).

In light of this, you could use the following code:

for ($c = "A"; $c != "Z"; $c++)
{
echo $c . ", ";
}

.. or use the actual ordinal value of the characters (which is the better solution in my opinion):

for ($c = ord("A"); $c <= ord("Z"); $c++)
{
echo chr($c) . ", ";
}

Python code for string modification doesn't work properly

This should work for you. You should use % to account for z.

The main point is you don't need to explicitly build a list of positions.

def str_changer(string):

string_list = list(string)
alphabets = 'abcdefghijklmnopqrstuvwxyz'

new_string_list = []

for letter in string_list:
new_string_list.append(alphabets[(alphabets.index(letter)+1) % len(alphabets)])

return ''.join(new_string_list)

lmao = raw_input('Enter somin\'')
print str_changer(lmao)

PHP - Erroneous Alphabet Loop

See the manual for the increment operator:

PHP follows Perl's convention when dealing with arithmetic operations
on character variables and not C's. For example, in PHP and Perl $a =
'Z'; $a++; turns $a into 'AA', while in C a = 'Z'; a++; turns a into
'[' (ASCII value of 'Z' is 90, ASCII value of '[' is 91). Note that
character variables can be incremented but not decremented and even so
only plain ASCII characters (a-z and A-Z) are supported.
Incrementing/decrementing other character variables has no effect, the
original string is unchanged.

Why does this for-loop comparison fail after auto-incrementing on letters

Comparison is alphabetic:

$letter < 'yz' 

When you get to y, you increment again and get z..... alphabetically, z is greater than yz

If you use

    $letter != 'yz' 

for your comparison instead, it will give you up to yy

So

for ($letter = 'a'; $letter !== 'aaa'; ++$letter) {
echo $letter . '<br>';
}

will give from a, through z, aa, ab.... az, ba.... through to zz.

See also this answer and related comments

EDIT

Personally I like incrementing the endpoint, so

$start = 'A';
$end = 'XFD';

$end++;
for ($char = $start; $char !== $end; ++$char) {
echo $char, PHP_EOL;
}

Alphabet range in Python

>>> import string
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> list(string.ascii_lowercase)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

Alternatively, using range:

>>> list(map(chr, range(97, 123)))
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

Or equivalently:

>>> list(map(chr, range(ord('a'), ord('z')+1)))
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

Other helpful string module features:

>>> help(string)
....
DATA
ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
digits = '0123456789'
hexdigits = '0123456789abcdefABCDEF'
octdigits = '01234567'
printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
whitespace = ' \t\n\r\x0b\x0c'


Related Topics



Leave a reply



Submit