0
Votes
Undo
  1. Tomas Beran
  2. PowerBuilder
  3. Thursday, 15 December 2022 12:16 PM UTC

Hi

It's surprising to me there hasn't been such discussion in the past.

I'm looking for 'foreach' alternative in Powerscript. I mean: In my app I use multirow selection everywhere it's possible. Therefore iterating through for-next loop is one of the most common action I usually do. Wouldn't be nice to have a special command just for making code smaller and safer?

The idea of foreach is taken from Lua language which I see very useful:

string ls_array[]

string ls_var

...

foreach ls_var in ls_array

//do something with ls_var

next

 

Second idea - taken from C++ - just enhancement of for-next loop:

string ls_array[]

...

long ll_n

for ll_n in ls_array

//do something with ls_array[ll_n]

next

 

What's your opinion?

 

Markus Eckert Accepted Answer Pending Moderation
  1. Thursday, 15 December 2022 16:38 PM UTC
  2. PowerBuilder
  3. # 1

I can understand where you're coming from, but for me, foreach wouldn't be that far up the syntactic sugar wishlist, since it's not that much shorter than a regular FOR loop and you also have complications when you want to edit the thing you're iterating over.

 

If I had one PowerScript wish, it would be String Interpolation:

Being able to write

ls_find = $"long_column = {ll_long} and date_column = Date({ld_date}) and string_column = '{ls_string}'"

instead of

ls_find = "long_column = "+ String( ll_long ) + " and date_column = Date( " + String( ld_date ) + " ) and string_column = '" + ls_string + "'"

would save me juggling apostrophes and quotation marks and plus signs.

Comment
There are no comments made yet.
Andreas Mykonios Accepted Answer Pending Moderation
  1. Thursday, 15 December 2022 14:49 PM UTC
  2. PowerBuilder
  3. # 2

Hi.

Some first thoughts about that.

Powerbuilder is very different than most moderns languages in many aspects. For example c# does support:

string[] values = {"VALUE 1", "VALUE 2", "VALUE 3"};
foreach (string v in values) 
{
  Console.WriteLine(v);
}

What does the previous loop? It will let you work with each item in you array. Variable v exists only in foreach scope (this isn't supported in powerbuilder).  I'm not sure but I believe that lua also has this scope thing.

Foreach loop reminds me how cursor works. In a simpliefied way. It may be very useful when you know that you have to pass through the objects sequentially. I believe that for each loops are very useful when working with collections, but powerbuilder doesn't have collections.

I don't believe that there are scenarios you cannot handle because of for each loop lack. Also, those kind of enhancements would be useful only if accompanied by powerscript improvements.

Your second example is a simplified syntax of for loop as I understand.

Andreas.

Comment
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Thursday, 15 December 2022 13:58 PM UTC
  2. PowerBuilder
  3. # 3

I think it my be useful. Your second example is confusing though:

long ll_n

for ll_n in ls_array

//do something with ls_array[ll_n]

next

 

Should that not be "ll_array" instead of "ls_array"? I'm not too familiar with LUA, so maybe this is something I'm not able to fully understand.

Please request this as an "enhancement request" on the bug site: https://www.appeon.com/standardsupport/newbug

 

regards.

Comment
  1. Tomas Beran
  2. Thursday, 15 December 2022 14:17 PM UTC
Hi Miguel

No because you're iterating through ls_array. You wand all available indexes from ls_array.



The idea behind is:

You need two access modes:

1. Simple (foreach) which gets you a value of the item and fits to most usual scenarios

2. General (for) which allows you access to an array element.

Both without working with pointers, C++ iterators etc.

Another alternative would be to add lambdas but I see this option too complex for Powerscript.
  1. Helpful
  1. Miguel Leeuwe
  2. Thursday, 15 December 2022 14:19 PM UTC
Hi Tomas,

Never too old to learn :)

Thanks for the explanation.
  1. Helpful
  1. Roland Smith
  2. Thursday, 15 December 2022 18:24 PM UTC
VBScript has this as well. It is usually used for collections which is basically an object that contains an array of objects. The advantage is that you don't need to determine how many entries are in the array and therefore don't need to add [index] to the end. Many collections will have a count property which allows you to do a for/next.
  1. Helpful 1
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.