Öznur tastan wrote: > > "Rob Dixon" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > > > > Öznur tastan wrote: > > > > > > I want to store alignment results( that is why I asked about struct yesterday). > > > I thougt that I could just push seq1 seq2 and the score to an array and can > > > acess them by using $k*3 + $n ($n=0 for seq1 $n =2 for score) > > > > > > But I also have a sort of grouping of the alignment which is denoted by $p. > > > So I wanted to use a matrix so in each row I would have one group (group index > > > is $p) and in that row alignments features(seq1 seq2 score) > > > > > > I tried this: > > > > As Joseph pointed out in your other thread, using a variable name like $p > > isn't very informative. If I didn't know better I would guess that it may be > > a pointer (but those are called 'references' in Perl) but couldn't > > guess beyond that. What's wrong with $index? > > > > > $p=0; > > > $seq1="A--V"; > > > $seq2="AAAV"; > > > $score=-5; > > > > > > [EMAIL PROTECTED]; > > > push @$ref,$seq1; > > > push @$ref,$seq2; > > > push @$ref,$score; > > > > > > when use this way it gives the error "Not an array reference". > > > > @subalignments[$p] is an array slice with one element, so [EMAIL PROTECTED] > > is a list of one scalar reference. Assigning this list to the scalar $ref > > copies the last (and only) element of the list, so $ref is now a reference > > to the scalar array element $subalignments[$p]. > > then is there a way to push an element to one of the arrays of a double-dimensional > array?
Yes. A Perl two-dimensional array is an array of array references. You can do this: push @{$subalignments[$p]}, $seq1; push @{$subalignments[$p]}, $seq2; push @{$subalignments[$p]}, $score; > > > I think I should declare the-two dimensional array so i thought adding > > > $subalignments[0][0]=0 (silly I know) would work- didn't work:) > > > > There's no need to predeclare arrays in Perl, just use an array as if was > > two-dimensional and it will be. So: > > > > $subalignments[$p][0] = $seq1; > > $subalignments[$p][1] = $seq2; > > $subalignments[$p][2] = $score; > > > > or > > > > $subalignments[$p] = [$seq1, $seq2, $score]; > > > > does the same thing. But I think perhaps you should be using a hash here: > > > > $subalignments[$p] = { > > seq1 => $seq1, > > seq2 => $seq2, > > score => $score, > > }; > > Yes that certainly is better. But $subalignments[$p] should be an array of hashes > because > I want to hold the alignment group there. > > $subalignments[$p][$groupindex] = { > seq1 => $seq1, > seq2 => $seq2, > score => $score, > }; > > would work but holding the $groupindex for each group is tedious so > I need the push the hashes into an row of the double dimensional array > (row would be array of hashes). How would that be? I'm losing track again here. /Please/ post a comprehensive example of your actual data. My best guess is that you should have a hash of hashes: $subalignments{$groupindex} = { seq1 => $seq1, seq2 => $seq2, score => $score, }; but I'm not at all clear on what $p is (hence my comment on useless variable names) and what sort of values $groupindex can have. These things are so important that I'll go through them again: - Please add use strict; use warnings; to any program you write, and especially anything you want help with. - Please use descriptive variable names throughout. Funnily enough I've seen plenty of %hash and @array but never a $scalar - Please give us sight of the data you're trying to process. With these we can help, but without them it's like trying to build a treehouse while looking through a magnifying glass. Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>