Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion Goto page 1, 2  Next
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Fri May 09, 2008 11:08 am   

looking through a string list
 
Someone posted the syntax before, but how do I look through a stringlist for if comparisons so that I can grab one entry from it.

something lie...

#if (%1=@stringlist[i])
{ }
{ }

i being it going through all values.
Reply with quote
Rorso
Wizard


Joined: 14 Oct 2000
Posts: 1368

PostPosted: Fri May 09, 2008 11:17 am   Re: looking through a string list
 
I thought it was done like:
#if (%1 = %item(@stringlist, %i)) {}
however you must add a loop somewhere to assign numbers to %i. Take a look at the user comments in the help file for %item.
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Fri May 09, 2008 11:18 am   
 
I have to add a loop even if I use the %item function?
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
Rorso
Wizard


Joined: 14 Oct 2000
Posts: 1368

PostPosted: Fri May 09, 2008 11:23 am   
 
chamenas wrote:
I have to add a loop even if I use the %item function?

What item does is to return a value from a specific index, %item(@list, index). E.g using a WHILE-loop you can loop through the entire list that way.

Another approach is to use #FORALL:
#forall @list {#echo %i}

Where %i is assigned an actual item in the list.
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Fri May 09, 2008 11:27 am   
 
So...

Code:

#if (%1 = #loop {%item(@stringlist, %i)}) {}
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
Rorso
Wizard


Joined: 14 Oct 2000
Posts: 1368

PostPosted: Fri May 09, 2008 11:37 am   
 
More like:
#loop @stringlist {
#if (%1 = %i) {#echo found match;#break}
}

This depends alot of what you want to do though, and what kind of comparison. For example to just check if an item you know exactly by name is in the list you could use:
#if (%ismember(%1, @stringlist)) {} {}
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Fri May 09, 2008 11:47 am   
 
Code:

