Erlang - ** exception error: no match of right hand side value - json

I have a problem with pattern match of json formatted string.
Here I add a shorted version (just changed long json string to "{\"jsondata\"}"
So i have this pattern match which is sucessfull:
> MyData2={ok,{{"HTTP/1.1",200,"OK"},
[{"connection","Keep-Alive"},
{"date","Thu, 10 Sep 2015 12:03:49 GMT"},
{"server","Apache/2.4.7 (Ubuntu)"},
{"vary","X-Auth-Token"},
{"content-length","1171"},
{"content-type","application/json"},
{"x-openstack-request-id",
"req-31b4efc1-2af4-4130-b7a8-01d94b456096"},
{"keep-alive","timeout=5, max=100"}],
"{\"jsondata\"}"}}.
After that I run the following:
> {ok,{{"HTTP/1.1",ReturnCode, State},B,J}}=MyData2.
unfortunatelly i get
If I change "{\"jsondata\"}" to "jsondata" the last pattern match works fine
I have no Idea how to extract the json and get in J the "{\"jsondata\"}"
I`ll appriciate any idea
** exception error: no match of right hand side value

Your pattern matching operation works perfectly. I think the problem is, that one of the variables ReturnCode, State, B or J is already bound.
Lets assume the variable J is already bound to a value, and the other variables are not. Depending on this value, the pattern matching operation
{ok,{{"HTTP/1.1",ReturnCode, State},B,J}} = MyData2.
either succeeds or not.
Case 1:J is already bound to "{\"jsondata\"}"
Your pattern-match will succeed and the values of the unbound variables (ReturnCode, State and B) will be set, according to the pattern of MyData2.
Case 2:J is already bound to "{jsondata}"
The J-variable on the right hand side won't match the pattern of MyData2 on the left hand side. Thus the execution fails with an exception.
This also happens on the shell if you forget to clear your variables with f(Variable).

Related

Make beam/dataflow Create.of(List<String>) node emit exactly the number of elements in the list

My beam/dataflow job starts with a single static entry passed in like this:
Pipeline p = Pipeline.create(options);
PCollection<String> initValue = p.apply(Create.of("MyStringValue"));
However when I run it (on DataflowRunner), the Create node produced by that statement emits multiple values. The longer I wait, the more times it emits the single value:
This doesn't appear to be an artefact as later in the pipeline I get duplicate/triplicate/.. elements. Beam also logs a warning:
Can't verify serialized elements of type BoundedSource have well defined equals method. This may produce incorrect results on some PipelineRunner
How do I make my Create.of with one value emit just one value to the pipeline?
Do I need to attach an equals method or point it towards the equals method for String values (if so, how)!?

Apache Nifi: Replacing values in a column using Update Record Processor

I have a csv, which looks like this:
name,code,age
Himsara,9877,12
John,9437721,16
Razor,232,45
I have to replace the column code according to some regular expressions. My logic is shown in a Scala code below.
if(str.trim.length == 9 && str.startsWith("369")){"PROB"}
else if(str.trim.length < 8){"SHORT"}
else if(str.trim.startsWith("94")){"LOCAL"}
else{"INT"}
I used a UpdateRecord Processor to replace the data in the code column. I added a property called /code which contains the value.
${field.value:replaceFirst('^[0-9]{1,8}$','SHORT'):replaceFirst('[94]\w+','OFF_NET')}
This works when replacing code's with
length less than 8 with "SHORT"
starting with 94 with "LOCAL"
I am unable to find a way to replace data in the column, code when it's equal to 8 digits AND when it starts with 0. Also how can I replace the data if it doesn't fall into any condition mentioned above. (Situation which the data should be replaced with INT)
Hope you can suggest a workflow or value to be added to the property in Update record to make the above two replacements happen.
There is a length and startsWith functions.
${field.value:length():lt(8):ifElse(
'SHORT', ${field.value:startsWith(94):ifElse(
'LOCAL', ${field.value:length():equals(9):and(${field.value:startsWith(369)}):ifElse(
'PROB', 'INT'
)})})}
I have put the line breaks for easy to recognize the functions but it should be removed.
By the way, the INT means that some string values to replace? Sorry for the confusion.
Well, if you want to regular expression only, you can try the below code.
${field.value
:replaceFirst('[0-9]{1,8}', 'SHORT')
:replaceFirst('[94]\w+', 'OFF_NET')
:replaceFirst('369[0-9]{6}', 'PROB')
:replace(${field.value}, 'INT')
}

What can be used instead of Either in DAML?

when am creating choice using either function,whenever error has been occurs means then they returns values to the left side of the either,which is terminating the complete template itself that is not proceeding further for other scenario execution,how to do both same functionality in daml instead of Either.
If execution doesn't proceed further, then the error is not handled. Moreover, a Left can't be simply ignored. Consider this DAML function:
steps : Bool -> Either Int Bool
steps q = do
a <- if q then Left 1 else Right "foobar"
return $ a == "foobar"
a is a Text, which is only present if the Either is Right. So if the Either is Left, execution cannot proceed to the last line, because there is nothing to assign to a.
It wouldn't do to change this behavior just because you might get an Either Text Text. So in this case, too, the variable will only be bound if it's Right.
It also wouldn't do to change behavior just because you removed the variable. For example,
steps2 : Bool -> Either Int Bool
steps2 q = do
if q then Left 1 else Right "foobar"
return q
If the semantics suddenly "just kept going" because you eliminated an unused variable binding, that would be incredibly inconsistent and confusing. So it stops right there on Left, just as if a <- was still there.
The thing is, this is not just about Either; this holds for all "error-reporting-style" actions, because they are all characterized by "I don't have a value to bind to your variable", so execution can never proceed further in any do, even if you use an "alternative" to Either.
You must handle the error right there if you want execution to proceed; in other words, if you have a Left, you have to come up with a Right if you want it to keep going, and that has equivalence in any action that features error-reporting, because they all have missing a values that you must come up with. Indeed, the meaning of error for all is "I can't come up with a value for your a <- or whatever else you're doing".

How to remove string that is no longer needed in MySQL database?

This seemed like it should be very simple to do yet I've not been able to find an answer after weeks of looking.
I'm trying to remove strings that are no longer needed. Regex_replace sounds perfect but is not available in MySQL.
In MySQL how would I accomplish changing this:
[quote=ABC;xxxxxx]
to this:
[quote=ABC]
The issues are:
- this can appear anywhere in a text blob
- the xxxxxx can only be numeric but may be 6, 7 or 8 characters long
- not adding/removing any rows, just rewriting the contents of one column on one row at a time.
Thanks.
I don't think you really need REGEX_Replace (though it would make things easier of course).
Assuming that the example you presented is a real reflection of what you have:
Your starting point is with the string [quote=<something>;, meaning that you can start searching for [quote=,
Once you found it, you need to search for ; and after that for ],
Once you found them both, you know what to extract when where to start for the next search (if the pattern you mentioned can appear more than once within a singe blob.
Did I get you correctly?
EDIT
This paradigm is aimed to convert all instances of [quote=ABC;xxxxxx] to [quote=ABC] under the following assumptions:
The pattern can appear any number of times within the input string,
The length of xxxxxx is not fixed,
The resulting string (after removing all the appearances of ;xxxxxx) should replace the value in the table,
Performance is not an issue since either this is going to be a one-time job (through the whole table) or it will run every time on a single string (e.g. before INSERTing a new record).
Some MySQL functions that will be used:
INSTR: Searches within a string for the first appearance of a sub-string and returns the position (offset) where the sub-string was found,
SUBSTR: Returns a substring from a string (several ways to use it),
CONCAT: Concatenates two or more strings.
The guidelines presented here apply for the manipulation of a single INPUT string. If this needs to be used over, say, a whole table, simply get the strings into a CURSOR and loop.
Here are the steps:
Declare five INT local variables to serve as indices and total input string length, say L_Start, L_UpTo, l_Total_Length, l_temp1 and l_temp 2, setting the initial value for l_Start = 1 and l_Total_Length = LENGTH(INPUT_String),
Declare a string variable into which you will copy the "cleaned" result and initiate it as '', say l_Output_str; also declare a temporary string to hold the value of 'ABD', say l_Quote,
Start a infinite loop (you will set the exit condition within it; see below),
Exit loop if l_Start >= l_Total_Length (here is one of the two exit points from the loop),
Find the first location of '[quote=' within the input string starting from L_Start,
If the returned value is 0 (i.e. substring not found), concatenate the current contents of l_Output_str with whatever remains if the input string from position L_start (e.g. SET l_Output_str = CONCAT(l_Output_str,SUBSTR(INPUT_String,L_Start) ;) and exit loop (second exit position),
Search the input string for the ; symbol starting from L_start + 7 (i.e. the length of [quote=) and save the value in l_temp_1,
Search the input string for the ] symbol starting from L_start + 7 + l_temp2 and save the value in l_temp_2,
Add the found result to output string as SET l_Output_str = CONCAT(l_Output_str,'[quote=',SUBSTR(INPUT_String,L_Start + 7, l_temp_2 - l_temp_1),']') ;,
Set L_Start = L_Start + 7 + l_temp_2 + 1 ;
End of loop.
Notes:
As I neither made the code nor tested it, it is possible that I'm not setting indices correctly; you will need to perform detailed tests to make get it working as needed;
The above IS the method I suggested;
If the input string is very long (many MBs), you might observe poor performance (i.e. it might take few seconds to complete) because of the concatenations. There are some steps that can be taken to improve performance, but let's have this working first and then, if needed, tackle the performance issues.
Hope that the above is clear and comprehensive.

Can I read the rest of the line after a positive value of IOSTAT?

I have a file with 13 columns and 41 lines consisting of the coefficients for the Joback Method for 41 different groups. Some of the values are non-existing, though, and the table lists them as "X". I saved the table as a .csv and in my code read the file to an array. An excerpt of two lines from the .csv (the second one contains non-exisiting coefficients) looks like this:
48.84,11.74,0.0169,0.0074,9.0,123.34,163.16,453.0,1124.0,-31.1,0.227,-0.00032,0.000000146
X,74.6,0.0255,-0.0099,X,23.61,X,797.0,X,X,X,X,X
What I've tried doing was to read and define an array to hold each IOSTAT value so I can know if an "X" was read (that is, IOSTAT would be positive):
DO I = 1, 41
(READ(25,*,IOSTAT=ReadStatus(I,J)) JobackCoeff, J = 1, 13)
END DO
The problem, I've found, is that if the first value of the line to be read is "X", producing a positive value of ReadStatus, then the rest of the values of those line are not read correctly.
My intent was to use the ReadStatus array to produce an error message if JobackCoeff(I,J) caused a read error, therefore pinpointing the "X"s.
Can I force the program to keep reading a line after there is a reading error? Or is there a better way of doing this?
As soon as an error occurs during the input execution then processing of the input list terminates. Further, all variables specified in the input list become undefined. The short answer to your first question is: no, there is no way to keep reading a line after a reading error.
We come, then, to the usual answer when more complicated input processing is required: read the line into a character variable and process that. I won't write complete code for you (mostly because it isn't clear exactly what is required), but when you have a character variable you may find the index intrinsic useful. With this you can locate Xs (with repeated calls on substrings to find all of them on a line).
Alternatively, if you provide an explicit format (rather than relying on list-directed (fmt=*) input) you may be able to do something with non-advancing input (advance='no' in the read statement). However, as soon as an error condition comes about then the position of the file becomes indeterminate: you'll also have to handle this. It's probably much simpler to process the line-as-a-character-variable.
An outline of the concept (without declarations, robustness) is given below.
read(iunit, '(A)') line
idx = 1
do i=1, 13
read(line(idx:), *, iostat=iostat) x(i)
if (iostat.gt.0) then
print '("Column ",I0," has an X")', i
x(i) = -HUGE(0.) ! Recall x(i) was left undefined
end if
idx = idx + INDEX(line(idx:), ',')
end do
An alternative, long used by many many Fortran programmers, and programmers in other languages, would be to use an editor of some sort (I like sed) and modify the file by changing all the Xs to NANs. Your compiler has to provide support for IEEE NaNs for this to work (most of the current crop in widespread use do) and they will correctly interpret NAN in the input file to a real number with value NaN.
This approach has the benefit, compared with the already accepted (and perfectly good) answer, of not requiring clever programming in Fortran to parse input lines containing mixed entries. Use an editor for string processing, use Fortran for reading numbers.