List comparision in perl - regex

The command "sh value" gives
A: optimal
size: 100
feature : ON
Minimum size: 0
CPU load: 100%
Done
The name-value pairs written above are parameters wit default values.
I want to compare the values corresponding to each parameter with the output of the command sh value each time its fired and verify if they match correctly. If the values don't match I need to mark the parameter for which the match doesn't happen.
How to do it ? Also, is it possible to iterate over the list with match results displayed as iterations?

I'm not going to assume the order is constant (i.e. the following code will work whether it is or not). I am going to assume the order of the fields isn't significant.
For each line of the previous output,
split the line on the first :.
Create an element in %prev_values, where the key and value are the results of split.
For each line of the current output,
split the line on the first :.
Create an element in %cur_values, where the key and value are the results of split.
For each key in %prev_values,
If the key doesn't exist in %cur_values,
This key was deleted. Print an appropriate message.
For each key in %cur_values,
If the key doesn't exists in %prev_values,
This key is a new key. Print an appropriate message.
Else
If the value of that key in %cur_values different than the one in %prev_values,
The value of this key changed. Print an appropriate message.

Related

How to get the first occurrence of value with the matching number from multiple sets

I have request comes with multiple elements where I need the first occurrence of the where data_type="3". Hence there could be multiple values comes as 0,2,3,4 in random.
When I tried to put the below Xpath function it's returning the all values where data_type='3'
<xsl:value-of select="/process/TransactionType/data_xml/transaction/sub_documents/transactionLine[#data_type='3']/Ref"/>
Full input and output code click here code snippet
How I can get the one value instead all values.
Please help me out here.
Well, with XPath if exp gives you a sequence of values and you want the first use e.g. (exp)[1] i.e. (/process/TransactionType/data_xml/transaction/sub_documents/transactionLine[#data_type='3']/Ref)[1].

Airtable If-statement outputting NaN

I'm using an If-statement to assign integers to strings from another cell. This seems to be working, but if I reference these columns, I'm getting a NaN value. This is my formula below. I tried adding INT() around the output values, but that seemed to break everything. Am I missing something?
IF(FIND('1',{Functional response}),-4,
IF(FIND('2',{Functional response}),-2,
IF(FIND('3',{Functional response}),0,
IF(FIND('4',{Functional response}),2,
IF(FIND('5',{Functional response}),4,"")))))
Assuming Functional response can only store a number 1 to 5 as a string a simple option in excel would be to first convert the string to a number and then use the choose function to assign a value. this works as the numbers are are sequential integers. Assuming Cell K2 has the value of Functional response, your formula could be:
=CHOOSE(--K2,-4,-2,0,2,4)
=CHOOSE(K2+0,-4,-2,0,2,4)
=CHOOSE(K2-0,-4,-2,0,2,4)
=CHOOSE(K2*1,-4,-2,0,2,4)
=CHOOSE(K2/1,-4,-2,0,2,4)
Basically sending the string of a pure number through a math operation has excel convert it to a number. By sending it through a math operation that does not change its value, you get the string as a number.
CHOOSE is like a sequential IF function Supply it with an integer as the first argument and then it will return the value from the subsequent list that matches the number. if the number you supply is greater than the number of options you will get an error.
Alternatively you could just do a straight math convertion on the number stored as a string in K2 using the following formula:
=(K2-3)*2
And as my final option, you could build a table and use VLOOKUP or INDEX/MATCH.
NOTE: If B2:B6 was stored as strings instead of numbers, K2 instead of --K2 would need to be used.

Kotlin getValue for String containing word/s ignoring case Upper or Lower and spaceing

I have generated a list from file.
The list contains a list that looks this below
(redTomatoes, 47.50)
(greenapple, 23.75)
(yellowBananas, 17)
and so forth
var value = versionsList.getValue("banana")
println("This is the value $value")
The above code won't retrun the value because it doesn't match exactly. I can't add ignoreCase or contains with .getValue
Is it possible to search for a String containing a word while ignoring case, spacing?
Do I need to create some sort of loop to test all possible combinations to get value?
Thanks
Is it possible to search for a String containing a word while ignoring case, spacing?
It is possible, but it is not something that can be done without iterating over all elements.
Do I need to create some sort of loop to test all possible combinations to get value?
So yes, a loop of some sort is needed, but testing all possible combinations is a bit extreme. A preferable way would have been to use .toLowerCase() on any String before putting it into the map, even tough it would not entirely remove the ambiguity of the format of the keys. But even without such altering of the input, it can be solved.
A Proposed Solution
Given the input such as
val fruits: Map<String, Double> = mapOf(
"redTomatoes" to 47.50,
"greenapple" to 23.75,
"yellowBananas" to 17.0
)
and the assumption that the data cannot be modified before inserting to the map, I propose that we convert them into a more common format when retrieving a value from the map. Since the irregularities is capital letters and spaces between words, we need to transform all keys and inputs to a common format without spaces and in lower case. Transforming a String can conveniently be written as an extension function.
fun String.toLowerCaseWithoutWhiteSpace() = this.toLowerCase().replace(Regex("\\s+"), "")
But as we cannot use .getValue() on our map for this, we need to write a custom function for that.
fun <T> Map<String, T>.getLike(key: String): T?
{
val transformed = key.toLowerCaseWithoutWhiteSpace()
return this
.asSequence()
// Change the keys in the map so they are also lower case and without whitespace
.map { it.key.toLowerCaseWithoutWhiteSpace() to it.value}
// Take the first entry where the key matches
.firstOrNull { (keyInMap, _) ->
keyInMap == transformed
}
// Return the value from the entry
?.second
}
And we can then use the following to access the right values
fruits.getLike("yellow bananas")
fruits.getLike("green apple")
fruits.getLike("red tomatoes")
This does of course come with a performance penalty, accessing the right value will have a time complexity of O(N) instead of O(1). This might be okay for a smaller map, but if there will be a lot of elements in your map, you might consider processing the elements before inserting them in the map, or if it is not possible, creating a new map were all keys have been converted as I have done above.

Returning an index of an alphabetical list python

So I'm working on a homework for a beginning python class. I'm asked to write a function taking two parameters, one a string and one of a list in alphabetical order. The function is to return the integer index as to where the string would be placed in order for the list to stay alphabetized.
I am not allowed to insert anything into the list, or append the list in any matter (I tried just adding the string to the list, resorting and then returning the index for where the string now lived) All the function is to return is the integer index value. I could use some direction as to where to start without using an insert and resorting... Thanks.
Because I dont want to write your homework for you, here is one way to do it in pseudo-code:
def insert_index(string, list)
for every item in your list:
if the item is greater than your string:
return index of item
else:
go to next item
weirdly enough, because of the way python is written, this is very close to actual code...
because strings are comparable, you can actually do something like 'a'<'b' and return a valid bool. and since your list is alphabetical, as soon as you hit an item that is greater than your string, then you know thats where you want your string to go.
also, it would be useful to use enumerate function for your loop structure (HINT HINT)
I would iterate over the list and compare the current string in the list with the string you are trying to insert ( < > comparators work on strings, with 'b' > 'a'. Without giving too much away, take advantage of the fact that the list you are given is already in alphabetical order to determine which index the passed in string would be placed in.
One cool thing in python is that letters are of a higher value if they are farther along in the alphabet. For example, 'b' > 'a'. In fact, you can type 'b' > 'a' into an interpreter and it will tell you 'true'. So simply loop through the alphabetical list comparing the first letter in the list item to the first letter in the string.
Something like this (I haven't checked it, so it may not work perfectly, but try to get the gist of it)
for i in range(0,len(list)):
if (list[i][0] < str[0]):
print(i)
break

Perl: Looping Through an Array to Increment the Values of a Hash

I am new to perl and I have a problem that I'm trying to solve. At this stage in my program, I have placed a file into an array and created a hash where all the keys are numbers, that increase by a user specified bin size, within a range The values of all keys are set to 0. My goal is to loop through the array and find numbers that match the keys of my hash, and increment the corresponding value by 1 in the event of a match. To make finding the specific value within the array a bit easier, each line of the array will only contain one number of interest, and this number will ALWAYS be a decimal, so maybe I can use the regex:
=~ m{(\d+\.\d+)}
to pick out the numbers of interest. After finding the number of interest, I need to round down the number (at the minute I an using "Math::Round 'nlowmult';") so that it can drop into the appropriate bin (if it exists), and if the bin does not exist, the loop needs to continue until all lines of the array have been scanned.
So the overall aim is to have a hash which has a record of the number of times that values in this array appear, within a user specified range and increment (bin size).
At the minute my code attempting this is (MathRound has been called earlier in the program):
my $msline;
foreach $msline (#msfile) {
chomp $msline;
my ($name, $pnum, $m2c, $charge, $missed, $sequence) = split (" ", $msline);
if ($m2c =~ /[$lowerbound,$upperbound]/) {
nlowmult ($binsize, $m2c);
$hash{$m2c}++;
}
}
NOTE: each line of the array contains 6 fields, with the number of interest always appearing in the third field "m2c".
The program isn't rounding the values down, neither is it adding values to the keys, it is making new keys and incrementing these. I also don't think using split is a good idea, since a real array will contain around 40,000 lines. This may make the hash population process really slow.
Where am I going wrong? Can anybody give me any tips as to how I can go about solving this problem? If any aspects of the problem needs explaining further, let me know!
Thank you in advance!
Change:
if ($m2c =~ /[$lowerbound,$upperbound]/) {
nlowmult ($binsize, $m2c);
$hash{$m2c}++;
}
to:
if ($m2c >= $lowerbound && $m2c <= $upperbound) {
$m2c = nlowmult ($binsize, $m2c);
$hash{$m2c}++;
}
You can't use a regular expression like that to test numeric ranges. And you're using the original value of $m2c as the hash key, not the rounded value.
I think the main problem is your line:
nlowmult ($binsize, $m2c);
Changing this line to:
$m2c = nlowmult ($binsize, $m2c);
would solve at least that problem, because nlowmult() doesn't actually modify $m2c. It just returns the rounded result. You need to tell perl to store that result back into $m2c.
You could combine that line and the one below it if you don't want to actually modify the contents of $m2c:
$hash{nlowmult ($binsize, $m2c)}++;
Probably not a compete answer, but I hope that helps.