Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Concatenation to elements in an array !

by pspillai (Initiate)
on Mar 24, 2011 at 19:52 UTC ( [id://895327]=perlquestion: print w/replies, xml ) Need Help??

pspillai has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Is there a way to concatenate a string to every element of an array? For example,,, in an array @array=(1,2,3,4,5).. what I need is @array=("1a","2a","3a","4a","5a"). Thank you. P.
  • Comment on Concatenation to elements in an array !

Replies are listed 'Best First'.
Re: Concatenation to elements in an array !
by toolic (Bishop) on Mar 24, 2011 at 19:57 UTC
    map
    use warnings; use strict; my @array = (1,2,3,4,5); @array = map { qq("${_}a") } @array; print "@array\n"; __END__ "1a" "2a" "3a" "4a" "5a"
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Concatenation to elements in an array !
by umasuresh (Hermit) on Mar 24, 2011 at 19:58 UTC
    use strict; use warnings; my @array = qw(1 2 3 4 5 ); my @new_array = map { $_."a"} @array; print join("\t", @new_array);
Re: Concatenation to elements in an array !
by logie17 (Friar) on Mar 24, 2011 at 20:54 UTC
    Something along these lines will do the trick:
    my @modified_array = map { $_ . "a" } (1,2,3,4,5);
    s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;
Re: Concatenation to elements in an array !
by sundialsvc4 (Abbot) on Mar 24, 2011 at 22:07 UTC

    One thought that flips through my head is that, unless you know that you really are going to “touch” every single one of those elements, you’re going to pay a terrible price in virtual-memory “thrashing” as you churn through all of those elements.   (Think “hundreds of thousands of page-faults, each one requiring milliseconds to complete.”   Suddenly, your CPU has been driven to a rate of performance that is entirely dictated by a physical device.   Ouch!!)

    Perhaps you could write a sub that returns the correct answer.   This would, of course, perform the concatenation “on the fly,” and therefore perhaps millions of times, but it would only be invoked when the value was actually needed.   This would avoid the virtual-memory “churn.”   And if you know that you are walking through an array stem-to-stern, a function like each() might be much more advantageous than an index-variable.

    (P.S. If you do write a sub, don’t get fancy.   Don’t update the value in the array-element so that you don’t have to calculate the value again, because when you do that, you have just dirtied the page and obliged it to eventually be paged-out.   You don’t want to impose an expensive I/O operation, which takes milliseconds, to avoid a cheap CPU operation, which takes nanoseconds.)

    Edit:   Oops!   Nevermind!!   You’re writing to a file, a row at a time.   So what I just said doesn’t apply in your case.   (Unless you can avoid creating that file.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://895327]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-19 19:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found