DominiqueBal
DominiqueBal

Reputation: 797

Print auto-increment strings from Perl map

This Perl script prints output like this

value value value value
my $tree = HTML::TreeBuilder::XPath->new_from_content( $content );

my @myvalue = $tree->findvalues('//html/body/center[1]/table/tbody/tr[4]/td[1]/following-sibling::td'); 

@myvalue = map {/^(\d+)/; $1} @myvalue;
print join(' ', @myvalue);

Instead I need it to print like this

foostring1:value foostring2:value foostring3:value foostringn:value

How do I prefix the values with an integer-auto-incremented string?

Upvotes: 1

Views: 340

Answers (2)

Borodin
Borodin

Reputation: 126742

The auto-increment operator will do this for you. This is all that is necessary

my @values = qw/ value value value value /;

my $key = 1;
say join ' ', map { 'foostring' . $key++ . ":$_" } @values;

output

foostring1:value foostring2:value foostring3:value foostring4:value

Upvotes: 2

mpapec
mpapec

Reputation: 50657

You should also check whether regex was successful in order to have desired content in $1,

my $i = 0;
@myvalue = map { /^(\d+)/ ? sprintf("foostring%s:%s", ++$i, $1) : () } @myvalue;

Upvotes: 1

Related Questions