I have a problem with searching for strings in an array. I want to search for one word and if it exists, I want to trace the position of the string in the array.
I believe it should be something like this:
if (myArray contains "11111111") {
trace("*position*")
} else {
trace("cant find it");
}
What kind of syntax are you using there and have you even checked the documentation of the Array class? That should be the first thing you check always, as there is a pretty straightforward method for it:
var arr:Array = ["1111", "2222", "3333"];
trace(arr.indexOf("3333")); //traces the index: 2
Related
I can't seem to find much online, but how can I get the first (and then second and third, later on) entry of an array?
My array is being saved to UserDefaults elsewhere and then pulled for use here.
Thanks!
Did you mean to loop through the elements? You can use for in or forEach for that.
var array: [MyType]
for element in array {
print(element)
}
array.forEach { element in
print(element)
}
Maybe you are trying to iterate value from continually with index also
Here is two way you can get first index value than second index value third index value with index also . hope you will get your result .
First way :
var stringArray = ["a","b","C","D","a","i","x","D"]
for (index, element) in stringArray.enumerated() {
print("Item \(index): \(element)")
}
Another way :
for index in 0 ..< stringArray.count {
print(stringArray[index])
}
let me know if its help you.
I know you shouldn't, I kind of know why. But I mean I don't understand my own code once I am trying really to think what's going on.
So I have an array with bunch of objects. I am iterating over it and once I find an object with specific type, I remove it from the array, and add another object into the array. So something like this:
var arr = parent.allchildren() //getting all the children in array
for ele in arr{
if(ele==somethingHere){
parent.remove(ele)
parent.add(new ele) //add new child into child array
}
}
If I have an array of 1,2,3,4,5, and I remove 3 and add a 6 while iterating, the actual array would be 1,2,4,5,6 but the array I am iterating would still be 1,2,3,4,5.
Which I think it would be fine, because at the end I still get what I want, which removed the element and added the element I need. However modifying the list while iterating it is bad and you shouldn't do that, but for my case I think it does what I need. What could be the potential issue in my case that I can't see?
One thing you may want to think about doing is making all of the changes at the end of the iteration. Instead of making the changes one by one, record the changes you want to make while iterating, and then actually make those changes once your loop is finished.
For example, you could make an array of elements to remove, and an array of elements to add.
//Our array where we record what we want to add
var elementsToAdd = [Any]()
//Our array of what elements we want to remove. We record the index at
//which we want to remove the element from the array
var indexesToRemoveAt = [Int]()
//Getting all the children in array
var arr = parent.allchildren()
//Enumerating an array allows us to access the index at which that
//element occurs. For example, the first element's index would be 0,
//the second element's index would be 1, the third would be 2, and so
//on
for (index,ele) in arr.enumerated() {
if(ele == somethingHere) {
indexesToRemoveAt.append(index)
elementsToAdd.append(newEle)
}
}
//Now that we have recorded the changes we want to make, we could make
//all of the changes at once
arr.remove(at: indexesToRemoveAt)
arr.append(contentsOf: elementsToAdd)
Note that removing array elements at multiple indexes would require the following extension to Array. If you wanted to avoid creating this extension, you could always just loop through the array of indexes and tell the array to remove at each individual index. All this extension function is really doing is looping through the indexes, and removing the array element at said index.
Array extension to remove elements at multiple indexes:
extension Array {
//Allows us to remove at multiple indexes instead of just one
mutating func remove(at indexes: [Int]) {
for index in indexes.sorted(by: >) {
if index <= count-1 {
remove(at: index)
}
}
}
}
I just tested in a playground with the following code:
var arr = ["hi", "bye", "guy", "fry", "sky"]
for a in arr {
if arr.count >= 3 {
arr.remove(at: 2)
}
print(a)
}
print(arr)
This prints:
hi
bye
guy
fry
sky
["hi", "bye"]
So it looks like when you use a for-in loop in Swift, the array is copied and changes you make to it will not affect the array you are iterating over. To answer your question, as long as you understand that this is the behavior, there's nothing wrong with doing this.
I need to sort an array of strings in node.js. I'm using the underscore library and for the following array it doesn't do what it's supposed to do.
This is the array:
var stringsArray =
['2b9ee0e611af977edbfd6bb04a56ebe05af07bdbc2daf88e5617fb872487f27c',
'6f009f9cb714849dba36a302d96dcd940c803bab5aa72dc25f52c26d2a32aab6',
'980942c275b7a407650980d6be561e6260edc55316247f20c3250ffc4d3d3c2f',
'e262181e5298faaefbfcec1f6b3e5684b50a31ed0c751f052a725a6dfafed2a7',
'e5cc268ab1ddd6e650fb5bac1e8d2bee01d2e561674eec0198683757347ded3e'];
Notice that the array is already sorted. This is the sorting code, which I'd expect to do nothing here:
var sortedArray = underscore.sortBy(stringsArray, function (s) { return s.toLowerCase().charCodeAt() * -1; }).reverse();
But the resulting array is unsorted:
[ '2b9ee0e611af977edbfd6bb04a56ebe05af07bdbc2daf88e5617fb872487f27c',
'6f009f9cb714849dba36a302d96dcd940c803bab5aa72dc25f52c26d2a32aab6',
'980942c275b7a407650980d6be561e6260edc55316247f20c3250ffc4d3d3c2f',
'e5cc268ab1ddd6e650fb5bac1e8d2bee01d2e561674eec0198683757347ded3e',
'e262181e5298faaefbfcec1f6b3e5684b50a31ed0c751f052a725a6dfafed2a7' ]
This is a strange one. In most cases I checked the code works. But not this time. What's the issue? Why is the array scrambled? How do you sort it correctly?
EDIT: duh, looks like my code is only sorting on the first character. If anyone has a sort that recursively checks for the entire string I'd appreciate it.
It's not clear if you actually want the array to be sorted or if you want it reverse-sorted. If you want to sort the array you could just use plain javascript
var stringsArray =
['2b9ee0e611af977edbfd6bb04a56ebe05af07bdbc2daf88e5617fb872487f27c',
'6f009f9cb714849dba36a302d96dcd940c803bab5aa72dc25f52c26d2a32aab6',
'980942c275b7a407650980d6be561e6260edc55316247f20c3250ffc4d3d3c2f',
'e262181e5298faaefbfcec1f6b3e5684b50a31ed0c751f052a725a6dfafed2a7',
'e5cc268ab1ddd6e650fb5bac1e8d2bee01d2e561674eec0198683757347ded3e'];
stringsArray.sort()
["2b9ee0e611af977edbfd6bb04a56ebe05af07bdbc2daf88e5617fb872487f27c",
"6f009f9cb714849dba36a302d96dcd940c803bab5aa72dc25f52c26d2a32aab6",
"980942c275b7a407650980d6be561e6260edc55316247f20c3250ffc4d3d3c2f",
"e262181e5298faaefbfcec1f6b3e5684b50a31ed0c751f052a725a6dfafed2a7",
"e5cc268ab1ddd6e650fb5bac1e8d2bee01d2e561674eec0198683757347ded3e"]
If you want an array sorted in reverse, you could just reverse the sorted array
stringsArray.sort().reverse()
["e5cc268ab1ddd6e650fb5bac1e8d2bee01d2e561674eec0198683757347ded3e",
"e262181e5298faaefbfcec1f6b3e5684b50a31ed0c751f052a725a6dfafed2a7",
"980942c275b7a407650980d6be561e6260edc55316247f20c3250ffc4d3d3c2f",
"6f009f9cb714849dba36a302d96dcd940c803bab5aa72dc25f52c26d2a32aab6",
"2b9ee0e611af977edbfd6bb04a56ebe05af07bdbc2daf88e5617fb872487f27c"]
so I am working on a graphical calculator (bit more of a challenge than the basic windows one), and I want to be able to do the entire "math" in one textfield, just like typing in "5+3-5*11/3" and it gives you the solution when you press '='
I decided to make it with arrays of numbers and symbols, but I have no idea how to make it to fill the next array if this one already is used:
var numbers:Array = new Array("","","","","","","","","","","","","","","","");
var actions:Array = new Array("","","","","","","","","","","","","","","","");
I am using split to split the numbers I input with symbols, and I want the numbers to be placed in the arrays. Example: I type in 555+666 and then I need to have something like
if (numbers[0] = "") {numbers[0] = 555}
else if (numbers[1] = "") {numbers[1] = 555}
else if.....
Know what I mean?
Pretty hard to describe...
something like... When I type in a number, if the numbers[0] is already filled, go fill in numbers[1], if numbers[1] is filled, go to numbers[2] etc
Even if I agree with #Nbooo and the Reverse Polish Notation
However Vectors may have a fixed length.
This is not an answer but just an example (if the length of Your Array must be defined):
//Just for information..
var numbs:Vector.<Number> = new Vector.<Number>(10,true);
var count:uint = 1;
for (var i in numbs){
numbs[i] = count++
}
trace(numbs);
// If You try to add an element to a Vector,
// You will get the following Error at compile time :
/*
RangeError: Error #1126: Cannot change the length of a fixed Vector.
at Vector$double/http://adobe.com/AS3/2006/builtin::push()
at Untitled_fla::MainTimeline/frame1()
*/
numbs.push(11);
// Will throw an Error #1126
trace(numbs);
If You use this code to update a fixed Vector, this will not throw an ERROR :
numbs[4]=11;
trace(numbs);
Output :
1,2,3,4,5,6,7,8,9,10
1,2,3,4,11,6,7,8,9,10
// length is 10, so no issue...
If You consider the performance between Arrays and vectors check this reference : Vector class versus Array class
I hope this may be helpful.
[EDIT]
I suggest you to check at those links too :
ActionScript 3 fundamentals: Arrays
ActionScript 3 fundamentals: Associative arrays, maps, and dictionaries
ActionScript 3 fundamentals: Vectors and ByteArrays
[/EDIT]
Best regards.
Nicolas.
What you want to implement is the Reverse Polish Notation. In actionscript3 arrays are dynamic, not fixed size, that means you can add elements to the array without concern about capacity (at least in your case).
const array:Array = new Array();
trace(array.length); // prints 0
array.push(1);
array.push(2);
trace(array.length); // prints 2
I suggest using "push" and "pop" methods of Array/Vector, since it's much more natural for such task. Using those methods will simplify your implementation, since you'll get rid of unnecessary checks like
if (numbers[1] == "") {...}
and replace it just with:
numbers.push(value);
and then to take a value from the top:
const value:String = numbers.pop();
I have a set of Strings, which I split it out and save it into an Array, and try to loop it with api as a parameter, but it keep crash with error
index out of range
why is it? Need help pls
You should learn about array basic ideas. This error shows becuse the item you request is not available in the array.
for i in 0...qrCoderArrau.count - 1 {
}
The error is because you are trying to access an element in array with an index greater than its size - 1.
Your qrCodeArray Array is empty also you need to for loop less one to the count of qrCodeArray. So change you for loop like this.
for i in 0..<qrCodeArray.count {
}
In your case for i in 0...qrCoderArray.count { it will execute for loop if your qrCoderArray.count is 0.
To avoid those errors (the last index of an array is count-1) use always
for qrCode in qrCodeArray { ...
rather than an index loop and even if you need the index use
for (index, qrCode) in qrCodeArray.enumerate() { ...
I think best solution is foreach in these situations
for qr in qrCoderArray {
...
}