tags:

views:

320

answers:

5

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?

+6  A: 
$newstring = join('', @array[$i..$j])
xiechao
thanks. if $j is end of string, i am using scalar(@array)-1 for $j. is there any other way of doing that.
iamrohitbanga
@iamrohitbanga Yes: `$#array` is a shorter way of saying `scalar@array - 1`
mobrule
Sorry I don't follow the "$j is end of string" part. But scalar(@array)-1 is $#array.
xiechao
The last time I said that `@array-1 == $#array`, somebody pointed out that "not if `$[` has been changed". Admittedly, an odd scenario that we usually don't even care to think about, most of the time...
ephemient
The point is to use the one that means what you want. Since in this case you want the last thing in `@array`, you write `$#array` because it *means* the last index in `@array`. You don't write `@array - 1` because that means "one less than the number of things in `@array`".
hobbs
+5  A: 
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 ''.

dsolimano
A: 

Try this ....

use warnings ;
use strict ;
use Data::Dumper ;
my $string ;
map { $string .=  $_; } @arr[$i..$j] ;
print $string ;
pavun_cool
You shouldn't use `map` in void context; a `for` loop would work just as well. But loops of any sort are unnecessary when you can just use `join`.
friedo
Is void `map` evil? I'd use a postfix `for` most places a void `map` can be used. Here I'd use a `join`. But Perl 5.8.1 and up optimize away map's return values when called it is called in void context. For more discussion of map in void context see: http://www.perlmonks.org/index.pl?node_id=296742
daotoad
A: 

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".

AmbroseChapel
You could also `undef $";` to eliminate characters between array elements. It is a good practice to localize changes to the global `$"` variable to its own block.
toolic
A: 

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?

Cantello
$c and $i are not numeric
iamrohitbanga
how did you initialize these?
iamrohitbanga
Hmm... $c and $i should be numeric as they are initialised in a "for $i (0 .. $xy)" loop.I have now 'fixed' it by using discrete commands for each possibility.Thanks for your help, though! :-)
Cantello