How do I form the gstring below ? - groovy

I am getting an error on forming the gstring below, can someone suggest the right way to form this gstring?
for(File fileToUnarchive: filesToUnarchive)
{
antBuilder.mkdir(dir:"${destinationDirectory}/${getSequenceNumber(${fileToUnarchive.name})}")
}

The problem is you're trying to double template the last section
Instead of
antBuilder.mkdir(dir:"${destinationDirectory}/${getSequenceNumber(${fileToUnarchive.name})}")
just do
antBuilder.mkdir(dir:"${destinationDirectory}/${getSequenceNumber( fileToUnarchive.name )}")

Related

String comparison in groovy script not working

I am trying to compare 2 Strings in groovy script. both have same value but they are in different case while m trying compare it using equalsIgnoreCase still it is showing not equals.
Here is my code:
def st1=Austin
def ct=AUSTIN
if(st1.equalsIgnoreCase(ct)){
log.info "city equals"
}
else{
log.info "not eq"
}
it's printing "not eq".I tried toString() and toUpperCase methods.Plz help me out
Sorry for the erroneous post. I got my problem I was working with db. it gives me value with some extra spaces. So my comparison was not working. later I used
trim()
to remove it.

IwNUI CTextFieldPtr to string

I'm using a IwNUI CTextFieldPtr control and I would like to store/use the text attribute stored on the object in a string variable. I need to use that string but I have no clue on the documentation or examples on how to do it... I don't have a complete code sample either because what I'm asking should be pretty straight forward, such as:
CTextFieldPtr login_tUsername;
//textfield init here
std::string c_username;
login_tUsername->GetAttribute("text", c_username);
Please help me, thank you very much!
And other approach, would be something like this, which is by far much closer to what I wanted to do:
CString value;
login_tUsername->GetAttribute("text", value);
std::string thestring = value.Get();
:)
(Credit goes billarhos billarhos)
Well... the best solution I found for this problem was something like this:
login_tUsername->SetEventHandler("textchanged", this, &OnUserEdit);
bool OnUserEdit(CTextField* textField, const char* text)
{
c_username = text;
return true;
}
I don't know why, but it seems you cannot use the textbox text attribute directly.
Cheers!

how to retrieve selected item from ListBox and convert it to string?

I tried
if( ListBox.SelectedItem.ToString().Equals("test")
{
//do something
}
and
if( ListBox.SelectedValue.ToString().Equals("test")
{
//do something
}
none of them gets the selected value or item into string
if that is actual looking of your code there are mistakes. you should add one brace at the end of if statement.
if not i don't see any mistakes at there. But beware if you use SelectedItem.toString(). That is not giving you the value. That returns the object value. example it will give you something like this "System.Data.DataRowView". So that condition never comes true.
that's all i can see from your code.

What do empty square brackets after a variable name mean in Groovy?

I'm fairly new to groovy, looking at some existing code, and I see this:
def timestamp = event.timestamp[]
I don't understand what the empty square brackets are doing on this line. Note that the timestamp being def'd here should receive a long value.
In this code, event is defined somewhere else in our huge code base, so I'm not sure what it is. I thought it was a map, but when I wrote some separate test code using this notation on a map, the square brackets result in an empty value being assigned to timestamp. In the code above, however, the brackets are necessary to get correct (non-null) values.
Some quick Googling didn't help much (hard to search on "[]").
EDIT: Turns out event and event.timestamp are both zero.core.groovysupport.GCAccessor objects, and as the answer below says, the [] must be calling getAt() on these objects and returning a value (in this case, a long).
The square brackets will invoke the underlying getAt(Object) method of that object, so that line is probably invoking that one.
I made a small script:
class A {
def getAt(p) {
println "getAt: $p"
p
}
}
def a = new A()
b = a[]
println b.getClass()
And it returned the value passed as a parameter. In this case, an ArrayList. Maybe that timestamp object has some metaprogramming on it. What does def timestamp contains after running the code?
Also check your groovy version.
Empty list, found this. Somewhat related/possibly helpful question here.
Not at a computer, but that looks like it's calling the method event.timestamp and passing an empty list as a parameter.
The same as:
def timestamp = event.timestamp( [] )

preg_replace: reference object in replacement

Do you know of any way to reference an object in the replacement part of preg_replace. I'm trying to replace placeholders (delimited with precentage signs) in a string with the values of attributes of an object. This will be executed in the object itself, so I tried all kinds of ways to refer to $this with the /e modifier. Something like this:
/* for instance, I'm trying to replace
* %firstName% with $this->firstName
* %lastName% with $this->lastName
* etc..
*/
$result = preg_replace( '~(%(.*?)%)~e', "${'this}->{'\\2'}", $template );
I can't get any variation on this theme to work. One of the messages I've been getting is: Can't convert object Model_User to string.
But of course, it's not my intention to convert the object represented by $this to a string... I want to grab the attribute of the object that matches the placeholder (without the percentage signs of course).
I think I'm on the right track with the /e modifier. But not entirely sure about this either. Maybe this can be achieved much more simple?
Any ideas about this? Thank you in advance.
Like I commented to Paul's answer: in the meanwhile I found the solution myself. The solution is much more simple than I thought. I shouldn't have used double quotes.
The solution is as simple as this:
$result = preg_replace( '~(%(.*?)%)~e', '$this->\\2', $template );
Hope this helps anyone else for future reference.
Cheers.
Check out preg_replace_callback - here's how you might use it.
class YourObject
{
...
//add a method like this to your class to act as a callback
//for preg_replace_callback...
function doReplace($matches)
{
return $this->{$matches[2]};
}
}
//here's how you might use it
$result = preg_replace_callback(
'~(%(.*?)%)~e',
array($yourObj, "doReplace"),
$template);
Alternatively, using the /e modifier, you could maybe try this. I think the only way to make it work for your case would be to put your object into global scope
$GLOBALS['yourObj']=$this;
$result = preg_replace( '~(%(.*?)%)~e', "\$GLOBALS['yourObj']->\\2", $template );

Resources