Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Concatenating Array elements

by frozenwithjoy (Priest)
on Aug 19, 2014 at 20:47 UTC ( [id://1098046]=note: print w/replies, xml ) Need Help??


in reply to Concatenating Array elements

First off, arrays have a 0-based index. For example:
my @array = 1..10; print "Index 7 (before): $array[7]\n"; print "Index 8 (before): $array[8]\n"; $array[7] = $array[7] . $array[8]; print "Index 7 (after): $array[7]\n"; print "Index 8 (after): $array[8]\n";

Would yield:

Index 7 (before): 8 Index 8 (before): 9 Index 7 (after): 89 Index 8 (after): 9

If you want to have an element disappear when concatenated elsewhere, you could use splice:

$array[7] = $array[7] . splice @array, 8, 1;
Your method: 1 2 3 4 5 6 7 89 9 10 Splice method: 1 2 3 4 5 6 7 89 10

Replies are listed 'Best First'.
Re^2: Concatenating Array elements
by wilmer_t (Novice) on Aug 19, 2014 at 21:54 UTC
    On splice; the third index tells perl how many elements to remove. Thus this effectively does what you want

    Just want to add to the lineup above that after the splice, index 8 still exists and now holds value 10 and so on (whereas previously index n held value n+1, it now holds n+2 for n = 8..15

    Or am I just too tired? :P

      Or am I just too tired? :P

      I think so.

      What would you expect to happen with the indices, if you insert elements into the Array (fourth Parameter of splice)?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-19 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found