PHP Strict Standards on line with variable [duplicate] - string

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Annoying PHP error: “Strict Standards: Only variables should be passed by reference in”
I have this line of code,
$extension=end(explode(".", $srcName));
when I fun my function I get
PHP Strict Standards: Only variables should be passed by reference in
I am not sure how to solve this

The function end() requires a variable to be passed-by-reference and passing the return-value of a function doesn't acheive this. You'll need to use two lines to accomplish this:
$exploded = explode(".", $srcName);
$extension = end($exploded);
If you're simply trying to get a file-extension, you could also use substr() and strrpos() to do it in one line:
$extension = substr($srcName, strrpos($srcName, '.'));
Or, if you know the number of .'s that appear in the string, say it's only 1, you can use list() (but this won't work if there is a dynamic number of .'s:
list(,$extension) = explode('.', $srcName);

Related

Python, what is this for loop doing? [duplicate]

This question already has answers here:
What does "list comprehension" and similar mean? How does it work and how can I use it?
(5 answers)
Closed 3 years ago.
I have scoured the internet and I cannot find any reference to this type of for loop:
variable = [(item["attribute1"], item["attribute2]") for item in piece_of_json_data]
I am using this to update wtform's SelecField choices:
form.SelectField.choices = variable
but I can only get it to work if I replace one of the attributes in parenthesis with a static number:
variable = [(1, item["attribute2"]) for item in piece_of_json_data]
but that sets the value of the option field to "1", when I need the option values to be one of the attributes as a string.
Does this create a dict? a tuple? is there some kind of terminology for this that I can use to find documentation?
Thanks to the comments, I now understand that I am using list comprehension to create a tuple. The tuple works fine with both string and integer values. My issue has to do with .choices not accepting a string.
I found that my only problem is that I had coerce set to int on my selectfield, so naturally it wanted an integer.

Builtin string functions with $ [duplicate]

This question already has answers here:
What does the $ symbol do in VBA?
(5 answers)
Closed 4 years ago.
Is there any difference between using a builtin function returning a string such as Left or using the same function with a $ appended (Left$)?
The output of this:
Debug.Print Left("Foo", 2)
Debug.Print Left$("Foo", 2)
is always
Fo
Fo
I suspect that it's strictly the same thing and that the $ versions exist only for some compatibility reasons.
The typed functions (those ending with a $) return a String. The un-typed versions return a Variant. Internally, these are handled by a pair of different functions (in the case of Left, it is _B_str_Left and _B_var_Left).
If you are assigning the return value to a String or a parameter expecting a String, using the typed version (Left$) avoids an implicit cast to a Variant. Similarly, if you're assigning to a Variant, using the un-typed version avoids a cast.
Left$() wants a string as argument but Left() expects a variant. Therefore using Left$() is faster if you know you will always pass it a string.

Underscore "_" in for loop [duplicate]

This question already has answers here:
What is the purpose of the single underscore "_" variable in Python?
(5 answers)
Trying to understand Python loop using underscore and input
(4 answers)
Closed 4 years ago.
I was check a solution on hacker rank where i was solving a question asking to print the name of the person with the second highest score from an input which has to be converted to a nested list first .
I understood all the logic in the code and most part of the code but why the Underscore ( _ ) in the for loop .Please explain me the code if there is a different concept .
marksheet = []
for _ in range(0,int(input())):
marksheet.append([input(), float(input())])
second_highest = sorted(list(set([marks for name, marks in marksheet])))[1]
print('\n'.join([a for a,b in sorted(marksheet) if b == second_highest]))
It's a Pythonic convention to use underscore as a variable name when the returning value from a function, a generator or a tuple is meant to be discarded.
In your example, the code inside the for loop does not make any use of the values generated by range(0,int(input())), so using an underscore makes sense as it makes it obvious that the loop does not intend to make use of it.

Bash Scripting with function that takes an argument [duplicate]

This question already has answers here:
How to pass all arguments passed to my Bash script to a function of mine? [duplicate]
(7 answers)
Closed 5 years ago.
I need to create a function and pass an argument like
myfunc word_100
and then the output should display
word_101
Basically it should increment taking in account the delimiter. I am thinking to say put word as one variable and the number and increment the number and combine it together. But not sure how to go about.
Try:
NAME=${1%_*}_
NUM=${1##*_}
echo $NAME`expr $NUM + 1`

Matlab Functions - How do I access structures with string-input? [duplicate]

This question already has answers here:
How do I access structure fields dynamically?
(6 answers)
Closed 9 years ago.
If I have a function like this:
function [ out ] = call(a)
out = s.a
end
How can I get it to access the structure s.hello with call('hello') or someting like this?
side question: Is it also possible to access a Variable "hello" with such a function?
Thanks in advance, you guys are awesome!
I would use dynamic structure access like so:
s.(a)
Learn more at the Mathworks website!
Also, if we look at your example function, I notice you're not passing in the structure as an argument, maybe it's global, but here an example of this technique using your function as a framework:
function out = call(s,a)
out = s.(a);
end
Then to use the function, I try:
>> s = struct('hello',42)
s =
hello: 42
>> call(s,'hello')
ans =
42
Works great with no recursion limit! If you're still getting a recursive function, try adding more of your code to the question, we'll get to the bottom of this!
HTH

Resources