Jeri has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
I would like to load an array with...
20- characters 'A'
20- characters 'T'
30- characters 'G'
30- characters 'C'
I've written
my @arr = ("A" x 20, "T" x 20, "C" x 30, "G" x 30);
but it puts
$arr[0]=AAAAAAAAAAAAAAAAAAAA
$arr[1]=TTTTTTTTTTTTTTTTTTTT
$arr[2]=CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
$arr[3]=GGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
instead of having 0-99 indices.
Does anyone know how to load this with minimal syntax? I've done this before, but I can't remember how to do it.
Thanks
Re: Loading multiple identical variables in array with multiplication variable
by LanX (Saint) on Sep 24, 2014 at 16:45 UTC
|
Yes, surround the strings with parens to enforce list repetition ( vs string)
edit
DB<126> @arr = (("A") x 2, ("T") x 2, ("C") x 3, ("G") x 3);
=> ("A", "A", "T", "T", "C", "C", "C", "G", "G", "G")
update
see perlop#Multiplicative-Operators "repetition operator" for details
Binary "x" is the repetition operator. In scalar context or if the
left operand is not enclosed in parentheses, it returns a string
consisting of the left operand repeated the number of times specified
by the right operand. In list context, if the left operand is enclosed
in parentheses or is a list formed by "qw/STRING/", it repeats the
list. If the right operand is zero or negative, it returns an empty
string or an empty list, depending on the context.
but I have to admit it sounds confusing ... :)
update
hmm should maybe be rephrased:
DB<129> $arr = ("G") x 3;
=> "GGG"
always a string in scalar context!
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
| [reply] [d/l] [select] |
|
| [reply] |
Re: Loading multiple identical variables in array with multiplication variable
by toolic (Bishop) on Sep 24, 2014 at 16:49 UTC
|
use warnings;
use strict;
my @arr = (("A") x 20, ("T") x 20, ("C") x 30, ("G") x 30);
print scalar @arr, "\n";
__END__
100
| [reply] [d/l] |
|
awesome. I wasn't able to properly search for it in the documentation. Thanks
| [reply] |
|
> pd x
Matches for x found in :
/usr/share/perl/5.10/pod/perlop.pod
pager are opened for each match and files are scolled to the right place with highlighted keyword
">>> X<x> <<<" in perlop
Binary "x" is the repetition operator. In scalar context or if
+ the
left operand is not enclosed in parentheses, it returns a strin
+g
consisting of the left operand repeated the number of times spe
+cified
by the right operand. In list context, if the left operand is
+enclosed
in parentheses or is a list formed by "qw/STRING/", it repeats
+the
list. If the right operand is zero or negative, it returns an
+empty
string or an empty list, depending on the context.
print '-' x 80; # print row of dashes
print "\t" x ($tab/8), ' ' x ($tab%8); # tab over
@ones = (1) x 80; # a list of 80 1's
@ones = (5) x @ones; # set all elements to 5
HTH! =)
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
| [reply] [d/l] [select] |
|
| [reply] |
Re: Loading multiple identical variables in array with multiplication variable
by AnomalousMonk (Archbishop) on Sep 24, 2014 at 16:48 UTC
|
my @arr = (('A') x 20, ('T') x 20, ('C') x 30, ('G') x 30);
Update: Or perhaps:
my @arr =
map { ($_->[0]) x $_->[1] }
[A => 20], [T => 20], [C => 30], [G => 30]
;
| [reply] [d/l] [select] |
Re: Loading multiple identical variables in array with multiplication variable
by roboticus (Chancellor) on Sep 24, 2014 at 22:37 UTC
|
Jeri:
A method that's pretty close to what you originally posted:
my @arr = map {split//} "A"x20, "T"x20, "C"x30, "G"x30;
...roboticus
When your only tool is a hammer, all problems look like your thumb.
| [reply] [d/l] |
Re: Loading multiple identical variables in array with multiplication variable
by johngg (Canon) on Sep 24, 2014 at 22:06 UTC
|
Another variation using an inner map instead of the list multiplier and a list of hashrefs as the template.
$ perl -Mstrict -Mwarnings -E '
my @array =
map {
my( $key ) = keys %$_;
map $key, 1 .. $_->{ $key };
} { A => 3 }, { T => 4 }, { C => 2 }, { G => 5 };
say for @array;'
A
A
A
T
T
T
T
C
C
G
G
G
G
G
$
I hope this is of interest.
Update: Another couple of variants:-
$ perl -Mstrict -Mwarnings -E '
my @array =
map {
( keys %$_ )
x
do { my( $v ) = values %$_; $v }
} { A => 3 }, { T => 4 }, { C => 2 }, { G => 5 };
say for @array;'
...
$ perl -Mstrict -Mwarnings -E '
my @array =
map {
my( $k, $v ) = each %$_;
( $k ) x $v;
} { A => 3 }, { T => 4 }, { C => 2 }, { G => 5 };
say for @array;'
...
| [reply] [d/l] [select] |
Re: Loading multiple identical variables in array with multiplication variable
by karlgoethebier (Abbot) on Sep 24, 2014 at 19:18 UTC
|
#!/usr/bin/env perl
+
use strict;
use warnings;
# my @array = split('', join "", ( "A" x 20, "T" x 20, "C" x 30, "G"
+x 30 ) );
# my @array = split '', join '', ( "A" x 20, "T" x 20, "C" x 30, "G" x
+ 30 );
my @array = split '', join '', "A" x 20, "T" x 20, "C" x 30, "G" x 30;
print scalar @array . qq(\n);
print for @array;
__END__
But it works.
Update: No parentheses.
Regards, Karl
«The Crux of the Biscuit is the Apostrophe»
| [reply] [d/l] |
|
|