for Loop on characters.

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
bembulak
Enthusiast
Enthusiast
Posts: 575
Joined: Mon Mar 06, 2006 3:53 pm
Location: Austria

for Loop on characters.

Post by bembulak »

Sometimes I think something like this would come very handy:

Code: Select all

For i.s = "a" To "z"
    ; do anything
Next

For j.s = "A" To "Z"
    ; do something other
Next
This would enhance some string operations, I think. Seen it sometimes in other languages (ObjectPascal and Python, eg) and it's neat.
cheers,

bembulak
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

I don't understand what you're doing.
Do you want something like that:

Code: Select all

Structure chars
  c.c[0]
EndStructure

s.s = "hallo"
len.l = Len(s)
*p.chars = @s
For z = 0 To len - 1
  Debug Chr(*p\c[z])
Next
:?:
Your syntax proposal isn't clear to me..

Or do you want something like this:

Code: Select all

For z = 'a' To 'z'
  Debug Chr(z)
Next
Athlon64 3700+, 1024MB Ram, Radeon X1600
User avatar
bembulak
Enthusiast
Enthusiast
Posts: 575
Joined: Mon Mar 06, 2006 3:53 pm
Location: Austria

Post by bembulak »

Or do you want something like this:
Code:
For z = 'a' To 'z'
Debug Chr(z)
Next
Yeah, that one.
Just the way ObjectPascal and Python do it.
cheers,

bembulak
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

bembulak wrote:
Or do you want something like this:
Code:

Code: Select all

For z = 'a' To 'z'
  Debug Chr(z)
Next
Yeah, that one.
Just the way ObjectPascal and Python do it.
Did you try to run that code?
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Trond wrote:Did you try to run that code?
Okay, you got me curious. It looks okay and it works here. Produces "a" through to "z".

What have you spotted?
Dare2 cut down to size
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Just that he requests something that is implemented, and probably didn't notice.
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

just like christmas...
Good programmers don't comment their code. It was hard to write, should be hard to read.
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

traumatic wrote:just like christmas...
:D
Trond wrote:Just that he requests something that is implemented, and probably didn't notice.
Ah, I see. I just thought maybe you had unearthed something unexpected. :)

Mind you the requested looping option was all strings: For i.s = "A" to "Z" so I guess there was a difference.


I'm not sweating on whether this one is implemented. :)
Dare2 cut down to size
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

The topic title said characters.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

I didn't know you could do that! :D

In the past I've done something like this:

Code: Select all

#LCase=abcdefghijklmnopqrstuvwxyz
#UCase=ABCDEFGHIJKLMNOPQRSTUVWXYZ

For i=1 To Len(#LCase)
	If somevar.s=Mid(#LCase,i,1)
		; do something
		Break
	EndIf
Next i
User avatar
bembulak
Enthusiast
Enthusiast
Posts: 575
Joined: Mon Mar 06, 2006 3:53 pm
Location: Austria

Post by bembulak »

I didn't know you could do that!
I didn't too!

I really did not now, that

Code: Select all

For z = 'a' To 'z'
will work, since you're passing 'a', .. on to the variable z, which is an integer value.
The code I tried to do, was for i.s = 'a'...
What a bad mistake.

Shame on me...
:oops:
cheers,

bembulak
Post Reply