Question: (Closed) perl programming error
0
gravatar for kriti.awasthi23
3.9 years ago by
kriti.awasthi2310 wrote:

hi everyone,

I am trying to make a program in which I am making a subroutine and passing(returning) its values into a array and then printing it. Actually the values which are being passed are read by an scalar variable what I am not able to do is to store it in an array afterwards or maybe something else.

the program is as follows-

@a=data2();
print " the array is @a \n";
division(@a);
sub division(@a){
($tcount)=@a;
print "in division array is first $tcount \n";
  }
sub data2(){
   open (FH,"dataset2.txt");
   %seq_dt2=<FH>;
   @index2=keys%seq_dt2;
   print "@index2";
   $len2=@index2;
   print "$len2";
 $occ_f_6len_in_1000bp_seqfile=995;
 $total_occurance= ($occ_f_6len_in_1000bp_seqfile * $len2)/6;
 print "ref ka occurance h ye $total_occurance";
 @seq2=values%seq_dt2;
 chomp (@seq2);
  print "@seq2";
  open (FILE,"Motif6.txt");
   @motif=<FILE>;
   chomp @motif ;
    print " @motif\n";
   foreach $motif (@motif) {
   foreach $seq2 (@seq2) {
   if ($seq2 =~ /$motif/) {
   $tcount++;
   }
  }
 print "$motif $tcount \n";
 print " $tcount \n";
  return ("$tcount");
  $tcount =0;
   close (FH);
    }

my dataset2 file is -

  >1
  ATGCATAAAAAAATCGACTAGCTACGATCGATCAGAAAAACACAC
  >2
  CAGCTAAAAAGCTACGAAAAAAATCGATAGCTAGAAAAACA

my motif6 file is-

  AAAAAA
  AAAAAT
  AAAAAG
  AAAAAC

the output which m getting is -

the array is 2
in division array is first 2

the desired output which i want is-

   the array is 2,2,1,2
   in division array is first 2,2,1,2
  AAAAAA   2
  AAAAAT   2
  AAAAAG   1
  AAAAAC   2
programming perl error perl • 911 views
ADD COMMENTlink modified 3.9 years ago by shenwei3565.6k • written 3.9 years ago by kriti.awasthi2310
2

Your script is incorrect, it does not use strict or warnings! Non-strict scripts cannot be debugged efficiently. Also the example is lacking any real relation to bioinformatics, please point those out.

Please turn on strict and warnings, then the errors will become more obvious. Start your script like this:

#!/usr/bin/env perl
use strict;
use warnings;
ADD REPLYlink modified 3.9 years ago • written 3.9 years ago by Michael Dondrup48k
1

sir, thankyou for pointing it......i will surely start doing it from now onwards. I just want this subroutine to return values in the @a which I called above, now what sort of bioinformatics connection you are trying yo ask is beyond my understanding. Its simply a programming mistake which m not able to debug how ever i will try using strict and warning right now.

ADD REPLYlink written 3.9 years ago by kriti.awasthi2310

The way your code is written, your subroutine is returning a scalar value,

return ("$tcount");

not an array. If you want to return an array, store the values you want to return in an array variable.

I think the problem is that you are expecting the subroutine to return one value at a time, whereas once the program executes the return statement, control transfers to the main script, and will not continue executing the subroutine. What you need to do is store your results in an array inside the subroutine, then, at the end, return that array.

return ("@tcounts");
ADD REPLYlink modified 3.9 years ago • written 3.9 years ago by mastal5112.1k
3

Err, no!

return ("@tcounts"); # returns string interpolation of an array

but

return (@tcounts);
ADD REPLYlink written 3.9 years ago by Michael Dondrup48k

Hello kriti.awasthi23!

We believe that this post does not fit the main topic of this site.

Not a bioinformatics question according to OP.

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

ADD REPLYlink written 3.9 years ago by Michael Dondrup48k

Sir, I am working on some motif prediction projects in which I have to use programming and I am making a program to match motifs from the data set file and call them and build calculations for certain methods! This is pure bioinformatics work sir if it would not have been a bioinformatics query sir I wouldn't have posted it here! I am a beginner and I have to learn alot from your elder people guidance!

ADD REPLYlink written 3.9 years ago by kriti.awasthi2310
1

That is irrelevant, you have received at least one working solution (plus 2-3 alternative approaches and recommendations to use strict; use warnings; which you did not implement) to your last question perl programming query stating the identical problem, if you have further comments on the solution for the exact same problem there, please attend the first question. In your above comment you further state yourself that this is not a bioinformatics question, but pure programming. If I understand the core question correctly, then it is essentially "how do I return an array from a subroutine in perl?", which I do agree with you is basic perl programming. You will find many answers by searching the exact sentence I quote above.

ADD REPLYlink modified 3.9 years ago • written 3.9 years ago by Michael Dondrup48k

Sir, Thankyou for your suggestions.I will keep all the important points in my mind.

ADD REPLYlink written 3.9 years ago by kriti.awasthi2310
1
gravatar for shenwei356
3.9 years ago by
shenwei3565.6k
China
shenwei3565.6k wrote:

That's interesting, you've posted a page 20 days ago for the almost the same question.

perl programming query

Please review that post carefully.

ADD COMMENTlink written 3.9 years ago by shenwei3565.6k

Sir, if you would have gone through my previous page seriously i was asking a different question there though i posted a same sample file but now m stuck up with the subroutine calling, which i wanted to seek guidance from learned people like you! The previous error got solved! I thankyou for actively taking a look at my post and helping me sorting them!

ADD REPLYlink written 3.9 years ago by kriti.awasthi2310

Besides, always format Perl code with perltidy command manually or by IDE.

People would like to not read Perl code with bad format.

ADD REPLYlink written 3.9 years ago by shenwei3565.6k
Please log in to add an answer.
The thread is closed. No new answers may be added.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 2022 users visited in the last hour