#wait 400
#say Add Attack Type alias...
#oninput {^add_attack (*)} {#noinput
#if (%ismember(%1, @stringlist)) {
#ADDITEM attack_type %1
#say %1 Added to attack type
}
{
#say It's already in the list! } } {General Aliases}


Thanks Very Happy
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Fri May 09, 2008 11:54 am   
 
Code:

#al Show List { #forall @attack_type {#say %i }} {General Aliases|Damage Calculator}


Doesn't want to work, what am I doing wrong?
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
Rorso
Wizard


Joined: 14 Oct 2000
Posts: 1368

PostPosted: Fri May 09, 2008 11:57 am   
 
chamenas wrote:

Doesn't want to work, what am I doing wrong?

You need to remove the space in the aliasname. 'Show List' -> 'showlist'.
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Fri May 09, 2008 11:58 am   
 
ewww, oh well. Thanks. can't do "Show List" ?
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
Caled
Sorcerer


Joined: 21 Oct 2000
Posts: 821
Location: Australia

PostPosted: Fri May 09, 2008 11:59 am   
 
#SAY %replace(@attack_type,"|",%cr)


%expandlist should work too, but for some reason it didn't for me just now.
_________________
Athlon 64 3200+
Win XP Pro x64
Reply with quote
Rorso
Wizard


Joined: 14 Oct 2000
Posts: 1368

PostPosted: Fri May 09, 2008 12:03 pm   
 
chamenas wrote:
ewww, oh well. Thanks. can't do "Show List" ?

"Show list" is not an identifier. Try use a command input trigger instead of alias if you really want to get around it:
#oninput {show list} {#echo ewww}

Edit: Or make an alias #alias{show} and handle 'list' as an argument.
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Fri May 09, 2008 12:06 pm   
 
Caled wrote:
#SAY %replace(@attack_type,"|",%cr)


%expandlist should work too, but for some reason it didn't for me just now.


huh? The show I'm using works fine now :)

@ Ros - Ah, well I just made showlist.

Code:

#oninput {^add_attack (*)} {#noinput
#if (%1=%ismember(%1, @stringlist)) {
#ADDITEM attack_type %1
#say %1 Added to attack type
}
{
#say %1 is already in the list! } } {General Aliases|Damage Calculator}


Automatically defaults to the else part, so something must be wrong in the if statement
Reply with quote
Rorso
Wizard


Joined: 14 Oct 2000
Posts: 1368

PostPosted: Fri May 09, 2008 12:08 pm   
 
chamenas wrote:

Automatically defaults to the else part, so something must be wrong in the if statement

It is a common problem:
#if (pattern goes here) {commandTrue} SingleSpaceHere {commandElse}
make the
}
{
be
} {
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Fri May 09, 2008 12:16 pm   
 
EDIT: Ninjad, but I think the spacing thing is peripheral - it could be causing a problem, but the expression is probably what's killing it.

...there's no need to use an oninput trigger if you don't have any spaces in the command name. Just use an alias if you don't have any.

Something is indeed wrong with your #if statement. Go back and read Rorso's example again - as one of my lecturers used to say: "Read. The f...lipping. Question."

#if (%ismember(%1,@stringlist)) {}

The syntax might also be (@stringlist,%1), I can't remember. Look it up.

%ismember returns the index number of the item - its place in the list. If "haddock" was the third member of the list, %ismember would return 3. This number will very rarely ever be equal to %1. But the #if with %ismember on its own works because the function returns 0 if "haddock"'s not a member. 0 evaluates false, numbers evaluate true.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Fri May 09, 2008 12:19 pm   
 
I had it on its own too and it didn't work. But let me try some things.
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Fri May 09, 2008 12:22 pm   
 
Code:

#al add_attack (*) { #if (%ismember(%1, @stringlist)) { #ADDITEM attack_type %1
#say %1 Added to attack type
} { #say %1 is already in the list! } } {General Aliases|Damage Calculator}


Expression not allowed here error.

So can I use expressions in aliases?
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Fri May 09, 2008 12:24 pm   
 
Quote:
#al add_attack (*) { #if (%ismember(%1, @stringlist)) { #ADDITEM attack_type %1
#say %1 Added to attack type
} { #say %1 is already in the list! } } {General Aliases|Damage Calculator}

Confused
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Rorso
Wizard


Joined: 14 Oct 2000
Posts: 1368

PostPosted: Fri May 09, 2008 12:25 pm   
 
chamenas wrote:

So can I use expressions in aliases?

You can't use patterns in an alias name. An alias puts the arguments automatically in the %1....%n variables so you shouldn't need to capture them with the *. However if it was an #oninput trigger then you would need the pattern.
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Fri May 09, 2008 12:28 pm   
 
PS. If your next question is going to be "it's only capturing one word", use %-1 instead of %1.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Fri May 09, 2008 12:28 pm   
 
blah, alright! Thanks.
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Fri May 09, 2008 12:30 pm   
 
Fang Xianfu wrote:
PS. If your next question is going to be "it's only capturing one word", use %-1 instead of %1.


beat me to it ;)

Edit:

But...

Code:

#al add_attack { #if (%ismember(%-1, @attack_type)) { #ADDITEM attack_type %-1
#say %1- Added to attack type
} { #say %-1 is already in the list! } } {General Aliases|Damage Calculator}


add_attack kick

gives:

kick- Added to attack type

Also, kick is already in the list, so it shouldn't be adding it anyways.
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4779
Location: Pensacola, FL, USA

PostPosted: Fri May 09, 2008 12:51 pm   
 
#say %1- Added to attack type

should be

#say %-1 Added to attack type

and better still to use than %-1 is %params

if you are having trouble passing it to %ismember, try putting it into a localvar and pass that instead
_________________
Discord server
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Fri May 09, 2008 1:13 pm   
 
%params?
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4779
Location: Pensacola, FL, USA

PostPosted: Fri May 09, 2008 1:19 pm   
 
#HELP %params

works for settings other then aliases as well
_________________
Discord server
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net