pattern-matching

How do I replace a substring by the output of a shell command with sed, awk or such?

廉价感情. 提交于 2019-12-06 11:53:50
I'd like to use sed or any command line tool to replace parts of lines by the output of shell commands. For example: Replace linux epochs by human-readable timestamps, by calling date Replace hexa dumps of a specific protocol packets by their decoded counterparts, by calling an in-house decoder sed seems best fitted because it allows to match patterns and reformat other things too, like moving bits of matches around, but is not mandatory. Here is a simplified example: echo "timestamp = 1234567890" | sed "s/timestamp = \(.*\)/timestamp = $(date -u --d @\1 "+%Y-%m-%d %T")/g" Of course, the $(...

switch statement for an std::pair?

寵の児 提交于 2019-12-06 11:45:55
I want to switch over possible values of two integers, or in another case two bools. For the sake of discussion, suppose I've done auto mypair = std::make_pair(foo, bar); How can I achieve the equivalent of switch(mypair) { case make_pair(true, false): cout << "true and false"; break; case make_pair(false, true) cout << "false and true"; break; case default: cout << "something else"; } with C++11? (C++14/17 also relevant if that helps)? C++'s switch statement doesn't have the pattern matching power of many other languages. You'll need to take a slightly different approach. Here's a possibility

Is is possible to pattern match on the underlying shape of a discriminated union?

末鹿安然 提交于 2019-12-06 11:17:13
问题 Does F# support pattern matching of a discriminated union member instance by criteria other than the Identifier pattern? For example, imagine that I want to match on the underlying shape of the data and I want to consider anything with an int * int shape, regardless of how the DU classifies the value. Is Here's how I'd do it now: type ExampleDU = | BinaryCase1 of x:int * y:int | BinaryCase2 of x:int * y:int | UnaryCase1 of x:int let underlyingValue = (1,2) let asCase1 = BinaryCase1

image matching/detection in iphone using opencv

删除回忆录丶 提交于 2019-12-06 10:38:10
I have 2 images, say bigImage and smallImage. I want to detect whether the smallImage is there anywhere in the bigImage, irrespective of its orientation or transforms(rotations). If its there, it should return true and otherwise return false. I have been going through the template matching (cvMatchTemplate) method in openCV, but haven't reach anywhere since there aren't much difference between the output for a true and a false match. Is my requirement still possible using cvMatchTemplate or are there any other methods in openCV for getting to this. This is a very general and very hard problem,

Finding the start and stop indices in sequence in R

旧时模样 提交于 2019-12-06 09:52:44
Suppose I have the sequence: x = c( 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0) Is there an elegant way in R to return the start and stop indices of each sequence of 1s? The answer should be a 2 column array with nRows = number of sequences of 1s: startIndx = [ 1, 5, 7 ] stopIndex = [ 2, 5, 9 ] Thanks. BSL Assuming your vector consists of 0 and 1 values: which(diff(c(0L, x)) == 1L) #[1] 1 5 7 which(diff(c(x, 0L)) == -1L) #[1] 2 5 9 Otherwise you'd need something like x <- x == 1L first. Elegant way is y <- which(x==1) startIndx <- y[!(y-1) %in% y] stopIndex <- y[!(y+1) %in% y] rbind(startIndx, stopIndex)

jsonb LIKE query on nested objects in an array

感情迁移 提交于 2019-12-06 09:36:04
My JSON data looks like this: [{ "id": 1, "payload": { "location": "NY", "details": [{ "name": "cafe", "cuisine": "mexican" }, { "name": "foody", "cuisine": "italian" } ] } }, { "id": 2, "payload": { "location": "NY", "details": [{ "name": "mbar", "cuisine": "mexican" }, { "name": "fdy", "cuisine": "italian" } ] } }] given a text "foo" I want to return all the tuples that have this substring. But I cannot figure out how to write the query for the same. I followed this related answer but cannot figure out how to do LIKE . This is what I have working right now: SELECT r.res->>'name' AS feature

In scala, how can I use pattern match to match a list with specified length?

别等时光非礼了梦想. 提交于 2019-12-06 09:21:11
My codes looks like this: 1::2::Nil match { case 1::ts::Nil => "Starts with 1. More than one element" case 1::Nil => "Starts with 1. Only one element" } I tried to use 1::ts::Nil to match the List who starts with 1 and whose length is greater than 1. It workes well for 2-element list, however, this pattern doesn't work for 3-element list , for example: 1::2::3::Nil match { case 1::ts::Nil => "Starts with 1. More than one element" case 1::Nil => "Starts with 1. Only one element" } This won't work..Does anyone have ideas about this? You don't have to match on Nil. What you could do instead is

Pattern matching for strings independent from symbols

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 08:59:40
问题 I have need for an algorithm which can find pre-defined patterns in data (which is present in the form of strings) independent from the actual symbols/characters of the data and the pattern. I only care about the relations between the symbols, not the symbols themselves. It is also legal to have different pattern symbols for the same symbol in the data. The only thing the pattern matching algorithm has to enforce is that multiple occurences of the same symbol in the pattern are preserved. To

Flattening nested lists of the same type

∥☆過路亽.° 提交于 2019-12-06 08:42:43
问题 Let's say I want to flatten nested lists of the same type... For example ListA(Element(A), Element(B), ListA(Element(C), Element(D)), ListB(Element(E),Element(F))) ListA contains nested list of the same type ( ListA(Element(C), Element(D)) ) so I want to substitute it with the values it contains, so the result of the upper example should look like this: ListA(Element(A), Element(B), Element(C), Element(D), ListB(Element(E),Element(F))) Current class hierarchy: abstract class SpecialList()

PostgreSQL, find strings differ by n characters

岁酱吖の 提交于 2019-12-06 08:41:39
Suppose I have a table like this id data 1 0001 2 1000 3 2010 4 0120 5 0020 6 0002 sql fiddle demo id is primary key, data is fixed length string where characters could be 0, 1, 2. Is there a way to build an index so I could quickly find strings which are differ by n characters from given string? like for string 0001 and n = 1 I want to get row 6. Thanks. There is the levenshtein() function, provided by the additional module fuzzystrmatch . It does exactly what you are asking for: SELECT * FROM a WHERE levenshtein(data, '1110') = 1; SQL Fiddle. But it is not very fast. Slow with big tables,