Search code examples
perlauto-increment

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?


Solution

  • 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