Stylus, refer to variable name and value - stylus

I want...
I am trying to do a loop like this
my-red = #fcc
my-blue = #ccf
for color in my-red, my-blue
.{color}
color x
I want it to output
.my-blue {
color: #ccf;
}
.my-red {
color: #fcc;
}
I can't seem to get both the variable name, and value as required
I have tried...
my-blue = #ccf
my-red = #fcc
for x in 'my-blue' my-red
.{x}
color x
But I just get either the class name or color name (depending on if I use a string as the variable name to iterate)
.my-blue {
color: "my-blue";
}
. {
color: #fcc;
}

You can use array for such task, so you can do this:
my-colors = my-red #fcc,
my-blue #ccf
for pair in my-colors
.{pair[0]}
color pair[1]
Doing so you declare the my-colors array and then iterate through it, using the first elements in the pairs as the names and second as the value.

Related

How to check string existance in string arraylist in flutter?

RxList<String> selectedMemberList = <String>[].obs;
selectedMemberList = [180,160,150]
I want to display text in red color if ID contains in selectedMemberList.
So, I simply called,
if selectedMemberList.contains('180'){
text in red color
}else{
text in black color}
but, I got text in black color every time. That means ID doesnt matched.
How do I match member ID to given selectedMemberList.
if your items are in a single string separated by a comma, first convert that into a list like this :
String collectedIds = "180,160,150";
final split = tagName.split(',');
final Map<int, String> selectedMemberList= {
for (int i = 0; i < split.length; i++)
i: split[i]
};
and now you can check the condition :
if selectedMemberList.contains('180'){
text in red color
}else{
text in black color}
Hope this will help you.
Here is a cleaner way on checking if value exist on a list.
RxList<String> selectedMemberList = <String>[].obs;
selectedMemberList = ["160","180","150"];
if (selectedMemberList.where((item) => item == "180").isNotEmpty){
text in red color
} else {
text in black color
}

How do you assign multiple values in a key for a table?

