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

Getting out an array within an array

by sokatron (Initiate)
on Oct 01, 2012 at 18:52 UTC ( #996715=perlquestion: print w/replies, xml ) Need Help??
sokatron has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Im new here and somewhat new to PERL. Im trying to learn how to apply some PERL to our research data. I seem to run into a problem when I want each for-loop to create a separate array from that loop. In my makeshift solution below I made an array within an array, where each array in the AoA represents a number of values for each sample. The problem then, is that I cannot extract each array and put into a separate array, it seems to be a easy question, perhaps the answer is to obvious. @A = @B[0]; This did not work to transfer the first array into a separate array. It also presents a problem because I would need to declare X new arrays (one for each sample) and then Im back in the looping and declaring. Any help or suggestions would be appreciated Cheers Oskar
#!/usr/bin/perl use strict; my ($antal); my ($size); print "How many replicates? "; $antal = <stdin>; print "How many Samples? "; $size = <stdin>; my ($tam); my ($tim); $tim = 0; my ($x); my (@a); my ($repvalue); my ($repvalue2); $repvalue2 = 1; for ($x=0; $x<$size; $x++) #For each Sample the number of replicat +es are entered { for ($tam=0; $tam<$antal; $tam++ ) #For each replicate in the +sample a value is added { $repvalue = $tam+1; print "Imput replicate value $repvalue for sample $repvalu +e2: "; $a[$tim][$tam] = <stdin>; } $repvalue2++; $tim++; print "\n"; } my ($n); my ($m); my ($n2); my ($m2); for ($n = 0; $n<$size; $n++) #For printing and checking all values { print "\n\n"; $n2 = $n+1; print "Replicates of sample $n2:\n\n"; for ($m=0; $m<$antal; $m++) { $m2 = $m+1; print "Replicate $m2 value is: $a[$n][$m]"; } print "\n\n"; }

Replies are listed 'Best First'.
Re: Getting out an array within an array
by sundialsvc4 (Abbot) on Oct 01, 2012 at 20:03 UTC

    Hello, Oskar!   Maybe the following concept will help to crystalize your thoughts and make the possible approaches clear ...

    Perl, itself, only has a small handful of very-simple data structures ... a one-dimensional “list” or “array,” ... a “hash” ... a “scalar” ... but(!) ... within all of that ... it also has the all-important concept of a reference.

    “A reference” is “a single thing,” just like (say...) an integer or a string, in the sense that “it occupies only one slot in” a scalar or an array or a hash.   But it could refer to “anything(!!).”   (So, if you have ever wished to be in two places at one time, “references” are darned-close to Willy Wonka’s “Golden Ticket.”)

    When you say, “an array within an array,” well ... no such thing exists.   What does exist is something much simpler and much more powerful.   The array-element in question consists of a reference to ... another (in this case) array.

    Here is the “cognitive jump” that you have to jump:   on the one hand, “a reference is” a single thing.   But on the other hand, that reference can refer to absolutely anything at all.   A scalar ... an entire hash ... an entire list ... an entire array ... even itself.

    Please wait patiently until “the little light blinks on,” because when it finally does, it will be well worth it.   You can in facct build data-structures of arbitrary complexity through judicious application of this “small handful of primitives” that the Perl language gives you.   “References” are the “real Golden Ticket” that allows you to pair anything with anything-else.

      Your "post" looks rather "funny" to me. May I recommend :-)

        Your "post" looks rather "funny" to me. May I recommend :-)
        That is just “the tip of the iceberg®”   ...  you “forgot” about ... unnecessary underlines, italics ... ellipses ...   boldface, (parentheses)-and-hyphens and exclamation marks (!!),   non-breaking-spaces ...   ™,   ®,   and other “symbols,” stylistic “devices,” flowery-analogies, and ...   “old bull-shitter wisdoms™”   (“Schweet!”)   ;-)

      Thanks! Thats what I get for "inventing" my own words, no wonder I scored so few google hits if it is not even a feature called that. I think I get how it will serve as a reference. Now that I know the concept I will try to understand how I can use it.
Re: Getting out an array within an array
by Kenosis (Priest) on Oct 01, 2012 at 20:47 UTC

    Welcome, sokatron!

    sundialsvc4 did an excellent(!) job addressing the concept of a reference to assist you with resolving your array issue.

    Although unsolicited--and I apologize in advance if I've confused the issue here--consider the following which doesn't use the C-stype for loop (e.g., for ($x=0; $x<$size; $x++)):

    #!/usr/bin/perl use strict; use warnings; print "How many replicates? "; chomp( my $antal = <> ); print "How many Samples? "; chomp( my $size = <> ); my @a; # Consider a more descriptive name for this array. for my $tim ( 0 .. $size - 1 ) { #For each Sample the number of replic +ates are entered for my $tam ( 0 .. $antal - 1 ) { #For each replicate in the sampl +e a value is added print 'Enter replicate value ' . ( $tam + 1 ) . ' for sample ' . ( $tim + 1 ) . ': '; chomp( my $repValue = <> ); $a[$tim][$tam] = $repValue; #push @{ $a[$tim] }, $repValue; # This notation can be used, t +oo. Is there a "Golden Ticket" here? } print "\n"; } for my $tim ( 0 .. $size - 1 ) { #For printing and checking all values print 'Replicates of sample ' . ( $tim + 1 ) . "\n\n"; for my $tam ( 0 .. $antal - 1 ) { print 'Replicate ' . ( $tam + 1 ) . " value is: $a[$tim][$tam] +\n"; } print "\n"; }

    Hope this is helpful.

      Yes! I think you figured from my code I have not been programming a lot, and the little I did was C. I will start making the code nicer when I have learned the concepts I need. But it is helpful, this would be how my code would look in perl and not in C, and it will be easier to get the examples from the tutorials if I know how the thing I want "should" look like.
Re: Getting out an array within an array
by Lotus1 (Curate) on Oct 01, 2012 at 20:50 UTC

    As SundialSvc4 mentioned reading about references should help. Here is a tutorial.

    Here is some code to help you out. Tack this on to the end of your program.

    use Data::Dumper; print "*"x75,"\n"; print Dumper(@a); print "*"x75,"\n"; chomp @$_ foreach @a; print Dumper(@a); print "*"x75,"\n"; print scalar @a, " elements [references] in \@a\n"; print "*"x75,"\n"; my @new1 = @{$a[0]}; my @new2 = @{$a[1]}; print Dumper($a[0]); print "\@new1 => @new1\n"; print "\@new2 => @new2\n"; print "*"x75,"\n"; foreach (@a) { print "@$_\n"; }
      I will definitely check the tutorial but you code was also really useful. Instead of getting nonsense from my attempt to use only @ and not the curly brackets, atleast I can get the values.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://996715]
Approved by herveus
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2018-06-24 03:52 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.