With/EndWith integrated to ForEach/Next

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

With/EndWith integrated to ForEach/Next

Post by Flype »

A small snippet is better than a long explanation. All is in the title and the snippet :

I'm not sure if it's a good idea - maybe yes maybe no - just discuss about it.

Code: Select all

Structure PRODUCT
  id.s
  name.s
  price.s
  desc.s
EndStructure

Global NewList product.PRODUCT()

ForEach product()
  With product()
    Debug "id = "    + \id
    Debug "name = "  + \name
    Debug "price = " + \price
    Debug "desc = "  + \desc
    Debug ""
  EndWith
Next

ForEach product()
  Debug "id = "    + \id     ; <--------- Syntax Error.
  Debug "name = "  + \name
  Debug "price = " + \price
  Debug "desc = "  + \desc
  Debug ""
Next
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

I once suggested this and people thought it was dreadful. But I think it's a good idea.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

hm... *shrug*

could be useful, but the leading backslash looks strange to me..

think i won't use it. copy/paste of the listname is sufficent ....
oh... and have a nice day.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

There is something similar in CGI - very useful.

Code: Select all

ForEach myArray()
     print $_;
next
$_ is the abbreviation for the currently used array in CGI/Perl.



I would appreciate this request, if there are no conflicts with other commands and if I still can access another linked list in that foreach/next loop.

Peaople like Kaeru Gaman, who don't like this, can still use myList()\id, etc within the foreach/next.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

No way!

Code: Select all

NewList ll.Long()
NewList ll2.Long()

ForEach ll()
  ForEach ll2()
    \l = 4
  Next
Next
Athlon64 3700+, 1024MB Ram, Radeon X1600
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

Hi remi_meyer.

No Way ! Hmmmm, Why ?

Code: Select all

NewList ll.Long()
NewList ll2.Long()

ForEach ll()
  \l = 1        ; same as ll()\l
  ForEach ll2()
    \l = 4      ; same as ll2()\l
  Next
Next

ForEach ll()
  ForEach ll2()
    ll()\l = 4  ; if you need to use ll() in ll2()
  Next
Next

No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

That's just a mess...
Do you see the reason, why With-EndWith is not nestable?
Athlon64 3700+, 1024MB Ram, Radeon X1600
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

Yes, i can easily imagine the mess for the pre-processor and for the pre-pre-processor (Fred/Freak).

I'm just discussing...
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

well... let me add the mess for the readability of your code... ;)
oh... and have a nice day.
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Post by nco2k »

i think its a bad request.

if you want to use it that much, then simply write:

Code: Select all

ForEach List()
  With List()
    \Element = 0
  EndWith
Next
i totally agree with remi.

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post by Hroudtwolf »

;-)

Code: Select all

Macro ForEachWith (_LIST_)
  ForEach _LIST_ : With _LIST_
EndMacro

Macro NextWith 
  EndWith : Next
EndMacro

NewList test ()

AddElement (test ()):test()=1
AddElement (test ()):test()=2
AddElement (test ()):test()=3
AddElement (test ()):test()=4

ForEachWith (test ())
  Debug test ()
NextWith 

SCRJ
User
User
Posts: 93
Joined: Sun Jan 15, 2006 1:36 pm

Post by SCRJ »

No, in version 4.02, you can't use "with" in macros.
Have a look on this topic

http://www.purebasic.fr/english/viewtopic.php?t=25273
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

I agree that a With integrated in the ForEach loop would be nice.
Something like this:

Code: Select all

ForEach With Apples()
  ;bla bla
Next
The compiler should then see that With is used in the ForEach loop and end it at the Next command. I think this is the best way to do it? Fred? :P
I like logic, hence I dislike humans but love computers.
jear
User
User
Posts: 20
Joined: Sun Dec 28, 2003 12:27 am
Location: Ammerland

Post by jear »

What is intended to be the higher abstraction level: ForEach or With?

Code: Select all

Structure Leute
  Name.s
  Vorname.s
  Strasse.s
  PLZ.s
  Ort.s
EndStructure

NewList Leute.Leute()

With Leute()
  ForEach
    \PLZ = "26160"
  Next
EndWith
Post Reply