Hello friends!
I have a small problem. I am parsing a text file, which looks like:
position patient<-not in file, but for your reference! 34562 1048 t t 30873 1048 t t 25912 1048 N N 25779 1048 t t 26827 1048 t t 24607 1048 t t 22301 1048 t t 24835 1048 t t 24955 1048 N N 25916 1048 N N 22131 1048 t t 22203 1048 t t 23759 1048 t t 23749 1048 t t 18504 1048 t t 20839 1048 c t 34562 1082 c c 30873 1082 c c 25912 1082 N N 25779 1082 c c 26827 1082 c c 24607 1082 c c 22301 1082 c c 24835 1082 c c 24955 1082 N N 25916 1082 N N 22131 1082 c c 22203 1082 c c 23759 1082 c c 23749 1082 c c 18504 1082 c c 20839 1082 c c
As you can see, there is a pattern there, specifically in the left two columns. In the far left column, there are a series of numbers that repeat themselves once the number in the second column changes. I am using a loop to pull in and split up each line, with the split line: ($position, $patient, $letter1, $letter2)=split(/\t/, $prettybase[$i]);. Due to the nature of this loop, I can't use value of $i to say which element of @prettybase to retrieve. Hence, is there a way to (without using a counter) have the loop parse @prettybase one element at a time from 1-until eof, all while keeping its place? I'm somwhat familiar with keeping one's place while pattern matching, but unfortunately this doesn't work here, unless there is something I'm unaware of.
Since most people like to see the big picture, here is the code on the whole:
#! C:\Perl\bin\perl use warnings; use strict; chomp(my $input=<STDIN>); open (INPUT, "$input") or die "Cannot open file: $!"; chomp(my $number_positions=<STDIN>); chomp(my $patient_number=<STDIN>); chomp(my $output=<STDIN>); open (OUTPUT, ">$output.txt") or die "Cannot open file: $!"; my @prettybase=<INPUT>; close (INPUT); my (@a,@c,@t,@g,@n,@a2,@c2,@t2,@g2,@n2); my ($position, $letter1, $letter2, $patient); for (my $k=0;$k<$number_positions; $k++) { @a=@c=@t=@g=@n=@a2=@c2=@t2=@g2=@n2=(); $position="0"; for (my $i=0;$i<=$patient_number; $i++) { ($position, $patient, $letter1, $letter2)=split(/\t/, $prettybas +e[$i]); if ($letter1=~ /[aA]/) {push ( @a, $letter1);} if ($letter1=~ /[cC]/) {push ( @c, $letter1);} if ($letter1=~ /[tT]/) {push ( @t, $letter1);} if ($letter1=~ /[gG]/) {push ( @g, $letter1);} if ($letter1=~ /[nN]/) {push ( @n, $letter1);} if ($letter2=~ /[aA]/) {push ( @a2, $letter2);} if ($letter2=~ /[cC]/) {push ( @c2, $letter2);} if ($letter2=~ /[tT]/) {push ( @t2, $letter2);} if ($letter2=~ /[gG]/) {push ( @g2, $letter2);} if ($letter2=~ /[nN]/) {push ( @n2, $letter2);} } my $number_a=scalar @a; #print "$number_a"; my $number_c=scalar @c; #print "$number_c"; my $number_t=scalar @t; my $number_g=scalar @g; my $number_n=scalar @n; my $number_a2=scalar @a2; my $number_c2=scalar @c2; my $number_t2=scalar @t2; my $number_g2=scalar @g2; my $number_n2=scalar @n2; my $percent_a=((("$number_a")/("$number_a"+"$number_c"+"$number_t"+ +"$number_g"+"$number_n")) * 100); my $percent_c=((("$number_c")/("$number_a"+"$number_c"+"$number_t"+ +"$number_g"+"$number_n")) * 100); my $percent_t=((("$number_t")/("$number_a"+"$number_c"+"$number_t"+ +"$number_g"+"$number_n")) * 100); my $percent_g=((("$number_g")/("$number_a"+"$number_c"+"$number_t"+ +"$number_g"+"$number_n")) * 100); my $percent_n=((("$number_n")/("$number_a"+"$number_c"+"$number_t"+ +"$number_g"+"$number_n")) * 100); my $percent_a2=((("$number_a2")/("$number_a2"+"$number_c2"+"$number +_t2"+"$number_g2"+"$number_n2")) * 100); my $percent_c2=((("$number_c2")/("$number_a2"+"$number_c2"+"$number +_t2"+"$number_g2"+"$number_n2")) * 100); my $percent_t2=((("$number_t2")/("$number_a2"+"$number_c2"+"$number +_t2"+"$number_g2"+"$number_n2")) * 100); my $percent_g2=((("$number_g2")/("$number_a2"+"$number_c2"+"$number +_t2"+"$number_g2"+"$number_n2")) * 100); my $percent_n2=((("$number_n2")/("$number_a2"+"$number_c2"+"$number +_t2"+"$number_g2"+"$number_n2")) * 100); print OUTPUT "Position\#\: $patient\n"; print OUTPUT "\tAllele 1 Allele 1 percentage\n"; print OUTPUT "A:\t$number_a\t$percent_a\%\n\n"; print OUTPUT "C:\t$number_c\t$percent_c\%\n\n"; print OUTPUT "T:\t$number_t\t$percent_t\%\n\n"; print OUTPUT "G:\t$number_g\t$percent_g\%\n\n"; print OUTPUT "N:\t$number_n\t$percent_n\%\n\n"; print OUTPUT "\tAllele 2 Allele 2 percentage\n"; print OUTPUT "A:\t$number_a2\t$percent_a2\%\n\n"; print OUTPUT "C:\t$number_c2\t$percent_c2\%\n\n"; print OUTPUT "T:\t$number_t2\t$percent_t2\%\n\n"; print OUTPUT "G:\t$number_g2\t$percent_g2\%\n\n"; print OUTPUT "N:\t$number_n2\t$percent_n2\%\n\n"; } close (INPUT); close (OUTPUT); _____ output_____ Position#: 1048 Allele 1 Allele 1 percentage A: 0 0% C: 1 1.5625% T: 56 87.5% G: 0 0% N: 7 10.9375% Allele 2 Allele 2 percentage A: 0 0% C: 0 0% T: 57 89.0625% G: 0 0% N: 7 10.9375% etc....
Thanks for all your help!!
Bioinformatics

In reply to Finding elements without a counter... by bioinformatics

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.