I have two arrays
print(">>>>>>>>>>>>>>>>>>>>>>deviceStartList")
dump(deviceStartList)
print(">>>>>>>>>>>>>>>>>>>>>>")
and
print(">>>>>>>>>>>>>>>>>>>>>>deviceEndList")
dump(deviceEndList)
print(">>>>>>>>>>>>>>>>>>>>>>")
It product this result:
I came from a PHP background when I can just call array_diff() , done.
How would one go about doing this?
If you are looking to do something like set subtraction, Swift has that. See Set operations (union, intersection) on Swift array?.
The title of the SO post above doesn't per se mention the methods subtract(_:) or subtracting(_:), but the content of the accepted answer does cite subtract(_:).
In Swift 4.1, You could do something like:
var deviceStartList = ["12345", "67890", "55555", "44444"]
var deviceEndList = ["12345", "55555"]
var deviceStartSet = Set<String>(deviceStartList)
var deviceEndSet = Set<String>(deviceEndList)
let devicesDiff = deviceStartSet.subtracting(deviceEndSet)
And if you need an array as the final output, you can get that by doing this:
var devicesDiffArray = [String](devicesDiff)
Here is a screenshot of a Playground with this working:
This is Set way. Hope it's working for you.
var a=["B8D7","38C9","484B", "F4B7"]
var b=["484B","F4B7"]
Set(a).symmetricDifference(Set(b))
You can try
let result = deviceStartList.filter { deviceEndList.contains($0) == false }
Also I strongly recommend the Set way as it's internally optimized rather than the usual way
Related
I try to use Google Script Apps (instead of VBA which I am more used to) and managed now to create a loop over different spreadsheets (and not only different sheets in one document) using the forEach function.
(I tried with a for (r=1;r=lastRow; r++) but I did not manage).
It is working now defining the array for the sheetnames manually:
var SheetList = ["17DCu1nyyX4a6zCkkT3RfBSfo-ghoc2fXEX8chlVMv5k", "1rRGQHs_JShPSBIGFCdG6AqXM967JFhdlfQ92cf5ISL8", "1pFDyXgYmvC5gnN5AU5xJ8vGiihwtubcbG2n4LPhPACQ", "1mK_X4Q7ysJQTt8NZoZASBE5zuUllPmmWSJsxu5Dnu9Y", "1FpjIGWTG5_6MMYJF72wvoiBRp_Xlt5BDpzvSZKcsU"]
And then for information the loop:
SheetList.forEach(function(r) {
var thisSpreadsheet = SpreadsheetApp.openById(r)
var thisData = thisSpreadsheet.getSheetByName('Actions').getDataRange()
var values = thisData.getValues();
var toWorksheet = targetSpreadsheetID.getSheetByName(targetWorksheetName);
var last = toWorksheet.getLastRow ()+ 1
var toRange = toWorksheet.getRange(last, 1, thisData.getNumRows(), thisData.getNumColumns())
toRange.setValues(values);
})
Now I want to create the definition of the array "automatically" out of the spreadsheet 'List' where all spreadsheets which I want to loop are listed in column C.
I tried several ideas, but always failed.
Most optimistic ones were:
var SheetList = targetSpreadsheetID.getSheetByName('List').getRange(2,3,lastRow-2,3).getValues()
And I also tried with the array-function:
var sheetList=Array.apply(targetSpreadsheetID.getSheetByName('List').getRange(2,3,lastRow-2,3))
but all without success.
It should be possible normally in more or less one single line to import the array from the speadsheet to the Google apps scripts?
I would very much appreciate if someone could please give me a hint where my mistake is.
Thank you very much.
Maria
I still did not manage to put the array as I wanted it initially, but now I found a workable solution with the For Loop which I want to share here in case someone is looking for a similar solution (and then finds at least my workaround ;) )
for (i=2; i<lastRow;i++){
var SheetList = targetSpreadsheetID.getSheetByName('List').getRange(i,3).getValues()
Logger.log(SheetList);
var thisSpreadsheet = SpreadsheetApp.openById(SheetList);
... // the rest identical to loop above...
Don't hesitate to add your comments or advice anyhow, but I will mark the question as closed.
Thanks a lot.
Maria
First off, I'm a relative amateur with app design having taught myself high-level swift/xcode last year, so apologies for my code in advance!
I've developed a game which has an array of a 'Player' Class called playerList. I currently convert this playerList array to JSON via Encoder and then save to device...however as my array grows, this exercise is beginning to take a long time, so I'm looking for an alternative. I presume the best solution is to rewrite the app to use CoreDate, SQLite etc, but I'm looking for a quick solution for now.
I could have used userDefaults, however steered away from this as large array and am instead trying to fudge a solution using Realm.
I've attempted the below, but whenever I look at my playerList after loading it is empty. Am I missing something obvious here, or alternatively is there a much better approach than using Realm?
class PlayerArray: Object {
var iden: Int = 0
var allThePlayers: [Player] = playerList
}
func saveViaRealm() {
// Get the default Realm
let realm = try! Realm()
// Define player list
let realmPlayerList = PlayerArray()
realmPlayerList.allThePlayers = playerList
realmPlayerList.iden = 1
// Write to realm
try! realm.write {
realm.add(realmPlayerList)
}
}
func loadViaRealm() {
// Get the default Realm
let realm = try! Realm()
//Retrieve objects from realm
let realmOutputPlayerList = realm.objects(PlayerArray.self)
// Filter to iden required
let realmFiltered = realmOutputPlayerList.filter{$0.iden == 1}[0]
// Assign to playerList
playerList = realmFiltered.allThePlayers
}
I would take a read through the Realm documentation once more around Lists and declaring variables. In your object class are you getting any errors? RealmSwift should be declared with #objc dynamic vars. Also, you shouldn't need but one let = realm. Here is the link to Realm.io documentation.
Please forgive my terminology, Im not educated on the proper.
Lets say I have multiple movieclip variables
var rblock1:MovieClip = new Rblock();
var rblock2:MovieClip = new Rblock();
var rblock3:MovieClip = new Rblock();
var yblock1:MovieClip = new Yblock();
var yblock2:MovieClip = new Yblock();
var yblock3:MovieClip = new Yblock();
I have them added to an array
var blockarray:Array = new Array(rblock1, rblock2, rblock3, yblock1, yblock2, yblock3);
var block
I want to create a for loop with an if statement that triggers if a variable is Rblock and not Yblock, for example
for each (block in blockarray)
{
if (block==Rblock)
{
trace("rblock");
}
}
The issue is that obviously "if (block==Rblock)" doesnt work.
How should this be written?
You apparently want to check if a block is red or yellow by checking against its class name. You can do it with this:
if (block is Rblock) {...} // yes, red
I have figured out a work around not really a perfect solution, which will only work for certain scenarios...
if each class has a unique trait you can identify it that way, for example...
if all variables defined by the Rblock class are wider than the Yblock class you could say
if (block.width>x) { trace(Rblock); }
Like I said this is only a work around though and only works for movieclip variables defined by classes that are different, if anyone has the actual solution please post
for example
var imageViewArray:UIImageView = [imageView1,imageView2,imageView3]
I want to chage sameimageView.image = img or imageView.isUserInteractionEnabled = false to all Image View inside the array
1. It's an array
First of all it's not
var imageViewArray:UIImageView
but
var imageViewArray:[UIImageView]
because you want an array of UIImageView right?
2. Naming conventions
Secondly is Swift we don't name a variable after it's type so imageViewArray becomes imageViews.
3. map
Now if you really hate the for in and the foreach your can write
imageViews = imageViews.map { imageView in
imageView.isUserInteractionEnabled = true
return imageView
}
or as suggested by Bohdan Ivanov in the comments
imageViews.map { $0.isUserInteractionEnabled = true }
4. Wrap up
This answer shows you how to use the wrong construct (map) to do something that should be made with the right construct (for in).
That's the point of having several constructs, everything could be made with an IF THEN and a GOTO. But a good code uses the construct that best fits that specific scenario.
So, the best solution for this scenario is absolutely the for in or the for each
imageViews.forEach { $0.isUserInteractionEnabled = true }
I am trying to define an array with single element... so,
var arr:Array = new Array(1,2,3,4) // arr[0] = 1
// but
var arr:Array = new Array(1) // arr[0] = undefined
//Also,
var arr:Array = new Array([1]) // arr[0] = 1 , << ILLUSION
//Because, arr[0] is NOT A NUMBER, IT ITSELF IS OF TYPE=> ARRAY.
var arr:Array = [1] //arr[0]=1 but i think it's AS1.0 notation..
So, is their any AS3.0 way of defining array with single element ?
var arr:Array = [1]; //arr[0]=1 but i think it's AS1.0 notation..
Why? This is perfectly legal shorthand array initialization, and it's exactly the way to do it.
Lol, I remember dealing with this a year or 2 back, the way I did it was with 2 lines.
var arr:Array = new Array();
arr[0] = "the element";
This is because the constructor for Array accepts the size of the array as an argument if you are passing a single integer value. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#Array()
weltraumpirat is right only in that the code will compile, it's still actionscript 1/2 notation (AVM1). You said you want to know the "AS3 way".... and one major difference and benefit of AS3 (AVM2) over AS1/AS2 (AVM1) is strict typing. Hence the creation of the Vector object, aka a strictly typed array (and it's faster because of that strict typing). Here is the proper way to initialize a typed array with 1 or more defined objects:
var vector:Vector.<String> = Vector.<String>(["v1", "v2", "v3"]);
See more here:
http://www.daveoncode.com/2009/04/06/actionscript-vector-class-initialization-with-a-source-array/
Edit For all the people who don't know what they're talking about:
http://www.mikechambers.com/blog/2008/09/24/actioscript-3-vector-array-performance-comparison/
Simple test, vector == 40% faster than array
http://www.masonchang.com/blog/2011/4/21/tamarin-on-llvm-more-numbers.html
Summary of tamarin JIT tests, typed variables performing 20% or more faster than un-typed in every scenario.
For the people who REALLY don't know what they're talking about, Tamarin IS the flash virtual machine (at least the open source component, the core minus the UI and other things).
Edit... again.. sigh
For people who do not understand what "context" is... when I say that the vector is FASTER... I'm speaking of the overall performance of the object in the virtual machine. This is not my own claim, it comes from adobe themselves and there are benchmarks from a flash platform evangelist included in my answer ( or rather a link to ). Maybe the people who are arguing with me don't have English as their first language.....
var myArray:Array = new Array();
myArray.push(1);
trace(myArray[0]); //1