Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Loading multiple identical variables in array with multiplication variable

by Jeri (Scribe)
on Sep 24, 2014 at 16:35 UTC ( #1101829=perlquestion: print w/replies, xml ) Need Help??
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

Replies are listed 'Best First'.
Re: Loading multiple identical variables in array with multiplication variable
by LanX (Bishop) 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 ☆☆☆☆ :)

      perfect thanks.
Re: Loading multiple identical variables in array with multiplication variable
by toolic (Bishop) on Sep 24, 2014 at 16:49 UTC
    More parens (Multiplicative Operators):
    use warnings; use strict; my @arr = (("A") x 20, ("T") x 20, ("C") x 30, ("G") x 30); print scalar @arr, "\n"; __END__ 100
      awesome. I wasn't able to properly search for it in the documentation. Thanks
        > I wasn't able to properly search for it in the documentation.

        see Re: Perldoc Keyword Search (update4)

        > 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 ☆☆☆☆ :)

        > I wasn't able to properly search for it in the documentation.

        Another approach: type "x" in the search-field here: http://perldoc.perl.org and you'll get

        • x
        • xor
        These operators are documented in perlop

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: Loading multiple identical variables in array with multiplication variable
by AnomalousMonk (Chancellor) on Sep 24, 2014 at 16:48 UTC

    I.e.,

    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] ;

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.

Re: Loading multiple identical variables in array with multiplication variable
by johngg (Abbot) 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;' ...

    Cheers,

    JohnGG

Re: Loading multiple identical variables in array with multiplication variable
by karlgoethebier (Monsignor) on Sep 24, 2014 at 19:18 UTC

    I don't know if this is mannerism or minimalism ;-)

    #!/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»

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1101829]
Approved by toolic
help
Chatterbox?
and snow settles gently...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2018-07-22 20:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (455 votes). Check out past polls.

    Notices?