I have an array of strings: @array
I want to concatenate all strings beginning with array index $i
to $j
.
How can I do this?
I have an array of strings: @array
I want to concatenate all strings beginning with array index $i
to $j
.
How can I do this?
my $foo = join '', @array[$i..$j];
First we generate an array slice with the values that we want, then we join them on the empty character ''.
Try this ....
use warnings ;
use strict ;
use Data::Dumper ;
my $string ;
map { $string .= $_; } @arr[$i..$j] ;
print $string ;
Just enclosing a perl array in quotes is enough to concatenate it, if you're happy with spaces as the concatenation character:
@array = qw(a b c d e f g);
$concatenated = "@array[2 .. 5]";
print $concatenated;
## prints "c d e f"
or of course
$" = '-';
@array = qw(a b c d e f g);
$concatenated = "@array[2 .. 5]";
print $concatenated;
if you'd prefer "c-d-e-f".
When trying to concatenate strings within an array as explained above, I get the following error message:
Argument "" isn't numeric in array element at ./orf.pl line 135, <GEN0> line 1.
I used:
my $newstring = join ('', $digested[$c][$i .. $i+$c+1]);
where $c runs from 0 to 3 and $i from 0 to 2. All elements in the array @digested contain strings. $newstring contains only the first element of the ones that should be joined.
Any hints?