Output list with first match among two list - list

I have two lists in Tcl:
list1
{a/1 a/2 b/1 b/2}
list2
{a b}
I wanted to write a code which will check an element from list2 in list1 and give the first match and ignores the rest of the matches, basically below output list:
a/1 b/1

The only tricky bit in this is knowing exactly how the items in list 2 are to be searched for in list 1. I'm going to assume they're fairly simple strings and that we can just glob match for them (as if with string match). That's a case that lsearch supports by default, allowing us to use a pretty simple bit of code:
proc FirstMatches {haystackList needlesList} {
lmap needle $needlesList {
lsearch -inline $haystackList $needle*
}
}
puts [FirstMatches {a/1 a/2 b/1 b/2} {a b}]
The lmap just runs its body (as with foreach) for every needle in $needleList and collects the results of that into a list. It's perfect here.

Related

parse URL params in Perl

I am working on some tutorials to explain things like GET/POST's and need to parse the URI manually. The follow perl code works, but I am trying to do two things:
list each key/value
be able to look up one specific value
What I do NOT care about is replacing the special chars to spaces or anything, the one value I need to get should be a number. In other languages I have used, the regular expression in question should group each key/value into one grouping with a part 1/part 2, does Perl do the same? If so, how do I put that into a map?
my #paramList = split /(?:\?|&|;)([^=]+)=([^&|;]+)/, $ENV{'REQUEST_URI'};
if(#paramList)
{
print "<h1>The Params</h1><ul>";
foreach my $i (#paramList) {
if($i) {
print "<li>$i</li>";
}
}
print "<ul>";
}
Per the request, here is a basic example of the input:
REQUEST_URI = /cgi-bin/printenv_html.pl?customer_name=fdas&phone_number=fdsa&email_address=fads%40fd.com&taxi=van&extras=tip&pickup_time=2020-01-14T20%3A45&pickup_place=&dropoff_place=Airport&comments=
goal is the following where the left of the equal is the key, and the right is the value:
customer_name=fdas
phone_number=fdsa
email_address=fads%40fd.com
taxi=van
extras=tip
pickup_time=2020-01-14T20%3A45
pickup_place=
dropoff_place=Airport
comments=
How about feeding your list of key-value pairs into a hash?
my %paramList = $ENV{'REQUEST_URI'} =~ /(?:\?|&|;)([^=]+)=([^&|;]+)/g;
(no reason for the split as far as I can tell)
This relies crucially on there being an even-sized list of matches, where each "before-=" thing becomes a key in the hash, with the value being its pairing "after-=" thing.
In order to also get "pairs" without a value (like comments=) change + in the last pattern to *

Combining elements of a sublist of a list

I have the following list of sublists
[[1;5;10];
[2;6;11];
[3;7;12]];
I am trying to a create the following list of sublists:
[[1;2;3];
[5;6;7];
[10;11;12]]
The first sublist of the result should containt the first element of each original sublist, second result sublist should contian the second elements of each of the original sublists and so on.
Each sublist contains the same number of elements as the other sublists. The amount of sublists is at least 2.
I was thinking of using List.map but I am not sure what function to apply to each sublist to exctract the needed elements.
This is what I have so far:
let rec compute list =
List.map (fun x -> ) list
Any suggestions are appreciated!
Here you need two recursions (as you would need 2 imbricated loops in an imperative language).
The first recursion should allow you to go through the inputs line, say from 1 to 3, and at each step of this recursion, you will need a second recursion,to go along the full row.
You can either do it all by hand or you can use List.fold_left. (I would use fold for the inner recursion.

Python - iterate over two lists and find matches and position of mis-matches

I am working in Python 2.7
I am trying to iterate over 2 lists, of un-equal length, and I want to create a new list, containing the matching elements (same elements in the same position), and when the elements do not match, I need to have some text as well as the position of the miss-matching elements.
list1=[1,2,3,4]
list2=[1,2,3,5,6]
This outputs the matches
match=[[b] for a, b in zip(list1, list2) if a==b]
result:
[1,2,3]
But I do not know, in a one-liner, how to also flag the mis-matches:
[1,2,3,"nomatch-pos4"]
or
[1,2,3,"nomatch-pos4","nomatch-pos5"]
It does not matter if it will iterate over the maximum or minimum of the 2 list lengths.
it first find the minimum of the two lists and iterate over the shorter list and check if an element in the list matches with other list in same position. check below code:
match = [list1[i] if list1[i] == list2[i] else 'nomatch-pos'+str(i+1) for i in range(0,min(len(list1),len(list2)))]

How to print comma-separated list with hamlet?

With the hamlet templating language that comes with yesod, what is the best way of printing a comma-separated list?
E.g. assume this code which just prints one entry after another, how do I insert commas in between the elements? Or maybe even add an “and” before the last entry:
The values in the list are
$ forall entry <- list
#{entry}
and that is it.
Some templating languages such as Template Toolkit provide directives to detect the first or last iteration.
I don't think there's anything built-in like that. Fortunately, it's easy to use helper functions in Hamlet. For example, if your items are plain strings, you can just use Data.List.intercalate to add commas between them.
The values in the list are
#{intercalate ", " list}
and that is it.
If you want to do fancier things, you can write functions to work with Hamlet values. For example, here's a function which adds commas and "and" between the Hamlet values in a list.
commaify [x] = x
commaify [x, y] = [hamlet|^{x} and ^{y}|]
commaify (x:xs) = [hamlet|^{x}, ^{commaify xs}|]
This uses ^{...} syntax to insert one Hamlet value into another. Now, we can use this to write a comma-separated list of underlined words.
The values in the list are
^{commaify (map underline list)}
and that is it.
Here, underline is just a small helper function to produce something more interesting than plain text.
underline word = [hamlet|<u>#{word}|]
When rendered, this gives the following result.
The values in the list are <u>foo</u>, <u>bar</u> and <u>baz</u> and that is it.

Filtering list of tuples

New to Haskell and have a stumbling block. I'm trying to filter a list of tuples based on the first item.
filter (==(x,_)) lis
I get an illegal '_' error, but I'm not sure how I can get around it?
In Haskell, you cannot iterate over a tuple like you can a list.
If the tuple only has two items, you can use fst to retrieve the first item of the tuple and snd to retrieve the second item.
One way to do what I think you want to do is this approach:
Prelude> let lst = [(1,2), (3,4)]
Prelude> filter ((==1).fst) lst
[(1,2)]
Which only returns the items in the list where the first element is equal to 1; of course, you can substitute x where I put 1.
To be a little more specific, (==1).fst first applies fst to the element in lst, then applies (==1) to the result of fst -- technically, the dot composes the two functions together.
You can't give an argument with a wildcard _ in it to the == operator (or to any other function). The argument needs to be a real value, not a pattern that should be matched against.
If you want to use pattern matching you could use a lambda function as you filter condition:
filter (\(a,_) -> a == x) lis
Also, there is the predefined function fst to extract the first element of a two-element tuple. This can be combined with == to do the same test:
filter ((== x) . fst)) lis