As you can see from below I have an array, I would like to remove the svg from the last item in that array when it runs. How might I do this with a condition? Something like if last:item else add svg
-navlinks = {"Home":"/Home", "About":"/About", "Store Directory":"/Store-Directory", "Store Page":"/Store-Page", "Events":"/Events",}
ul.navbar-menu
for val, key in navlinks
li
a(href='#{val}') #{key}
svg.icon.icon-dots
use(xlink:href="#icon-dots")
Well the thing is, contrary to what you said, navlinks is not an Array, but rather an Object. Since Object elements do not have a numeric index, the notion of last does not have much meaning.
However, you could iterate over Object.keys(navlinks) which is a proper Array with a numeric index. So you could do something like :
-navlinks = {"Home":"/Home", "About":"/About", "Store Directory":"/Store-Directory", "Store Page":"/Store-Page", "Events":"/Events",}
ul.navbar-menu
- each key, index in Object.keys(navlinks)
li
a(href='#{val}')= navlinks[key]
if index < Object.keys(navlinks).length - 1
svg.icon.icon-dots
use(xlink:href="#icon-dots")
Related
How can you display a specific item in an array with Pug? For example:
each answer in answers
li!= answer.Response
Will display each item in the array. But, say I wanted just the the third item or, better yet, pass a variable for a specific index to display. What is the syntax for this?
- const indexIwant = 2;
if answers && answers.length>indexIwant
li=answers[indexIwant]
You need to ensure answers is not null and has at least the number of items to include the indexed item you want.
Another thing: don't use != unless you know exactly what data you are handling.
The simplest way to access a specific index of an array in pug:
-const meals = ["breakfast", "lunch", "dinner"]
-const favoriteDishes = ["coffee & doughnut salad","cheese danish soup","red wine","banana split sandwich"]
-const sides = ["ranch dressing","chutney","ketchup","chocolate sauce"]
p I reckon I will fix myself a hefty helping of #{favoriteDishes[2]} for #{meals[0]} with a side of #{sides[2]}.
Considering that indentation and whitespace is everything in jade / pug, this works :))
I'm designing a Mastermind game, which basically compares 2 lists and marks the similarities. When a colour is found at the right place, a flag making the correct position is added and the item found on the reference list is marked off. The reference list is feeding off an array from another function. The problem is at the mark off, as any changes done to the reference list is changing also the original array, which i don't want it to happen
tempCode = mCode #mCode is the array combination randomly generated from another function
for i in range (len(uCode)): #user input array
for j in range (len(tempCode)): #temp array
if uCode[i] == tempCode[j]: # compare individual chars
if i == j: #compare position
flagMark = "*"
tempCode.insert(j+1, "x") #problem starts here
tempCode.remove(tempCode[j])
fCode.append(flagMark)
When the insert is reached both the tempCode and mCode change which it is not intended.
The code is written in a way should the user enter a combination of the same colours, thus checking the chras(the colours are just letters) and the position, and then mark them of with "x"
As it stands, when it gets to
tempCode.insert(j+1, "x")
the arrays will change to
mCode = ["B","R","x","G","Y"]
tempCode = ["B","R","x","G","Y"]
when I would just want
mCode = ["B","R","G","Y"]
tempCode = ["B","R","x","G","Y"]
See also this answer, which is a different presentation of the same problem.
Essentially, when you do tempCode = mCode, you're not making a copy of mCode, you're actually making another reference to it. Anything you do to tempCode thereafter affects the original as well, so at any given time the condition tempCode == mCode will be true (as they're the same object).
You probably want to make a copy of mCode, which could be done in either of the following ways:
tempCode = mCode.copy()
tempCode = mCode[:]
which produces a different list with the same elements, rather than the same list
a = {0:[[1,2,3], [1,3,4,5]]}
print([1,2,3] in a.values())
I get False. Because this list is in values I need True. Is it possible to check all lists in nested list as a value in dictionary? Maybe without loops?
Since you're using python3 you can do this:
[1,2,3] in list(a.values())[0]
a.values() returns a dictionary view. dictionary views
Then you can wrap the dictionary view into list but this list will contain only one element which can be accessed by index 0. However, if your dictionary contains several keys and corresponding values then list(a.values()) will contain the same number of elements (values mapped to keys) as keys in the dictionary.
Note that when you use some_value in some_collection construct and don't use loops explicitly it will still iterate through the collection.
I'm making a game of hangman. I use a list to keep track of the word that you are guessing for, and a list of blanks that you fill in. But I can't figure out what to do if for example someone's word was apple, and I guessed p.
My immediate thought was to just find if a letter is in the word twice, then figure out where it is, and when they guess that letter put it in both the first and second spot where that letter is. But I can't find
How to test if two STRINGS are duplicates in a list, and
If I were to use list.index to test where the duplicate letters are how to I find both positions instead of just one.
Create a string for your word
Create a string for user input
Cut your string into letters and keep it on a list/array
Get input
Cut input into letters and keep it on another array
Create a string = "--------" as displayed message
Using a for loop check every position in both array lists and compare them
If yourArray[i] == inputArray[i]
Then change displayedString[i] = inputArray[i] and display message then get another input
If it doesnt match leave "-" sings
Displayed the "---a--b" string
One way to do it would be to go through the list one by one and check if something comes up twice.
def isDuplicate(myList):
a = []
index = 0
for item in myList:
if type(item) == str:
if item in a:
return index
else:
a.append(item)
index += 1
return False
This function goes through the list and adds what it has seen so far into another list. Each time it also checks if the item it is looking at is already in that list, meaning it has already been seen before. If it gets through the whole list without any duplicates, it returns False.
It also keeps track of the index it is on, so it can return that index if it does find a duplicate.
Alternately, If you want to find multiple occurrences of a given string, you would use the same structure with some modifications.
def isDuplicate(myList, query):
index = 0
foundIndexes = []
for item in myList:
if item == query:
foundIndexes.append(index)
index += 1
return foundIndexes
This would return a list of the indexes of all instances of query in myList.
In my documents there are two elements(<a> and <b>) on which range indexes(of the same type) exist. I want all those documents in which the values of <a> and <b> are same. I understand that using cts:element-value-co-occurrences() I can fetch the pair of values of <a> and <b> from each fragment and compare the values. But how do I refer back to the fragment where a match is found? Or is there a simpler way to do this? All I want is the range indexes to get utilized.
The co-occurences functions return a list of all existing (within-fragment) value combinations of those two elements. If you simply look for all documents in which the value of element a is equal to the value of element b, you could do something like:
for $v in cts:element-values(xs:QName("a"))
return
cts:search(
collection(),
cts:and-query((
cts:element-value-query(xs:Qname("a"), $v),
cts:element-value-query(xs:Qname("b"), $v)
))
)
Or you could use cts:uris instead of cts:search to find the database uris of those docs..
ADDED:
What #mblakele in the comment below means is this:
let $query :=
cts:or-query(
for $v in cts:element-values(xs:QName("a"))
return
cts:and-query((
cts:element-value-query(xs:Qname("a"), $v),
cts:element-value-query(xs:Qname("b"), $v)
))
)
return
cts:search(
collection(),
$query
)
That saves you from doing cts:search for each value separately, and is likely to perform quicker..
HTH!