-3

I'm trying to create 23 arrays without typing out @array1, @array2, and so on, and load them each with the variables from the array @r if the $chrid matches the array number (if $chrid=1 it should be placed in @array1). How can I achieve this?

Here is what I have so far:

#!/usr/bin/perl
use warnings;
use strict;

my @chr;
my $input;
open ($input, "$ARGV[0]") || die;
while (<$input>) {
    my @r = split(/\t/);
    my $snps = $r[0];
    my $pval = $r[1];
    my $pmid = $r[2];
    my $chrpos = $r[3];
    my $chrid = $r[4];
    for ($chrid) {
        push (@chr, $chrid);
    }
}

close $input;
AST
  • 211
  • 6
  • 18
Jack
  • 1
  • 3

1 Answers1

5

You can use an array of arrays, where each subarray is stored at a sequentially increasing index in your array of arrays. Here is what that could look like, but it is still unclear to me what you want data you want to store:

use warnings;
use strict;

my @chr;
open my $input_fh, '<', $ARGV[0]
   or die "Unable to open $ARGV[0] for reading: $!";
while (< $input_fh> ) {
    # you can unpack your data in a single statement
    my ($snps, $pval, $pmid, $chrpos, $chrid) = split /\t/;

    # unclear what you actually want to store
    push @{ $chr[$chrid] }, ( $snps, $pval, $pmid, $chrpos, $chrid );

}
close $input_fh;
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
  • Thanks and I would like to store a line that contains $snps, $pval, $pmid, $chrpos, and $chrid – Jack Aug 10 '15 at 20:09