2018-11-28 07:38:59

This issue has pissed me off for many years now. I have a line of code that I don't want very long. To shorten it, I want to be able to write the string, then the starting and ending number. For example, house 1 through 5. Instead of writing if(currentloc=="house1" or currentloc=="house2" or currentloc=="house3", ect, I want to be able to write if (currentloc=="house"1,5 to shorten it. I've already figured out making a string variable and adding it to the end, if (currentloc=="house"+string, helps, but what do I write in that string variable to make it recognize numbers 1 through 5? Thanks

Thumbs up

2018-11-28 07:52:42

sorry for posting twice. I just realized this may not be possible since you have to state whether the condition is and, or or. But maybe there is a way to shorten it even if the method I was thinking of doesn't work. Let me know if you have solved this issue please.

Thumbs up

2018-11-28 07:55:54

Maybe you can make an array and you can loop through the items. If array.find or something. You never said what language you were using so...

Ivan

2018-11-28 10:40:31

It depends on how your structuring your data, using Classes with dictionaries or multi-dimensional lists can be a good way to keep track of location data or doing checks and iteration. For example you could put a house class in a dictionary and do a quick comparison like so:

class house(object):
    def update(self):
        print('house things')

block = {'house1':house(),'house2':house(),'house3':house(),'house4':house(),'house5':house()}

block[currentloc].update()

Or with lists with currentloc being an integer:

class house(object):
    def update(self):
        print('house things')

block = [house(),house(),house(),house(),house()]

block[currentloc].update()
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

Thumbs up

2018-11-28 11:09:55 (edited by CAE_Jones 2018-11-28 11:13:24)

If you want the example you gave, to shorten if (cl == "house1" or cl == "house2") etc, you might try
if (string_left(cl, 5) == "house" and string_to_number(string_trim_left(cl, 5)) <= 5)
IDR if python slices work with strings, but if so, it'd be something like if (cl[0:5] == "house" and int(cl[5:-1]) <= 5)
Hopefully I didn't completely misunderstand what you were asking.
[edit] Actually, if you can use regular expressions, then something like "house[1-5]" would do exactly what it sounds like you're asking for. IIRC, BGT does not support regular expressions, while Java and Python can (I do not remember how). [/edit]

Some of my games
Keep up to date by following @Jeqofire on twitter!
Ear Ninja?

Thumbs up

2018-11-28 17:20:34

I should have mentioned what language I am using, sorry for being so dumb. I am a bgt user. And @ post 5, yes, you interpreted my question correctly. BGT needs to be updated to support expressiions such as that one.

Thumbs up

2018-11-28 18:43:31

Someone could probably hack together functions for working with regular expressions, if they haven't already. There'd need to be equivalents for equals, contains, replace, and split, and that would probably cover it.

Some of my games
Keep up to date by following @Jeqofire on twitter!
Ear Ninja?

Thumbs up

2018-11-28 20:21:12

If you were using regular expressions (and if the function was just called "match") you could just do:
if (match(currentloc, "^house[0-9]$")) {
//...
}
Believe me, your not going to get regular expressions in BGT any time soon. Unless you enjoy hacking together C++ DLLs, that is. Or getting PCRE/PCRE2 to work. Another reason to not use BGT...

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.

Thumbs up

2018-11-28 22:08:34

... Why? Why can't I just look up the most common rules and write an implementation like it's that expr assignment from CS102? I mean, aside from the fact that I'm not sure if I finished that assignment or not...?

Some of my games
Keep up to date by following @Jeqofire on twitter!
Ear Ninja?

Thumbs up

2018-11-28 22:09:53

@9, uh... because regular expressions are a lot more powerful than that? big_smile

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.

Thumbs up

2018-11-28 23:03:37

Still not seeing the problem? Fundamentally, it's a set of rules for parsing strings. Implementing those rules might not be as easy as fizzbuzz, but I'm not sure what would make it so much harder as to require DLLs or built-in support. We're talking parsing rules, not hardware drivers or DSP.

Some of my games
Keep up to date by following @Jeqofire on twitter!
Ear Ninja?

Thumbs up

2018-11-29 01:55:50 (edited by Ethin 2018-11-29 01:57:58)

@11, yes. But we're also talking about implementing backtracking, grouping, character matching, ranges, line delineation, escaping, character classes, subpatterns (named and unnamed), repetition, atomic grouping and possessive quantifiers, backreferences, assertions, conditional subpatterns, conditional matches ([0-9]|[a-zA-Z]), comments, recursive patterns... I could go on. And that's just some of the features that people would expect, and probably want, n a regular expression engine, and libraries like PCRE and PCRE2 already provide all of that. (It also provides subpatterns as subroutines, callouts, and backcontrol, and one other thing too.) There's no point whatsoever in writing your own regular expression parser in a language that doesn't provide one when there are libraris that are widely known (and that people will know how to use) rather than inventing your own syntax and attempting to implement all of that when people start requesting it, which they will do, very quickly. The quantifiers, especially, will be particularly important, unless you want to match [0-9]+ between five and a hundred times and enjoy writing that same range expression well over 200 times and making expressions that take five minutes just to read? Believe me, I know how painful that is, because MOO doesn't have quantifiers. One of my regular expressions, which is quite short in engines like PCRE, is over a thousand characters in MOO's regular expression engine.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.

Thumbs up

2018-11-29 03:15:27

there are a few things that bgt can't do that I've run into. Number 1, when telling it to write text into a document, you can't tell bgt to insert a new line to start a new line. Number 2, you cant layer on quotes, so if you wanted to build a string containing quotes, you couldn't. And 3, it doesn't have this 1-5 thing which really upsets me. Here's an update  though. I made my string say this: space or space currentloc==. Only problem: when I write that varible in my if statement and then just add the locations in quotes immediately before and after the string variable, bgt doesn't recognize the variable as the normal text it is suppose to. I coppied the text inside the string and pasted it between the location and it worked fine, but when I attempted to replace that same exact text with the string variable bgt didn't compile the same result. Why? I will try taking out the = signs and see if that helps but I don't see why it would

Thumbs up

2018-11-29 03:33:27

@13, yeah, BGT doesn't have raw string literals, also known as here documents. Quite sad, that; here documents are incredibly useful.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.

Thumbs up

2018-11-29 12:30:38

@13: It sounds like you haven't read about escape sequences. \n (new line), \r (carriage return, aka newline for windows because windows is weird like that), \" (a quote in a string literal), \', \\, \t (tab), etc.
I don't think I understand what you were trying to do that wouldn't work?

Some of my games
Keep up to date by following @Jeqofire on twitter!
Ear Ninja?

Thumbs up

2018-11-30 00:17:32

MacOS is just as weird -- it uses \r as its new line. Which is just odd....

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.

Thumbs up

2018-11-30 01:10:58

At post 13, read the language tutorial and check the examples.
To insert a new line into a string, just write \r\n. So, for example:
string multiline_string="This string\r\nspans\r\nmultiple lines.";
Same to insert a quotation mark into a string, just do \".
string observation="The word \"like\" is really overused.";
HTH,
Lukas

Thumbs up