Source language: Translate to:

Variables and Arrays

General questions about NeoAppBuilder - our rapid application development tool for building HTML5, web and mobile apps.

Moderator: Neosoft Support

Variables and Arrays

Postby PaulTomo » Sun Jan 25, 2015 6:50 am

I'm trying to understand how to pull a single element from an array.

What I would like to do (ultimately) is read a text file into an array and then generate a Radom number and display what ever was in that line.

My first hurdle is how to get the single element!

Code: Select all
CreateArray [Alphabet] "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
jsPrompt "Type a number from 1 to 26 to find the position of a letter in the Alphabet" "" [Pos]
SetVar [Letter] "[Alphabet[Pos]]"
AlertBox "Result" "The letter at position [Pos] in the Alphabet is [Letter]" ""


Using the CreateArray all is well but as we can not now use nested variables "[Alphabet[Pos]]" as I would in NeoBook.

How would I single out the element requested and now in the variable [Pos]

Still not sure how to read the file into an array yet but need to understand this first.

One thing at a time..

Thanks
User avatar
PaulTomo
 
Posts: 62
Joined: Tue Apr 28, 2009 1:15 am
Location: UK

Re: Variables and Arrays

Postby Tony Kroos » Sun Jan 25, 2015 7:21 am

I believe you have to use
Code: Select all
SubArray [Alphabet] [pos] 1 [Letter]

but it doesn't work because [pos] is not recognized as number. If you place number explicitly then it works:
Code: Select all
SubArray [Alphabet] 1 1 [Letter]

It gives B (note that array is zero-based)

and this sample script seems broken
Code: Select all
CreateArray [Alphabet] "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
jsPrompt "Type a number from 1 to 26 to find the position of a letter in the Alphabet" "" "[Pos]"
AlertBox "Pos contains this number:" "[pos]" ""
SubArray [Alphabet] [pos] 1 [Letter]
AlertBox "Result" "The letter at position [Pos] in the Alphabet is [Letter]" ""

now we see the problem in AlertBox - [pos] contains undefined expression instead of number. Also it's a bit confusing - two AlertBoxes overlap itself (do not block script flow when fired as usual). And I have similar question - how to reference array item without explicitly copying it into new array?
Tony Kroos
 
Posts: 402
Joined: Thu Oct 15, 2009 3:43 pm

Re: Variables and Arrays

Postby Gaev » Sun Jan 25, 2015 12:45 pm

PaulTomo, Tony Kroos:

1) This works ...
Code: Select all
SetVar [startAt] 2
SetVar [numItems] 1
SubArray [gkArray] "[startAt]" "[numItems]" [newArray]
AlertBox "newArray" "[newArray]"


2) But this does not ...
Code: Select all
SetVar [startAt] "2"
SetVar [numItems] "1"
SubArray [gkArray] "[startAt]" "[numItems]" [newArray]
AlertBox "newArray" "[newArray]"

... i.e. need "numeric variables" (not "string variables").

3) But when you use jsPrompt, the result is stored in a "string variable" ... so here is a way around the problem ...
Code: Select all
SetVar [numItems] 1

jsPrompt "Type a number from 0 to 25" "" "[startAtString]"

BeginJS
   $rootScope.startAt = Number($rootScope.startAtString);
EndJS

SubArray [gkArray] "[startAt]" "[numItems]" [newArray]
AlertBox "newArray" "[newArray]"


4)
how to reference array item without explicitly copying it into new array?

This seemed to work ...
Code: Select all
SetVar [newString] "[newArray]"
AlertBox "newString" "[newString]"

... but I can not tell if [newString] is truly a string variable or an array variable (with just one item).
User avatar
Gaev
 
Posts: 3717
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: Variables and Arrays

Postby PaulTomo » Sun Jan 25, 2015 1:08 pm

Gaev,

Thanks for this, I knew it was something to do with the fact it was a String Variable after playing around with the suggestion from Tony.

Seems to be all working now.

A lot to learn for us novices by the looks of things but thanks to this forum, help is never to far away.
User avatar
PaulTomo
 
Posts: 62
Joined: Tue Apr 28, 2009 1:15 am
Location: UK

Re: Variables and Arrays

Postby Neosoft Support » Mon Jan 26, 2015 12:38 pm

There is no way for you to know this, but you can reference array elements like this:

SetVar [Letter] "[Alphabet(5)]"

This will place the fifth* element of Alphabet into the variable [Letter]. You can also do this:

SetVar [Letter] "[Alphabet([Pos])]"

This exception to the no nested variables rule works with arrays and in code only.

On-screen, you can display an array element like this:

[Alphabet|element:5]

or with the [Pos] variable:

[Alphabet|element:(Pos)]

*Actually this is the sixth element since the first element is zero.
NeoSoft Support
Neosoft Support
NeoSoft Team
 
Posts: 5593
Joined: Thu Mar 31, 2005 10:48 pm
Location: Oregon, USA


Return to General NeoAppBuilder Discussion

Who is online

Users browsing this forum: No registered users and 0 guests