Is Perl JOIN different?

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

The values value1, value2, value3... are in the array (e.g. @values).

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

But, as you see above, I need to add an extra a: for each value.

What would be a neat way to do this?

+4
source share
1 answer

Usually you use map for the following things:

 #!/usr/bin/env perl use strict; use warnings; my @array = qw(value1 value2 value3); print join(" OR ", map "a:$_", @array),"\n"; 

Conclusion:

 a:value1 OR a:value2 OR a:value3 

map is a simple loop construction that is useful when you want to apply some simple logic to each element of a list without creating too much of your code.

+5
source

Source: https://habr.com/ru/post/1414306/


All Articles