I have a dataframe with a list of strings in it
df$a
=========
"4343-2"
"7889-5"
"4-3456"
"334-45"
"8765-4"
I'd like to perform a string operation on the list to remove the dash sign, so I did this..
df$a <- lapply(df$a, sub, "-","", df$a)
..which only produces a set of completely empty strings. What did I get wrong?
you can just use sub directly.
df$a <- sub('-', '', df$a)
Instead of the convoluted lapply you're doing since sub is "vectorized". You can also use gsub if you think there may be more than one dash per entry.
Related
This appears to be pretty basic but I am unable to find a suitable pipeline expression function to achieve this.
I have set an array variable VAR1 with the following value, which is an output from a SQL Lookup activity in an ADF pipeline:
[
{
"Code1": "1312312"
},
{
"Code1": "3524355"
}
]
Now, I need to convert this into a comma separated string so I can pass it to a SQL query in the next activity - something like:
"'1312312','3524355'"
I am unable to find an expression function to iterate over the array elements, nor convert an array to a string. The only pipeline expression functions I see are to convert string to array and not the other way around.
Am I missing something basic? How can this be achieved?
Use 'join' function present in 'collection' functions in 'Add dynamic content'. For example:
join(variables('ARRAY_VARIABLE'), ',')
I had this same issue and was not totally satisfied just using the join function because it keeps the keys from the json object. Also, using an iterator approach can work but is needlessly expensive and slow if you have a long list. Here was my approach, using join and replace:
replace(replace(join(variables('VAR1'), ','), '{"Code1":', ''), '}', ''))
This will give you exactly the output you are looking for.
I got it working using a ForEach loop activity to iterate over my array and use a Set Variable task with a concat expression function to create my comma separated string.
Wish they had an iterator function in the expression language itself, that would have made it much easier.
In case, you just have two elements in the array, then you can do something like:
#concat(variables('variable_name')[0].Code1, ',', variables('variable_name')[1].Code1)
I can have two types of string: nxs_flo_dev.nexus orfpdesk.
I want to test if there is a . in the string.
If there is a . I divide the string otherwise I do not do anything.
if(ngx.var.host.contains('.') then
content_by_lua 'ngx.say(ngx.var.host:match("(.-)%."))';
end
Is there a function to do that? Because .contains() doesn't work.
Use match again. Remember to escape the dot with %.:
if ngx.var.host:match('%.') then
If you want to do this inside content_by_lua do
content_by_lua 'if ngx.var.host:match('%.') then ngx.say(ngx.var.host:match("(.-)%.")) end';
Given your edit, this is the simplest solution:
content_by_lua 'ngx.say(ngx.var.host:match("(.-)%.") or ngx.var.host)';
I have a string /sample/data. When I split using split I get the following result,
["","sample","data"]
I want to ignore the empty string(s). So I tried the following code,
"/sample/data".split('/').findAll(it != "")
It gives me an error "cannot call String[] findAll with argument bool".
How can I split and get a List without empty string in it?
split method returns array.
If you need List, use tokenize
"/sample/data".tokenize('/')
also you don't need to use findAll in this case.
You can do as below:
println "/sample/data".split('/').findAll {it}
findAll {it} would fetch all the non empty values.
Parens would work (see comments on question). So your solution is already close:
"/a/b".split("/").findAll()
Because most of the Groovy functions have a zero arity, which will call the function with an identity closure. And since an empty string is considered falsey, this will filter them out.
The findvalue function in HTML::TreeBuilder::XPath returns a concatenation of any values found by the xpath query.
Why does it do this, and how could a concatenation of the values be useful to anyone?
Why does it do this?
When you call findvalue, you're requesting a single scalar value. If there are multiple matches, they have to be combined into a single value somehow.
From the documentation for HTML::TreeBuilder::XPath:
findvalue ($path)
...If the path returns a NodeSet, $nodeset->xpath_to_literal is called automatically for you (and thus a Tree::XPathEngine::Literal is returned).
And from the documentation for Tree::XPathEngine::NodeSet:
xpath_to_literal()
Returns the concatenation of all the string-values of all the nodes in the list.
An alternative would be to return the Tree::XPathEngine::NodeSet object so the user could iterate through the results himself, but the findvalues method already returns a list.
How could a concatenation of the values be useful to anyone?
For example:
use strict;
use warnings 'all';
use 5.010;
use HTML::TreeBuilder::XPath;
my $content = do { local $/; <DATA> };
my $tree = HTML::TreeBuilder::XPath->new_from_content($content);
say $tree->findvalue('//p');
__DATA__
<p>HTML is just text.</p>
<p>It can still make sense without the markup.</p>
Output:
HTML is just text.It can still make sense without the markup.
Usually, though, it makes more sense to get a list of matches and iterate through them instead of doing dumb concatenation, so you should use findvalues (plural) if you could have multiple matches.
Use
( $tree->findvalues('//p') )[0] ;
instead.
Trying to use rstrip() at its most basic level, but it does not seem to have any effect at all.
For example:
string1='text&moretext'
string2=string1.rstrip('&')
print(string2)
Desired Result:
text
Actual Result:
text&moretext
Using Python 3, PyScripter
What am I missing?
someString.rstrip(c) removes all occurences of c at the end of the string. Thus, for example
'text&&&&'.rstrip('&') = 'text'
Perhaps you want
'&'.join(string1.split('&')[:-1])
This splits the string on the delimiter "&" into a list of strings, removes the last one, and joins them again, using the delimiter "&". Thus, for example
'&'.join('Hello&World'.split('&')[:-1]) = 'Hello'
'&'.join('Hello&Python&World'.split('&')[:-1]) = 'Hello&Python'