in reply to Re: using reference to subroutine
in thread using reference to subroutine

Sorry, wrong version, the code I meant to post was:
#!/usr/bin/perl $fileName = "tmp.txt"; open(CPF, $fileName) or die ("Can't open file ", $fileName); @lines = <CPF>; close (CPF); $index = 0; @allNames; @allSeq; $nbTaxa; $nbGenes; #$lastLine = "start"; $alternatingIter = sub {return $_[0]}; $followedIter = sub {return (($_[0] % 10) - 1) }; $iter; chop $lines[0]; ($nbTaxa,$nbGenes)=split / +/, $lines[0]; print $nbTaxa." ".$nbGenes."\n"; &sorter ($lines[1]); ($first, $second)= split / +/, $lines[2]; if (((length $second) == 0) && ($first =~ /[AGTC]+/)){ $iter = $followedIter; chop ($first); $allSeq[$index]= $allSeq[$index].$first; print "TYPE: just sequence --> "; print "first:".$first."; second:".$second.";\n"; } elsif (((length $second) > 0) && ($first =~ /(\w)+/) && ($second =~ / +[AGTC]+/) ){ $iter = $alternatingIter; $allNames [$index] = $first; chop ($second); $allSeq [$index] = $second; $index++; print "TYPE: name + sequence --> "; print "first:".$first."; second:".$second.";\n"; } foreach $tmp (@lines[3..@lines]){ &sorter ($tmp, $iter); } for ($i = 0; $i <= $index; $i++){ print $allNames[$i]." --> ".$allSeq[$i]."\n"; } sub sorter { my $tmp = $_[0]; my $iter = $_[1]; ($first, $second)= split / +/, $tmp; if (((length $second) == 0) && ($first =~ /[AGTC]+/)){ chop ($first); $allSeq[$iter]= $allSeq[$index].$first; $index++; print "TYPE: just sequence --> "; print "first:".$first."; second:".$second.";\n"; } elsif (((length $second) > 0) && ($first =~ /(\w)+/) && ($second +=~ /[AGTC]+/) ){ $i = &{$iter}($index); # ou $iter->($index); $allNames [$i] = $first; chop ($second); $allSeq [$i] = $second; $index++; print "TYPE: name + sequence --> "; print "first:".$first."; second:".$second.";\n"; } elsif($first eq "\n"){ print "TYPE: do nothing --> "; print "first:".$first."; second:".$second.";\n"; } else{ print "TYPE: you have a problem with the format of file".$file +Name." --> "; print "first:".$first."; second:".$second.";\n"; } print "index: ".$index."\n"; }
Here is the tmp.txt file you should use as input
10 705 Cow ATGGCATATCCCATACAACTAGGATTCCAAGATGCAACATCACCAATCATAGAAGAACTA Carp ATGGCACACCCAACGCAACTAGGTTTCAAGGACGCGGCCATACCCGTTATAGAGGAACTT Chicken ATGGCCAACCACTCCCAACTAGGCTTTCAAGACGCCTCATCCCCCATCATAGAAGAGCTC Human ATGGCACATGCAGCGCAAGTAGGTCTACAAGACGCTACTTCCCCTATCATAGAAGAGCTT Loach ATGGCACATCCCACACAATTAGGATTCCAAGACGCGGCCTCACCCGTAATAGAAGAACTT Mouse ATGGCCTACCCATTCCAACTTGGTCTACAAGACGCCACATCCCCTATTATAGAAGAGCTA Rat ATGGCTTACCCATTTCAACTTGGCTTACAAGACGCTACATCACCTATCATAGAAGAACTT Seal ATGGCATACCCCCTACAAATAGGCCTACAAGATGCAACCTCTCCCATTATAGAGGAGTTA Whale ATGGCATATCCATTCCAACTAGGTTTCCAAGATGCAGCATCACCCATCATAGAAGAGCTC Frog ATGGCACACCCATCACAATTAGGTTTTCAAGACGCAGCCTCTCCAATTATAGAAGAATTA CTTCACTTTCATGACCACACGCTAATAATTGTCTTCTTAATTAGCTCATTAGTACTTTAC CTTCACTTCCACGACCACGCATTAATAATTGTGCTCCTAATTAGCACTTTAGTTTTATAT GTTGAATTCCACGACCACGCCCTGATAGTCGCACTAGCAATTTGCAGCTTAGTACTCTAC ATCACCTTTCATGATCACGCCCTCATAATCATTTTCCTTATCTGCTTCCTAGTCCTGTAT CTTCACTTCCATGACCATGCCCTAATAATTGTATTTTTGATTAGCGCCCTAGTACTTTAT ATAAATTTCCATGATCACACACTAATAATTGTTTTCCTAATTAGCTCCTTAGTCCTCTAT ACAAACTTTCATGACCACACCCTAATAATTGTATTCCTCATCAGCTCCCTAGTACTTTAT CTACACTTCCATGACCACACATTAATAATTGTGTTCCTAATTAGCTCATTAGTACTCTAC CTACACTTTCACGATCATACACTAATAATCGTTTTTCTAATTAGCTCTTTAGTTCTCTAC CTTCACTTCCACGACCATACCCTCATAGCCGTTTTTCTTATTAGTACGCTAGTTCTTTAC

Replies are listed 'Best First'.
Re^3: using reference to subroutine
by cdarke (Prior) on Apr 05, 2007 at 15:05 UTC
    Several problems here. Use warnings and strict!
    Your error is because you are calling 'sorter' on line 26 with only one argument - you are not passing it an '$iter'.
    It is a bad idea to have a local variable and a global of the same name - very confusing ($iter).
    The syntax for calling the ref should be &$iter($index)
    I also spotted that your foreach loop is wrong, use $#lines for the highest index, @lines will go one element beyond the end of the array (it give s the number of elements).
    Probably more errors lurking, but that should do for now.
    update: $allSeq[&$iter()]= $allSeq[$index].$first;I really hate your use of globals in the subroutine, sorry, but it makes the code difficult to figure out. Should your $first and $second in sorter() overwrite the globals?
      The syntax for calling the ref should be &$iter($index)

      Well, also $iter->($index).

      Thanks a lot!

      The strict module is really great! I'm not such a big fan of warnings, it makes the program crash and I have no idea why. but at this point I changed the code a lot so maybe the bug is noteven up there

      Also, how do I make simple paragraphs in these texts boxes? So far I only can up with the p tag but it does double paragraph :(

        The strict module is really great! I'm not such a big fan of warnings, it makes the program crash and I have no idea why.

        warnings can't make your program "crash". It will spit out, ehm... warnings. Generally this is good, because it is an indication that you may be doing something wrong, and you'd better take care of correcting that. OTOH in the rare situations in which you feel a particular warning is unjustified, you can still locally disable it.