I get this error from this line can anyone tell me who can resolve this problem.
function format_numeric($str) {
if(empty($str) && $str!=0) return;
global $appearance_settings;
$decimals = $appearance_settings['number_format_decimals'];
$point = $appearance_settings['number_format_point'];
$th_separator = $appearance_settings['number_format_separator'];
$result = number_format($str, $decimals, $point, $th_separator); //THIS IS THE LINE WITH THE ERROR
return $result;
}
Judging by your variable names you're passing a string into this function, but as the message says, number_format() requires a double.
You can force the issue by adding
$str = floatval($str);
as the first line of your function.
This assumes that your $str variable contains something that can be coerced to a double. If it doesn't you might see other errors.
Related
Answer: it has to do with GString type and "lazy evaluation."
See http://docs.groovy-lang.org/latest/html/documentation/index.html#_string_interpolation for the formal documentation.
See https://blog.mrhaki.com/2009/08/groovy-goodness-string-strings-strings.html for someone's write-up on this.
Firm solution in the code below as commenter said is to explicitly cast it on creation using String targ = "${TARGET_DATA}"
I'm seeing what seems on the surface to be a delayed string interpolation or something in Groovy. I've figured out workarounds for my immediate needs, but the behaviour is a real gotcha, and a potential source for serious bugs...
I strongly suspect it arises from Groovy being a meta-language for Java, and some objects not using the usual string-matching routines.
This was discovered when we were trying to use a string interpolation on some parameter in Jenkins, and checking it against a list of pre-approved values - hence the resulting example below.
Consider this code:
TARGET_DATA= "hello"
data = ["hello"]
targ = "${TARGET_DATA}"
// Case 1: Check for basic interpolated string
if( data.contains(targ) ) {
println "Contained interpolated string"
} else {
println "Interpolation failed"
}
// Case 2: Check to see if using something that actively forces its interpolation changes the variable
println "interpolating targ = ${targ}"
if( data.contains(targ) ) {
println "Contained re-interpolated string"
} else {
println "re-Interpolation failed"
}
// Case 3: Use direct variable assignment
targ = TARGET_DATA
if( data.contains(targ) ) {
println "Contained assigned variable"
} else {
println "Assignment failed"
}
Its output is this:
Interpolation failed
interpolating targ = message: hello
re-Interpolation failed
Contained assigned variable
This indicates that:
In case 1 , the placeholder string is checked for in the list, and fails, as it hasn't been interpolated
In case 2, after forcing the interpreter to perform an interpolation against targ, the content of that variable isn't updated. At this stage, targ still contains a literal placeholder string
In case 3, after assigning the initial variable directly to the target variable, we get a successful match
My guess is that targ literally contains a string starting with a dollar sign, curly brace, and a variable name, etc. This only resolves under certain conditions, like the use of a println , but not in the case of a <list>.contains() which just gets the uninterpolated variable as-is, and does not know during check, to interpolate it.
Using targ = new String("${TARGET_DATA}") does actively interpolate the string however, as the call to function somehow registers as something active.
However this code does interpolate correctly:
TARGET_DATA= "hello"
targ = "${TARGET_DATA}"
def eq(var1) { return var1 == "hello" }
basic_check = eq(targ)
println "${basic_check}" // resolves as true
which means that at some point, the string is interpolated - possibly the == operation has been reimplemented by Groovy to call String's equality function always:
Such that, Groovy re-implemented the String object - and its equality check - but the <list>.contains() function doesn't use that comparator (or it is not caught during script interpretation, due to being compiled code in the Java standard library) and so fails to trigger the interpolation substitution...
Can someone shed some light here please?
targ is of type Gstring, rather than a java String. GString retains the information for how to build itself from the interpolated form.
Because targ isn't a String, it will never pass the equality check required by List.contains, where the List contrains a String.
I'm using Sphinx::Search.
Is there is a easier way for this code example to convert a string to a constant?
use Sphinx::Search;
my $config = {
x => 'SPH_MATCH_EXTENDED2',
};
my $x = $config->{x};
print Sphinx::Search->$x(); # output: 6
I have used advice from
How do I access a constant in Perl whose name is contained in a variable?
and this example works, but if I am always using a string from a hash then do I need to put it into a separate variable to use it in this way?
my $x = $config->{x};
print Sphinx::Search->$x(); # output: 6
Is there a one- liner for this?
# does not work
print Sphinx::Search->$config->{x}();
You can create a reference to the value and immediately dereference it:
Sphinx::Search->${ \$config->{x} };
(If there are no arguments, the () is optional).
I'm guessing that SPH_MATCH_EXTENDED2 is the name of a constant that is exported by Sphinx::Search. The problem is that these are implemented as a subroutine with no parameters, so you may use them only where a bare subroutine name will be understood by Perl as a call, or where an explicit call is valid ( SPH_MATCH_EXTENDED2() )
The easiest solution is to avoid quoting the hash value at all, like so
my $config = { x => SPH_MATCH_EXTENDED2 }
and afterwards, you may use just
$config->{x}; # 6
instead of calling a pseudo class method
I wrote a program which processes a string using functional programming style. It gives an odd "type mismatch;
found : String
required: Int" error on this line else if(c==('s')) helper1(s(str), s.drop(1)). Thanks in advance.
def stringpipeline(string:String) : (String) => String = {
def r(s:String):String = {s.reverse}
def s(s:String) = {s.sorted}
def U(s:String) = {s.toUpperCase}
def l(s:String) = {s.toLowerCase}
def star(s:String):String = {s.trim}
def T(s:String):String = {s.split(' ').map(_.capitalize).mkString(" ")}
def helper1(str:String, s:String): String = {
if (s.length != 0)
{
val c = s(0)
if(c==('T')) helper1(T(str), s.drop(1))
if(c==('s')) helper1(s(str), s.drop(1))
if(c==('*')) helper1(star(str),s.drop(1))
else str
}
else str
}
def helper2(strn:String): String = {helper1(strn,string)}
helper2 _
}
helper1(s(str), s.drop(1))
In code s(str) you're calling String.apply(Int) method. str is String, so compiler notifies about it
The problem is that you have declared s in two different scopes. In the overall scope you have declared s as a method which acts on a String and returns a String. In the scope of helper1() you have also declared s as a String parameter. This declaration overrides (shadows) the declaration of the method s outside helper1().
Where the error is reported, you are trying to use the method s(str), but the compiler is picking up the declaration s:String. You should fix this by renaming the name of either the method or the parameter. In general I would suggest avoiding single character names for methods, parameters or variables and instead using longer and more descriptive names - this is not a requirement to fix this problem, but you would have been more likely to avoid it by using, for example, sorted().
I'm trying to remove stop words from a text in MarkLogic 8 using this function :
declare function rec:remove-stop-words($string, $stop_words) {
(: This is a recursive function. :)
if(not(empty($stop_words))) then
rec:remove-stop-words(
replace($string, $stop_words[1], '', 'i'),
(: This passes along the stop words after
the one just evaluated. :)
$stop_words[position() > 1]
)
else normalize-space($string)
};
Here where I call it
for $r in /rec:Record
return
rec:remove-stop-words(data($r/rec:Abstract), $stop_words}
It gives me the following error
XDMP-ARGTYPE: (err:XPTY0004) fn:replace((xs:untypedAtomic("
chapter
utilized asymmetry of n..."), xs:untypedAtomic("
book
interrelationship between ...")), "a", "", "i") -- arg1 is not of type xs:string?
The function expects a string type but the actual type is untypedAtomic. I don't know what to do!
NOTE: (( The problem is not in the function because I've tried to use it for a different text and it worked well )).
I tried to the code by converting untypedAtomic to string by:
return
<info>{rec:remove-stop-words(data(xs:string($r/rec:Abstract)), $stop_words)}</info>
but I got this error:
XDMP-ARGTYPE: (err:XPTY0004) fn:replace(("
chapter
utilized asymmetry of n...", "
book
interrelationship between ..."), "a", "", "i") -- arg1 is not of type xs:string
The problem is that when you iterate over /rec:Record and pass $r/rec:Abstract as input, at least one of your records is returning more than one rec:Abstract. The function signature for rec:remove-stop-words allows a sequence of values as input for $string, but the function body where you call fn:replace only handles input for a single value, so it throws an argument exception (given xs:string+ and expecting xs:string?).
You can handle the sequence by iterating over rec:Abstract before you call the function:
for $r in /rec:Record
for $a in $r/rec:Abstract
return
rec:remove-stop-words($a, $stop_words)
If you use stricter function signatures, it can help avoid problems like this, or at least make them easier to debug. For example, if you define your function to only allow a single input for the first parameter:
rec:remove-stop-words($string as xs:string, $stop_words as xs:string*)
...
This will throw a similar exception when $string is passed a sequence, but higher up the call stack, which can help make these types of errors a little more obvious.
Try using this code -
for $r in /rec:Record
return
rec:remove-stop-words(fn:string($r/rec:Abstract), $stop_words}
It looks like you are sending it a node and not a string. Try $r/rec:Abstract/text() or $r/rec:Abstract/string()
I've got a Lua script that receives a function call in a string. I need to execute that call and retrieve the output as a string in a variable so I can later send it somewhere.
For example, I will receive the string "json.encode('{1:1, 2:3, 5:8}')". I'd like to execute it and get a variable with the value ret = json.encode('{1:1, 2:3, 5:8}').
I've tried using loadstring in a bunch of different ways, including a way I found in the docs, but I can't get it to work as I want:
> s = "json.encode('{1:1, 2:3, 5:8}')"
> ret = assert(loadstring(s))()
> print(ret)
nil
I know the string is being executed, because if I set s = print(json.encode('{1:1, 2:3, 5:8}')) I see the output. I just don't know how to get the output in a variable.
Thanks!
I just found a way to do what I wanted, but I'd still like to know if you guys can find any flaw/better way to do it, since I'm very new to Lua:
> s = "json.encode('{1:1, 2:3, 5:8}')"
> s2 = "return("..s..")"
> ret = assert(loadstring(s2))()
> print(ret)
"{1:1, 2:3, 5:8}"
You need to return the value from the loaded chunk. As it is you are telling lua you don't care about the returned value so it is throwing it away.
Note: I don't have the json module handy so I replaced the function with a function that just returns its argument for demonstration purposes:
> json = { encode = function(s) return s end }
> s = "json.encode('{1:1, 2:3, 5:8}')"
> ret = assert(loadstring("return "..s))()
> print(ret)
{1:1, 2:3, 5:8}