4

I am working on a perl module and looking for an output (string) of the form : a:value1 OR a:value2 OR a:value3 OR ...

The values value1, value2, value3... are in an array (say, @values).

I know we could use join( ' OR ', @values ) to create a concatenated string of the form: value1 OR value2 OR value3 OR ...

But as you see above, I need an additional a: to be prepended to each value.

What would be a neat way to do so?

4

1 に答える 1

5

You typically use map for these kinds of things:

#!/usr/bin/env perl
use strict;
use warnings;

my @array = qw(value1 value2 value3);
print join(" OR ", map "a:$_", @array),"\n";

Output:

a:value1 OR a:value2 OR a:value3

map is a simple looping construct, which is useful when you want to do apply some simple logic to every element of a list without making too much clutter of your code.

于 2012-05-25T06:31:50.250 に答える