I am trying to assign multiple values for one variable in a table. One for the string name, and one for an integer. The code would go:
items = {
potion = "Potion", 100
}
I do not know how to formally write this, and how to call for those specific values.
(Do you call it like this?)
io.write(item.potion.1) --> Potion
io.write(item.potion.2) --> 100
(Or something else?)
Please help. :I
You can assign those values to a table indexed by numbers or identifiers:
-- identifiers
items = {
potion = {name = "Potion", value = 100},
}
print(items.potion.name, items.potion.value)
-- numeric indexes
items = {
potion = {"Potion", 100},
}
print(items.potion[1], items.potion[2])
I personally prefer the former approach (as it's more maintainable, even though a bit more verbose), but either one should work.
Lua allows for multiple assignments to multiple variables.
like so:
potion, value = "Potion", 100
but this can not be done inside a table definition.
items = {
potion = "Potion", 100
}
What your code here is doing is setting potion to the value "Potion" and then the , ends the assignment. The next assignment is 100 which will be assigned to a default key, in this case 1.
In side a table you end each assignment with a , so your tables contents are equal to:
items = {
potion = "Potion",
[1] = 100
}
To accomplish the desired behavior you can nest tables:
items = {
potion = {
"Potion",
100
}
}
This example can be accessed like items.potion[1] not like items.potion.1 this is because the . notation can't be used with a key that begins with a number.

How to change background colour of an element at index of array?

I want to create a drop down like this:
.
I have an string array as datasource of an drop down menu.I want to give background to element at 3rd index of an array.
I have tried it in doing all things possible but not able to find out any solution.
If you insert your values to list of UIButtons (let btns : [UIButton] = [btn0, btn1, btn2, btn3]), you can do that:
for i in 0..<btns.count {
if i == 2 {
btns[i].backgroundColor = .red
}
}
or just find needed UI-element and set needed color.
You need to create an array of similar size as your dataSource and this is only if you can't add a property of the color
var arr = [UIColor]()
arr = [.red,.green,.blue]
arr[2] = .orange

Cannot get first item of array

Here's an array printed by a function getTargets();:
{
name = {
isPlayer = true,
isBlocking = false,
username = "yes"
}
}
When I do players = getTargets(); to put this in variable and want to access first variable no matter its name, I have some trouble. I tried those:
players.name.username --displays "yes"
players[0].username --displays nil
players[1].username --displays nil
I want to access first variable of this array no matter what is its value.
How can I do this?
Your code
local players = {
name = {
isPlayer = true,
isBlocking = false,
username = "yes"
}
}
is equivalent to
local players = {}
players.name = {
isPlayer = true,
isBlocking = false,
username = "yes"
}
So there is no index 0 or 1, hence players[0] and players[1] are nil.
players[0].username and players[1].username will cause an error for indexing nil values.
To get the first element of a table of unknown keys simply do this:
local key, value = next(someTable)
https://www.lua.org/manual/5.3/manual.html#pdf-next
When called with nil as its second argument, next returns an initial
index and its associated value.
Keep in mind that:
The order in which the indices are enumerated is not specified, even
for numeric indices.
If you want to make sure you should change your data structures accordingly.
But I cannot give you much advice here as I don't know the purpose of this.
You could have a little function like (simplyfied):
local function addPlayerToList(playerList, playerLookUpTable, player)
table.insert(playerList, player)
playerLookUpTable[player.name] = #playerList
end
Read something about OOP in Lua for nicer and more advanced ideas.
You can try to get the key/name in this way:
local players = {
name = {
isPlayer = true,
isBlocking = false,
username = "yes"
}
}
local FirstPlayer
for k,v in pairs(players) do FirstPlayer=k break end
print(players[FirstPlayer].username)
however there is no guarantee that this will always be the first. But maybe this is your case.
You don't even need a for loop:
n,t = pairs(players)
firstKey, firstValue = n(t)
https://repl.it/JBw1/1
As lhf pointed out, you don't even need pairs, you can simply do
firstKey, firstValue = next(players)

swift - using .map on struct array

i have a struct array that i want "break up" into smaller arrays that can be called as needed or at least figure out how i can map the items needed off one text value.
the struct:
struct CollectionStruct {
var name : String
var description : String
var title : String
var image : PFFile
var id: String
}
and the array made from the struct
var collectionArray = [CollectionStruct]()
var i = 0
for item in collectionArray {
print(collectionArray[i].name)
i += 1
}
printing partArray[i].name gives the following result:
pk00_pt01
pk00_pt02
pk00_pt03
pk01_pt01
pk01_pt02
pk01_pt03
pk01_pt04
pk01_pt05
pk01_pt06
pk01_pt07
pk01_pt08
this is just some test values but there could be thousands of entries here so i wanted to filter the entire array just by the first 4 characters of [i].name i can achieve this by looping through as above but is this achievable using something like .map?
I wanted to filter the entire array just by the first 4 characters of
[i].name
You can achieve this by filtering the array based on the substring value of the name, as follows:
let filteredArray = collectionArray.filter {
$0.name.substring(to: $0.name.index($0.name.startIndex, offsetBy: 4)).lowercased() == "pk00"
// or instead of "pk00", add the first 4 characters you want to compare
}
filteredArray will be filled based on what is the compared string.
Hope this helped.
If you want to group all data automatically by their name prefix. You could use a reducer to generate a dictionary of grouped items. Something like this:
let groupedData = array.reduce([String: [String]]()) { (dictionary, myStruct) in
let grouper = myStruct.name.substring(to: myStruct.name.index(myStruct.name.startIndex, offsetBy: 4))
var newDictionart = dictionary
if let collectionStructs = newDictionart[grouper] {
newDictionart[grouper] = collectionStructs + [myStruct.name]
} else {
newDictionart[grouper] = [myStruct.name]
}
return newDictionart
}
This will produce a dictionary like this:
[
"pk00": ["pk00_pt01", "pk00_pt02", "pk00_pt03"],
"pk01": ["pk01_pt01", "pk01_pt02", "pk01_pt03", "pk01_pt04", "pk01_pt05", "pk01_pt06", "pk01_pt07"],
"pk02": ["pk02_pt08"]
]
Not sure if i am understanding you correctly but it sounds like you are looking for this...
To create a new array named partArray from an already existing array named collectionArray (that is of type CollectionStruct) you would do...
var partArray = collectionArray.map{$0.name}

Resources