I would like to replace the contents of a variable with a value that can be found in a mapping-table.
For example:
Variable priority contains the value 3, and should be replaced with the string medium according to the following table:
key value
------------
1 none
2 low
3 medium
4 high
How do I accomplish this without having a giant mess of nested replace-markup?
You could use a switch statement from the same extension https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#%23switch
Related
Table has field containing the list of IDs separated by "-".
Example: 559-3319-3537-4345-29923
I need to check rows that use at least 4 of the specified identifiers using regex
Example: before inserting to the db, I need to check the value 559-3319-3537-29923-30762 for this condition.
I've build a pattern that only works in the specified order, but if the IDs are swapped, it doesn't work.
Template: ^.*\b(-*(559|3319|3537|29923|30762)-*){4,}\b.*$
Initially, I thought that a simple (559|3319|3537|29923|30762){4,} should be enough, but in this case it also doesn't work, although it sees all 4 values without a quantifier.
Please tell me how to write such an expression correctly.
For ease of reading/testing, I've simplified the Ids being searched for to single digit integers 1-5. The following pattern will match strings with at least 4 out of the 5 ids:
(\b(1|2|3|4|5)\b.*){4,}
(Play with this here)
OR MySQL's regex dialect:
([[:<:]](1|2|3|4|5)[[:>:]].*){4,}
(Play with MySQL version here)
Here are some examples:
#
Example
Is Match?
Description
1
1-2-3-4-5
YES
All the Ids
2
1-2-3-9-5
YES
Enough Ids
3
1-1-9-1-1
YES
Enough Ids, but there are repeats
4
9-8-7-6-0
NO
None of the Ids
5
1-2-3-9-9
NO
Some, but not enough of the Ids
If the repeated Ids as shown in example 3 are an issue, then regex is probably not a good fit for this problem.
Edit:
^.*\b((559|3319|3537|29923|30762)-?([0-9]*)?-?){4,}\b.*$
The reasoning behind this is that each group is not just one of the 5 numbers, but it can include some extra characters. So the matched groups in your example are:
(559-)
(3319-)
(3537-4345-)
(29923)
Original answer:
This would be one way to do it (not sure if there are other ways to do it):
^.*\b(559|3319|3537|29923|30762)[0-9-]*(559|3319|3537|29923|30762)[0-9-]*(559|3319|3537|29923|30762)[0-9-]*(559|3319|3537|29923|30762)\b.*$
How does one capture a value recursively with regex, where value is a part of a group that repeats?
I have a serialized array in mysql database
These are 3 examples of a serialized array
a:2:{i:0;s:2:"OR";i:1;s:2:"WA";}
a:1:{i:0;s:2:"CA";}
a:4:{i:0;s:2:"CA";i:1;s:2:"ID";i:2;s:2:"OR";i:3;s:2:"WA";}
a:1 stands for array:{number of elements}
then in between {} i:0 means element 0, i:1 means element 1 etc.
then the actual value s:2:"CA" means string with length of 2
so I have 2 elements in first array, 1 element in the second and 4 elements in the last
I have this data in mysql database and I DO NOT HAVE an option to parse this with back-end code - this has to be done in mysql (10.0.23-MariaDB-log)
the repeating pattern is inside of the curly braces
the number of repeats is variable (as in 3 examples each has a different number of repeating patterns),
the number of repeating patterns is defined by the number at 3rd position (if that helps)
for the first example it's a:2:
and so there are 2 repeating blocks:
i:0;s:2:"OR";
i:1;s:2:"WA";
I only care to extract the values in bold
So I came up with this regex
^a:(?:\d+):\{(?:i:(?:\d+);s:(?:\d+):\"(\w\w)\";)+}$
it captures the values I want all right but problem is it only captures the last one in each repeating group
so going back to the example what would be captured is
WA
CA
WA
What I would want is
OR|WA
CA
CA|ID|OR|WA
these are the language specific regex functions available to me:
https://mariadb.com/kb/en/library/regular-expressions-functions/
I don't care which one is used to solve the problem
Ultimately I need this in as sensible form that can be presented to the client e.g. CA,ID,OR or CA|ID|OR
Current thoughts are perhaps this isn't possible in a one liner, and I have to write a multi-step function where
extract the repeating portion between the curly braces
then somehow iterate over each repeating portion
then use the regex on each
then return the results as one string with separated elements
I doubt if such a capture is possible. However, this would probably do the job for your specific purpose.
REGEXP_REPLACE(
REGEXP_REPLACE(
REGEXP_REPLACE(str1, '^a:\\d+:\{', ''),
'i:\\d+;s:\\d+:\"(\\w\\w)\";',
'\\1,'
),
'\,?\}$',
''
)
Basically, this works with the input string (or column) str1 like
remove the first part
replace every cell with the string you want
remove the last 2 characters, ,}
and voila! You get a string CA,ID,OR.
Aftenote
It may or may not work well when the original array before serialised is empty (it depends how it is serialised).
Can someone please help me to better understand components, queries, etc. I'm struggling with a seemingly trivial task, I need one component with parametrized query. Instances of that component need to be included in a parent component,
for example I want lists of different kinds of fruits that need to be distributed among group of kids and each row would show kid's name and a quantity of fruits of one kind:
(defui FruitsLedger
static om/IQuery
(query [this]
'[(:data/fruits) {:kind ?kind}])
Object (render [this]
(let [{:keys [data/fruits]} (om/props this)]
(dom/ul nil (apply #(dom/li nil (str "for " (% :kid) " " (% :qt))))))))
now I need to have let's say two instances of this component in another component
where :params
for the 1st instance would be: {:kind :apples}
for the 2nd instance would be: {:kind :oranges}
this should render 2 lists similar to this:
| apples | oranges |
|---------------+---------------|
| for Aaron 2 | for Katie 1 |
| for Dan 1 | for Rob 3 |
| for Charles 0 | for Charles 3 |
| | |
|---------------+---------------|
I'm sure that parameterized queries have their uses. But perhaps this problem and other similar ones can be solved without resort to them.
Have :app/fruit-query-kinds in your db (default db format). It will be a list of fruits. You already have the fruit component, but you will need another component that has this idea of being a list of fruit kinds for querying purposes. Give this component a query and an ident. It doesn't matter if it is going to be displayed or not. Most important thing is getting the data structure right. Its ident will just be something like: '[:fruit-query-kind/by-id 1100], and it might consist of '[:fruit/by-id 10] and '[:fruit/by-id 20]. As per your example 10 will be the id for apples and 20 for oranges. That will be the refs value (a vector of idents in default db storage) of the key :app/fruit-query-kinds. 1100 is just a unique number that won't be expected to change.
The data structure is the important thing, and everything can be modelled in it, even query parameters.
You will need mutations and some buttons (or other input) that call transact! somewhere that directly change the fruit query kinds in your db. If you don't need any re-rendering to happen call transact! with the app's reconciler rather than with a component.
The idea is that you can then have component/s that query on the choices previously made by the user. So your first list component there won't know that it is to be for apples - all it knows is that it is to display fruits of the first fruit query kind.
I've found that you can do as much to-ing and fro-ing between the view and the db as you like, having the db not just store data but the current state of the view. So far the only time I got into trouble with this was when I mistakenly transacted on the not of the boolean key in the component's query. That of course ended up in a forever flickery screen.
In my opinion the way to work simply with Om Next on the Client is for all your reads to look exactly the same, all using db->tree. Once you accept this approach the next step is to get rid of them all together, which you can do by switching to the Untangled framework.
I am trying to make a menu calculator in which the user inputs items and the program will add up the order numbers and output the cost. I have done some of the code already but in the function in says can't assign to literal.
itemlist=["1","2","3","4","5","6","7","8","9"]
def itemcost():
1=3.50 #can't assign literal error is here
2=2.50
3=4.00
4=3.50
5=1.75
6=1.50
7=2.25
8=3.75
9=1.25
return itemcost
order=int(input("Enter order"))
while items in order:
itemcost+str(order)
First, some good information to put in a question is the language and platform you are using. Your error comment in the code IS helpful, however.
What your code is trying to do is assign the value 3.50 to the VALUE 1. You can't change the value of pure numbers for obvious reasons. What I think you want is:
itemlist["1"]=3.50
On line 4 (and lines 5-12 after it) the 1 is read as the value one, i.e. a literal value. If you want to assign the value 3.50 to a variable, you will need to name the variable something that cannot be interpreted as a number and does not begin with a number, such as _1 or var1.
What would be the best way to create an array that can have an index and a key at the same time ?
i mean something like this
index | key | value
0 | "myItem" | "Some value"
1 | "anotherItem" | "Some other value"
2 | "blabla" | "Bla Bla"
I know i can create a normal Array/Vector and then use an Object/Dictionary to map the keys to the index in the current array.
But if the array changes then the Dictionary needs to change all the indexes that would have been affected because an item has been removed for example.
I can go ahead and create a class that tries to synchronize the map with the array etc...
But i dont think it is the best way of doing it at all... :)
I wanna use it to have a list... that holds queued items for example.
You should be able to get a particular item by its key :
item = list["myItem"]
But you should also be able to find out the index of an item, they have to be ordened , and it should be possible to loop through it as a normal array.
What would be the best way to do something like this in as3 ?
You say you don't want to have a reverse index for the keys, so I don't see how you could achieve what you are after other than having a function that does a linear search in the array and finds an item given an id.
This assumes your items have a value but also an id: {value:"someValue, id="myItem"}.
A linear search is not a bad idea anyway, unless you have lots of items in your queue and retrieve them by id very often (specially in a tight loop).
Now, if you want to go all the way, you can extend Array functionality by extending the Proxy class to make index / id access transparent (that is, your code would get items with queue[0] or queue['myItem']). You'd still have to synchronize the items internally if you have a reverse index or you could just look them up dinamically (with a linear search).
Check out this answer for pointers on how to do this: extending AS3's Array access operators to 'wrap' out-of-bound index values