Perl’s “quote word” in Groovy

When it comes to embedding a long list of simple text strings in the code, Perl’s “quote word” is very handy:

my @l = qw(a b c d e f g h);

This is the closest equivalent I could find in Groovy:

def l = "a b c d e f g h".split("\\s+").findAll { it.length() > 0 }

You can also use a multiline string :

def l = """
a
b
c
d
e
f
g
h""".split("\\s+").findAll { it.length() > 0 }

The “findAll” avoid to get empty strings at the beginning of the end of the list in case of empty lines or leading and trailing spaces.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s