PDA

View Full Version : Sagescript progress



JamusPsi
05-23-2009, 03:10 PM
$list = new(list, 1, 1, 2, 3, 4, 1, 2, 5, 19, 2, 1, 1)
$list.remdupes = &removeduplicates
echo Before:
for($a = 0;$a < %list.count; $a = $a + 1) {
echo %list[%a]
}
$list.remdupes()
echo
echo After:
for($a = 0;$a < %list.count; $a = $a + 1) {
echo %list[%a]
}



function removeduplicates() {
for($a = 0;$a < %this.count; $a = $a + 1) {
for($b = $a + 1; $b < %this.count; $b = $b + 1) {
if(%this[%a] == %this[%b]) {
%this.removeat(%b)
$b = $b - 1
continue
}
}

}
}


When run:


>Before:
1
1
2
3
4
1
2
5
19
2
1
1

After:
1
2
3
4
5
19
Script ended. (Ended)

JamusPsi
05-23-2009, 05:56 PM
I need some advice about how to handle regular expressions.

My engine will have matchre, and will also have a =~ operator (as in p-e-r-l) to test a string against a regex pattern.

It was always intended that you will be able to retrieve captured groups from those regex, for instance:



"The quick brown dog" =~ "/The (.*?) brown/"


You should be able to pull "quick" out afterward. There are, unfortunately, many ways of doing this.

The c# way would be to return an object which could be tested for them, as in:



$m = "The quick brown dog" =~ "/The (.*?) brown/"
if($m) {
echo $m[1] is quick
} else {
echo The string did not match.
}


But what about matchre, where there are no variables to hold it?



matchre whisper /^(\w+?) whispers, "(.*?)"$/
matchwait


Now, I can automatically set $m1, $m2, etc. Or I can use a special variable like $lastmatch which automatically takes it. But regular expressions support named captures, such as:



matchre whisper /^(?<name>\w+?) whispers, "?<message>(.*?)"$/
matchwait


Now, if it were done with a variable, you'd say $m[name] and $m[message]. But in a matchre, I'm tempted to simply pull name and message out automatically, and set $name/$message.



matchre whisper /^(?<name>\w+?) whispers, "?<message>(.*?)"$/
matchwait

whisper:
echo %name whispered %message to me at gettime()
-or-
echo %m1 whispered %m2 to me at gettime()
-or-
echo %lastmatch[name] whispered %lastmatch[message] to me at gettime()
-or-
echo %lastmatch[1] whispered %lastmatch[2] to me at gettime()


The direction I'm leaning towards this is to do ALL of the above, giving script authors the most flexibility in how they use regular expressions. The real question is, just how many local variables should I be setting when this happens? I don't want to clutter your scope. Just named matches? just numbered ones? Just a single lastmatch variable? All of the above?

Looking for opinions on this.

Jayvn
05-23-2009, 05:57 PM
WITCH!

Joseph
05-23-2009, 09:39 PM
I believe that regular expression already consists of everything you are asking about.. unless I misunderstand the question.. for the "?"s in the reg ex that are identifiers you have "$"s for the return.. example $` should be the begenning of the match $' the end (unless i got those backwards) $0 should be the entirety of match $1 first "?" $2 2nd etc.. if you would like I could point you towards a tutorial to explain what I am talking about.. if that does not solve your problem then I think I misunderstood the questions.

Drunken Durfin
05-23-2009, 09:46 PM
I think I am going to learn Mandarin Chinese before I tackle Sagescript.

Jayvn
05-23-2009, 10:11 PM
http://img.4chan.org/b/src/1243130809560.jpg

Gibreficul
05-25-2009, 03:56 PM
I think I am going to learn Mandarin Chinese before I tackle Sagescript.

You learn that Chineese. I bet I end up with a mansion on Mars before Sagescript is even worth learning, let alone POSSIBLE to learn.

Gibreficul

JamusPsi
05-25-2009, 05:43 PM
Oh, don't be so hard on yourself Gib; I've made it easy enough that the simplest of users can probably benefit.

Bhuryn
05-25-2009, 08:48 PM
$list = new(list, 1, 1, 2, 3, 4, 1, 2, 5, 19, 2, 1, 1)
$list.remdupes = &removeduplicates
echo Before:
for($a = 0;$a < %list.count; $a = $a + 1) {
echo %list[%a]
}
$list.remdupes()
echo
echo After:
for($a = 0;$a < %list.count; $a = $a + 1) {
echo %list[%a]
}



function removeduplicates() {
for($a = 0;$a < %this.count; $a = $a + 1) {
for($b = $a + 1; $b < %this.count; $b = $b + 1) {
if(%this[%a] == %this[%b]) {
%this.removeat(%b)
$b = $b - 1
continue
}
}
}
}




I assume this could be also be performed like this?:



$list = $removeduplicates($list)
function removeduplicates(list2) {
for($a = 0;$a < $list2.count; $a = $a + 1) {
for($b = $a + 1; $b < $list2.count; $b = $b + 1) {
if($list2[%a] == $list2[%b]) {
$list2.removeat(%b)
$b = $b - 1
continue
}
}

}
(return list2?)

JamusPsi
05-26-2009, 02:36 AM
I assume this could be also be performed like this?:



$list = $removeduplicates($list)
function removeduplicates(list2) {



Yes, but:

There's no need for the $ before your call it's a bare function call rather than a call to a variable holding a function.

ex:


function foo() {
}

Call with:
foo()

OR
$func = &foo
$func()


Also, because you passed $list as a variable reference, and modified it in place, you do not need to perform the assignment. This would work just as well:



removeduplicates($list)


If you wanted to be really snazzy, you could even make a function which takes a parameter OR uses this (if it's been put into an object)



function removeduplicates(passed) {
if($this)
$list = $this
else
$list = $passed

Or, simpler
$list = $this || $passed

//modify list...
}

Sean of the Thread
05-26-2009, 02:42 AM
10 goto 20

sorry that's all I got.

my VB and SQL are rusty as hell let alone C+ or #

JamusPsi
05-26-2009, 03:17 AM
I'm showing the complex, powerful stuff here. The basics look a lot more like wizard/sf scripts than anything else.

Donquix
05-26-2009, 03:19 AM
I'm showing the complex, powerful stuff here. The basics look a lot more like wizard/sf scripts than anything else.

http://www.feebleminds-gifs.com/exploding-head.gif