Author Topic: Letoscript: Breaking out of a loop?  (Read 406 times)

Legacy_MrZork

  • Hero Member
  • *****
  • Posts: 1643
  • Karma: +0/-0
Letoscript: Breaking out of a loop?
« on: October 03, 2012, 11:28:03 pm »


               I don't know how many letoscripters are checking the forum these days, but I had a quick letoscript question and I didn't want to clutter up FunkySwerve's letoscript sticky.

Does letoscript implement perl's
next
and
last
, or any analog to NWScript's
continue
and
break
statements?

In other words, if I have something like

%mod = 'C:\\Program Files (x86)\\games\\NeverwinterNights\\NWN\\modules\\MyMod.mod';

# creature skin/hide item
$BASE_ITEM_CREATUREITEM = 73;

# hunt through items
$founditem = False;
$numchecked = 0;    # for debugging
for (%mod['*.uti']) {
  $numchecked++;
  ($numchecked>5) and last;
      $BaseItemType = /~/BaseItem;
      if ($BaseItemType == $BASE_ITEM_CREATUREITEM) {
            print "/~/LocalizedName is a creature skin\\n";
            }
      }

In that code, the
($numchecked>$MAXCHECK) and last;
line should be checking the count and exiting the loop after the first five items are done. But I am doing something wrong, because I am getting no errors, but the loop runs through every (custom) item in the mod.

(I should be clear that I know there are other ways to do just a certain number of iterations of the above loop. I want to know if letoscript has a way to break out of a loop, as there is in most languages.)
               
               

               


                     Modifié par MrZork, 03 octobre 2012 - 10:30 .
                     
                  


            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
Letoscript: Breaking out of a loop?
« Reply #1 on: October 05, 2012, 01:45:09 pm »


               I assume you've tried "break"?

i.e. if ($numchecked>5) break;
               
               

               
            

Legacy_MrZork

  • Hero Member
  • *****
  • Posts: 1643
  • Karma: +0/-0
Letoscript: Breaking out of a loop?
« Reply #2 on: October 05, 2012, 05:06:46 pm »


               Yes, I actually tried
break
first, as that's the loop exit keyword for NWScript and Python and I had forgotten that the Perl equivalent was
last
.

BTW, here's the test I was using

# looptest.ls
# letoscript loop test to see what keywords will allow exiting from or skipping through a for loop.
@somelist = ('one','two','three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten');
$count = 0;
$MAXCOUNT = 5;
for (@somelist) {
    $count++;
    if ($count>$MAXCOUNT) {
        break;
        }
    print "$_\\n";
    }
And running that yields

one
two
three
four
five
six
seven
eight
nine
ten
BTW, I also tried the somewhat more compact
($count>$MAXCOUNT) and break;
construction, also with no luck.
               
               

               


                     Modifié par MrZork, 05 octobre 2012 - 04:10 .
                     
                  


            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
Letoscript: Breaking out of a loop?
« Reply #3 on: October 05, 2012, 06:13:22 pm »


               I figured you probably had, but you never know... '<img'>
               
               

               
            

Legacy_acomputerdood

  • Sr. Member
  • ****
  • Posts: 378
  • Karma: +0/-0
Letoscript: Breaking out of a loop?
« Reply #4 on: October 09, 2012, 06:25:48 pm »


               i've spent quite some time trying to find documentation on this mystery language myself, but to no avail.  the best you could probably do is to cheat:


# looptest.ls
# letoscript loop test to see what keywords will allow exiting from or skipping through a for loop.
@somelist = ('one','two','three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten');
$count = 0;
$MAXCOUNT = 5;
for (@somelist) {
   $count++;
   if ($count>$MAXCOUNT) {
   }else{
        print "$_\\n";
   }  
}

               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Letoscript: Breaking out of a loop?
« Reply #5 on: October 09, 2012, 07:36:02 pm »


               

acomputerdood wrote...

i've spent quite some time trying to find documentation on this mystery language myself, but to no avail. the best you could probably do is to cheat:


Well, If it is a Perl sentax, which it is looking like.  You cold go with:

http://www.irt.uni-h.../pub/report.pdf
or
http://www.perl.org/
               
               

               


                     Modifié par Lightfoot8, 09 octobre 2012 - 06:41 .
                     
                  


            

Legacy_acomputerdood

  • Sr. Member
  • ****
  • Posts: 378
  • Karma: +0/-0
Letoscript: Breaking out of a loop?
« Reply #6 on: October 09, 2012, 08:00:37 pm »


               but "last" doesn't work to break out of a loop like it does in perl.

i agree that it certainly seems modeled after perl, but there are some differences that i can't find documented anywhere.  the best i've found is this (http://www.weatherso...LetoScript.html):

LetoScript has gone through a lot of changes. Early on, it had a syntax that, while very straightforward, looked very strange, especially to non-programmers. This version of the syntax was named Phoenix, and you can still find examples of it on the forums and in use. It's readily identifiable by its use of brackets. It looks something like this:

...

The language was re-written completely from scratch for a new syntax that looks more like other scripting languages and is much more powerful - but not quite as easy to learn. This version of the syntax is named Unicorn. You'll recognize it by the semicolons at the end of most lines.


so no, it's definitely not actual perl.
               
               

               


                     Modifié par acomputerdood, 09 octobre 2012 - 07:03 .
                     
                  


            

Legacy_MrZork

  • Hero Member
  • *****
  • Posts: 1643
  • Karma: +0/-0
Letoscript: Breaking out of a loop?
« Reply #7 on: October 11, 2012, 06:12:40 am »


               I've had to do perl coding in the past and, in most regards, Letoscript is very perl-like. The way lists and hashes are denoted, the chained conditional execution, the way subroutines are defined, etc. But, for whatever reason,
last
and
next
are not the Letoscript analogs to NWScript's
break
and
continue
. There may be something odd in play here, since using those keywords also does not cause Letoscript to give an error... It's possible there was a plan to implement them that never was completed.

acomputerdood, I can figure out how to conditionally skip iterations in the loop. My goal was to save compute cycles by actually ending the loop early when possible. For example, if I want to grab only the first instance of an object in the module that meets certain criterion and then do something with it, it will save a lot of time if the code doesn't cycle through all possible objects.

For present purposes, I am pretty sure Letoscript will do what I want by using a
while
loop and using
pop
to get each loop iterable, then setting a variable that fails the
while
test when the criterion is met. It's not as nice a method as using
break
, but it will do. Unfortunately, implementing
continue
that way is somewhat messy, as I think I would need a cascaded set of
if
blocks in the loop. :-(