(Pipelines) String split; why isn't this working?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

(Pipelines) String split; why isn't this working?

Peter Berghold
Given an input of: /data/ci/jenkins/workspace/PeterBLaboratory/Test-unittestlibobjs-10/somesubdir/somefile.txt

I have a function:

   
    def basename(path){
        def pieces 
        
        pieces = path.split("/")
        
        context.sh 'echo first element: ' + pieces[0]
        context.sh 'echo path: ' + path
        context.sh 'echo parts: ' + pieces.size
        
        def idx
        idx = pieces.size - 1
        
        return pieces[idx]
    }

and I expect it to return the last element in a split array based on the character '/'

What I'm seeing instead is:
groovy.lang.MissingPropertyException: Exception evaluating property 'size' for java.util.Arrays$ArrayList, Reason: groovy.lang.MissingPropertyException: No such property: size for class: java.lang.String

what the heck is going on here?

It also looks like path.split is not behaving as I'd expect and returning an array of strings based on the delimiter. Thoughts folks?

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAArvnv3AbsixuXJ14epdqLOJFg5hspnwiCuDCAvyLx61dxLwRA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: (Pipelines) String split; why isn't this working?

slide
This is off topic of what you are asking, but why not just use context.echo instead of context.sh? Also, use string interpolation:

context.echo "first element ${pieces[0]}"

Note: you must use double quotes for this to work. 

Also, you can def and assign on the same line:

def pieces = path.split('/').

path.split takes a regular expression, so you need to make sure you are escaping anything that needs to be escaped (in your case it doesn't look like anything does, just something to keep in mind).

On Mon, Jan 8, 2018 at 12:53 PM Peter Berghold <[hidden email]> wrote:
Given an input of: /data/ci/jenkins/workspace/PeterBLaboratory/Test-unittestlibobjs-10/somesubdir/somefile.txt

I have a function:

   
    def basename(path){
        def pieces 
        
        pieces = path.split("/")
        
        context.sh 'echo first element: ' + pieces[0]
        context.sh 'echo path: ' + path
        context.sh 'echo parts: ' + pieces.size
        
        def idx
        idx = pieces.size - 1
        
        return pieces[idx]
    }

and I expect it to return the last element in a split array based on the character '/'

What I'm seeing instead is:
groovy.lang.MissingPropertyException: Exception evaluating property 'size' for java.util.Arrays$ArrayList, Reason: groovy.lang.MissingPropertyException: No such property: size for class: java.lang.String

what the heck is going on here?

It also looks like path.split is not behaving as I'd expect and returning an array of strings based on the delimiter. Thoughts folks?

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAArvnv3AbsixuXJ14epdqLOJFg5hspnwiCuDCAvyLx61dxLwRA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAPiUgVfJ%3D%3DECwKUkcKS3puWqXZGaSweVYVpZ3iSUy%2BASNb%2BKnw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: (Pipelines) String split; why isn't this working?

slide
FYI, you want pieces.length, not pieces.size.String.split returns a String[], which you get the length of using .length



On Mon, Jan 8, 2018 at 1:01 PM Slide <[hidden email]> wrote:
This is off topic of what you are asking, but why not just use context.echo instead of context.sh? Also, use string interpolation:

context.echo "first element ${pieces[0]}"

Note: you must use double quotes for this to work. 

Also, you can def and assign on the same line:

def pieces = path.split('/').

path.split takes a regular expression, so you need to make sure you are escaping anything that needs to be escaped (in your case it doesn't look like anything does, just something to keep in mind).

On Mon, Jan 8, 2018 at 12:53 PM Peter Berghold <[hidden email]> wrote:
Given an input of: /data/ci/jenkins/workspace/PeterBLaboratory/Test-unittestlibobjs-10/somesubdir/somefile.txt

I have a function:

   
    def basename(path){
        def pieces 
        
        pieces = path.split("/")
        
        context.sh 'echo first element: ' + pieces[0]
        context.sh 'echo path: ' + path
        context.sh 'echo parts: ' + pieces.size
        
        def idx
        idx = pieces.size - 1
        
        return pieces[idx]
    }

and I expect it to return the last element in a split array based on the character '/'

What I'm seeing instead is:
groovy.lang.MissingPropertyException: Exception evaluating property 'size' for java.util.Arrays$ArrayList, Reason: groovy.lang.MissingPropertyException: No such property: size for class: java.lang.String

what the heck is going on here?

It also looks like path.split is not behaving as I'd expect and returning an array of strings based on the delimiter. Thoughts folks?

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAArvnv3AbsixuXJ14epdqLOJFg5hspnwiCuDCAvyLx61dxLwRA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAPiUgVeduU%2Bmhr_iBE9DdYEOpntMHDTYwqqYUHQDhJ%3DR89MJbg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.