Python output of split function - python-2.7

I am trying to understand syntax of
attributeMap[tuple[0]] = tuple[1]
from
I have Python code for detecting input parameter - How to do similar in Powershell
It doesn't look correct because the brackets are uneven, but the program is interpreted without error. On the other hand if I change it to
attributeMap[tuple[0] = tuple[1]]
I get the error
File "lookup.py", line 15
attributeMap[tuple[0] = tuple[1]]

The brackets are not "uneven" at all:
attributeMap[tuple[0]] = tuple[1]
We have three expressions here:
tuple[0] # first element of tuple
tuple[1] # second element of tuple
attributeMap[tuple[0]] # value in attributeMap which has the key matching first element of tuple
As you can see, the third expression makes use of the first, and at the end all we do is assign the second to the third. The brackets are in the right places.

Related

What is the RegEx for finding 3 different numbers in consecutive lines?

Let's say I have the following type of data:
[577] {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
[578] {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
[579] {0x05,0x08,0x01,0x00,0x47,0x00,0x61,0x00,0x6c}
[580] {0x05,0x08,0x01,0x00,0x47,0x00,0x61,0x00,0x6c}
[581] {0x05,0x08,0x01,0x00,0x47,0x00,0x61,0x00,0x6c}
[582] {0x04,0x08,0x00,0x00,0x61,0x00,0x78,0x00,0x79}
[583] {0x04,0x08,0x00,0x00,0x61,0x00,0x78,0x00,0x79}
[584] {0x04,0x08,0x00,0x00,0x61,0x00,0x78,0x00,0x79}
[585] {0x04,0x08,0x00,0x00,0x61,0x00,0x78,0x00,0x79}
[586] {0x04,0x08,0x00,0x00,0x61,0x00,0x78,0x00,0x79}
[587] {0x04,0x08,0x00,0x00,0x61,0x00,0x78,0x00,0x79}
[588] {0x03,0x08,0x00,0x00,0x20,0x00,0x53,0x00,0x32}
[589] {0x03,0x08,0x00,0x00,0x20,0x00,0x53,0x00,0x32}
[590] {0x03,0x08,0x00,0x00,0x20,0x00,0x53,0x00,0x32}
[591] {0x03,0x08,0x00,0x00,0x20,0x00,0x53,0x00,0x32}
[592] {0x02,0x08,0x00,0x00,0x32,0x00,0x2b,0x00,0x20}
[593] {0x02,0x08,0x00,0x00,0x32,0x00,0x2b,0x00,0x20}
[594] {0x02,0x08,0x00,0x00,0x32,0x00,0x2b,0x00,0x20}
[595] {0x02,0x08,0x00,0x00,0x32,0x00,0x2b,0x00,0x20}
[596] {0x02,0x08,0x00,0x00,0x32,0x00,0x2b,0x00,0x20}
[597] {0x02,0x08,0x00,0x00,0x32,0x00,0x2b,0x00,0x20}
[598] {0x01,0x08,0x00,0x00,0x2d,0x00,0x39,0x00,0x33}
[599] {0x00,0x08,0x00,0x00,0x34,0x00,0x00,0x00,0x00}
[600] {0x00,0x08,0x00,0x00,0x34,0x00,0x00,0x00,0x00}
[601] {0x00,0x08,0x00,0x00,0x34,0x00,0x00,0x00,0x00}
[602] {0x00,0x08,0x00,0x00,0x34,0x00,0x00,0x00,0x00}
The relevant data is between the braces { }.
I want to find where the first column doesn't repeat.
In the data above that would be for the row marked as "[598]".
Because row "[597]" start with a '0x02', and row "[599]" starts with a '0x00'. So the '0x01' is unique.
But it could very well be that the '0x01' is a '0x09'. I mean that the number per-se don't matter, as long as it's different from the lines above and below it. Only for the first column matters though.
I've been trying with Lookarounds but it doesn't work:
(?<!.*\{(\3).*\n)(.*\{(0x\d\d))(?!.*\n.*\{(\3))
Any ideas?
Notes:
I'm using VSCode to find.
No need to capture it, just would like it to highlight.
I think the following works for what you're after (slightly improved from Andrej, and adapted to support JavaScript's flavour regex, which I believe is what VSCode uses).
Regex101
^\[\d+\]\s+{([^,]+)[^[]+^\[\d+\]\s+{((?!\1)[^,]+)[^[]+^\[\d+\]\s+{((?!\2)[^,]+)[^[]+$
Notes:
JavaScript regex doesn't appear to support the (?!\1|\3) negative lookahead syntax, so I've swapped this for a single back reference 2-vs-1, and 3-vs-2
Due to this, if the first and third lines have the same value in the first element, then it'll still match, which isn't ideal...
Matches full lines and fields, if you need/want to use this for processing too
This is operating over three distinct lines:
^\[\d+\]\s+{([^,]+)[^[]+
matches against the numeric component surrounded by [] brackets, and the first element in the {} braces
^\[\d+\]\s+{((?!\1)[^,]+)[^[]+
matches the same again, but instead of "the first value", it explicitly forbids the value used on the first line
when compared with Andrej's answer, this will capture the full element due to ((?!\1)[^,]+) vs ((?!\1).{4})
^\[\d+\]\s+{((?!\2)[^,]+)[^[]+$
same again, but explicitly forbids the value used on the second line
Try (regex101):
^\[\d+\]\s+{([^,]+)[^{]+{((?!\1|\3).{4})[^{]+{((?!\1|\2).{4})

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 *

Split string and get last element

Let's say I have a column which has values like:
foo/bar
chunky/bacon/flavor
/baz/quz/qux/bax
I.e. a variable number of strings separated by /.
In another column I want to get the last element from each of these strings, after they have been split on /. So, that column would have:
bar
flavor
bax
I can't figure this out. I can split on / and get an array, and I can see the function INDEX to get a specific numbered indexed element from the array, but can't find a way to say "the last element" in this function.
Edit:
this one is simplier:
=REGEXEXTRACT(A1,"[^/]+$")
You could use this formula:
=REGEXEXTRACT(A1,"(?:.*/)(.*)$")
And also possible to use it as ArrayFormula:
=ARRAYFORMULA(REGEXEXTRACT(A1:A3,"(?:.*/)(.*)$"))
Here's some more info:
the RegExExtract function
Some good examples of syntax
my personal list of Regex Tricks
This formula will do the same:
=INDEX(SPLIT(A1,"/"),LEN(A1)-len(SUBSTITUTE(A1,"/","")))
But it takes A1 three times, which is not prefferable.
You could do this too
=index(SPLIT(A1, "/"), COLUMNS(SPLIT(A1, "/"))-1)
Also possible, perhaps best on a copy, with Find:
.+/
(Replace with blank) and Search using regular expressions ticked.
You can try use this!
You've got the array of String, so you can acess the last element by length
String message = "chunky/bacon/flavor";
String[] outSplited = message.split("/");
System.out.println(outSplited[outSplited.length -1]);

Simplest way to find out if at least one cell in a cell array matches a regular expression

I need to search a cell array and return a single boolean value indicating whether any cell matches a regular expression.
For example, suppose I want to find out if the cell array strs contains foo or -foo (case-insensitive). The regular expression I need to pass to regexpi is ^-?foo$.
Sample inputs:
strs={'a','b'} % result is 0
strs={'a','foo'} % result is 1
strs={'a','-FOO'} % result is 1
strs={'a','food'} % result is 0
I came up with the following solution based on How can I implement wildcard at ismember function of matlab? and Searching cell array with regex, but it seems like I should be able to simplify it:
~isempty(find(~cellfun('isempty', regexpi(strs, '^-?foo$'))))
The problem I have is that it looks rather cryptic for such a simple operation. Is there a simpler, more human-readable expression I can use to achieve the same result?
NOTE: The answer refers to the original regexp in the question: '-?foo'
You can avoid the find:
any(~cellfun('isempty', regexpi(strs, '-?foo')))
Another possibility: concatenate first all cells into a single string:
~isempty(regexpi([strs{:}], '-?foo'))
Note that you can remove the "-" sign in any of the above:
any(~cellfun('isempty', regexpi(strs, 'foo')))
~isempty(regexpi([strs{:}], 'foo'))
And that allows using strfind (with lower) instead of regexpi:
~isempty(strfind(lower([strs{:}]),'foo'))

decipher the regular expression

please help me decipher the regular expression-
'!_[$0]++'
It is being used to get a MSISDN (one at a time from a file containing list of MSISDN starting with zero )by the following usage:
awk '!_[$0]++' file.txt
It's not a regular expression, it's an arithmetic and boolean expression.
$0 = The current input line
_[$0] = An associative array element whose key is the input line
_[$0]++ = increment that array element each time we encounter a repeat of the line, but evaluates to the original value
!_[$0]++ = boolean inverse, so it returns true if the value was originally 0 or the empty string, false otherwise
So this expression is true the first time a line is encountered, false every other time. Since there's no action block after the expression, the default is to print the line if the expression is true, skip it when false.
So this prints the input file with duplicates omitted.
'true'- then the line will be printed
'_[$0]++'- associative array will be incremented everytime when $0 is present.means it will set the number of times each line is repeated.
'!_[$0]++'-this will be true when a line is inserted in the associative array for the firsttime only and the rest of the times it will resolve to false ultimately not printing the line.
So all the duplicate lines will not be prited.
This is not a regular expression. This particular command prints unique lines the first time they are found.
_ is being used as an array here and $0 refers to the entire line. Given that the default numeric value for array element is 0 (it's technically an empty string, but in numeric contexts its treated as 0), the first time you see a line, you print the line (since _[$0] is falsy, !_[$0] will be true). The command increments every time it sees a line (after printing -- awk's default command is to print), so the next time you see the line _[$0] will be 1 and the line will not be printed