Ascii characters in a loop

M

Mark Hobley

Uri Guttman said:
what part of that do you find unclear?

I think that general navigation of this document is not straightforward, and I
think that where there are fundamental issues that affect basic loops, they
should be mentioned in the loop section, and the document should provide
examples.
you could be using the only hammer you have to work on different nails. i see
that all the time.

Yeah, that is quite possible. I tend to use generic code, that I may reuse in
other projects.

FYI my background is MSDOS assembly language, so I am having to learn the Unix
way of doing everything, which just sometimes seems alien to me.

However, it will be nice when I have learnt everything.

Regards,

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Telephone: (0121) 247 1596
International: 0044 121 247 1596

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/
 
T

Tad McClellan

Mark Hobley said:
Yeah! I just think that 'a to z' looks far nicer late on a Friday night when I
am trying to debug code.


Then you just need to learn to spell "to" as "..":

for $l ( 'a' .. 'z' )
 
U

Uri Guttman

MH> I think that general navigation of this document is not
MH> straightforward, and I think that where there are fundamental
MH> issues that affect basic loops, they should be mentioned in the
MH> loop section, and the document should provide examples.

well, what if you are wrong in your assumptions? magic increment is
independent of loops as it should be. there is nothing special about a
loop that makes your code special. it is the same as if you did a loop
from 1 to 10 but went to 11 because you had a classic off by one
error. this is a fault of the coder, not of the language. i have seen
many off by one errors and worse, coders who don't understand them or
how to fix them. teach that instead and not some well documented 'quirk'
about magic increment.

MH> Yeah, that is quite possible. I tend to use generic code, that I
MH> may reuse in other projects.

hammers aren't generic. you missed my metaphor.

MH> FYI my background is MSDOS assembly language, so I am having to
MH> learn the Unix way of doing everything, which just sometimes seems
MH> alien to me.

and you don't considier winblows alien? MUAHAHAHAHAH!

MH> However, it will be nice when I have learnt everything.

good luck. i know it will take you at least three more weeks. i have
only been learning about computers for 33 years and i know nothing. that
is proven every day.

uri
 
A

anno4000

Peter J. Holzer said:
[...]
It seems strange that if I increment $l (which equals 'z') then I now have a
value that is comparitively less than 'z'.

That's because the "le" operator doesn't define "less" and "greater" in
the same sense as "++" increments. The same happens if you use numbers:

for (my $l = 0; $l le 9; $l++) {
print "$l ";
}

prints

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

Because 90 is the first number which is greater than 9 in a string
comparison.

Only with numbers there is a simple solution (use "<=" instead of "le"),
while there is no builtin comparison operator for "alphanumeric
numbers".

That doesn't mean you can't define an appropriate comparison. In
terms of a sort block (untested):

{ length( $a) <= length( $b) and $a le $b }

Applied back to the OPs loop that results in

for (my $l = 'a'; length( $l) <= 1 and $l le 'z'; $l++) { #...

Anno
 
P

Peter J. Holzer

That doesn't mean you can't define an appropriate comparison.

I didn't mean to imply that. It should always be possible to define a
comparison function for a given sequence. The point was that the
programmer has to define that himself and can't use a builtin.

In terms of a sort block (untested):

{ length( $a) <= length( $b) and $a le $b }

Not quite. It returns false for $a = "b" and $b = "aa".

{
length($a) < length($b) ? 1 :
length($a) == length($b) ? $a le $b :
0;
}

should be correct (except that a sort block should return negative,
zero, positive instead of true, false).
Applied back to the OPs loop that results in

for (my $l = 'a'; length( $l) <= 1 and $l le 'z'; $l++) { #...

Right.

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top