How I can store some value in loop?

R

Rita

Hi ,
I Have Some Values which ih running in loop and every time is giving
different value . I want to store that all different value in
different variable.
like
Start -34
End -267

Start -1068
End -1248

Start -824
End -917
This is my values which i get from loop and I want to use this like
Start1,End1,Start2,End2,Start3,End3.


How I can do this?
thanks
 
I

it_says_BALLS_on_your_forehead

Rita said:
Hi ,
I Have Some Values which ih running in loop and every time is giving
different value . I want to store that all different value in
different variable.
like
Start -34
End -267

Start -1068
End -1248

Start -824
End -917
This is my values which i get from loop and I want to use this like
Start1,End1,Start2,End2,Start3,End3.

you could use two arrays...
#!/usr/bin/perl

use strict; use warnings;

my ( @start, @end );

while ( <DATA> ) {
chomp;
next if /^\s*$/;
/Start/ and push @start, (split)[1];
/End and push @end, (split)[1];
}


__DATA__
Start -34
End -267

Start -1068
End -1248

Start -824
End -917
 
I

it_says_BALLS_on_your_forehead

it_says_BALLS_on_your_forehead said:
Rita said:
Hi ,
I Have Some Values which ih running in loop and every time is giving
different value . I want to store that all different value in
different variable.
like
Start -34
End -267

Start -1068
End -1248

Start -824
End -917
This is my values which i get from loop and I want to use this like
Start1,End1,Start2,End2,Start3,End3.

you could use two arrays...
#!/usr/bin/perl

use strict; use warnings;

my ( @start, @end );

while ( <DATA> ) {
chomp;
next if /^\s*$/;
/Start/ and push @start, (split)[1];
/End and push @end, (split)[1];
....sorry, the above line should be:
/End/ and push @end, (split)[1];
 
U

usenet

Rita said:
This is my values which i get from loop and I want to use this like
Start1,End1,Start2,End2,Start3,End3.

Do you REALLY want to name the variables like this? You would need to
do something like ${"start$_"} in your loop, which is something called
a "symbolic reference" and it will cause the earth to explode (really).

Why not use a hash and organize the data like: $start{1}, $end{3},
etc?
(or maybe $data{start}{1}, $data{end}{3}, etc)

for example:

#!/usr/bin/perl
my (%start, %end);
for (1..3) { # loop three times
$start{$_} = rand; #a random number - just for demo
$end{$_} = rand;
}
__END__

If you do it like that, the earth is safe.
 
R

Rita

#!/usr/bin/perl

use strict; use warnings;

my ( @start, @end );

while ( <DATA> ) {
chomp;
next if /^\s*$/;
/Start/ and push @start, (split)[1];
/End and push @end, (split)[1];
...sorry, the above line should be:
/End/ and push @end, (split)[1];
But my problam is i am getting value from Loop using Module so How i
can use while(<DATA>) bcoz that all value i want to store.
you know what i mean.
thanks
 
I

it_says_BALLS_on_your_forehead

Rita said:
#!/usr/bin/perl

use strict; use warnings;

my ( @start, @end );

while ( <DATA> ) {
chomp;
next if /^\s*$/;
/Start/ and push @start, (split)[1];
/End and push @end, (split)[1];
...sorry, the above line should be:
/End/ and push @end, (split)[1];
But my problam is i am getting value from Loop using Module so How i
can use while(<DATA>) bcoz that all value i want to store.
you know what i mean.

no, i do not know what you mean. to which module are you referring?
 
R

Rita

no, i do not know what you mean. to which module are you referring?
I am trying to make Program of BioInformatics .so I am using
$hsp->sbjct->start; Method Via Bio::SearchIO Module.
 
R

Rita

No. Show us your code and a sample of your data.
you want to see my code but my all code is related to each other.
and my programm is so big if I show you i know you wouldnot give me
response i already tried it .may be my programm is not as good as you
guys but still i am trying to make something.
If you want to see my code no problam but tell me whole Programm or
just for this problam.
Thanks
 
U

usenet

Rita said:
and my programm is so big...

You're right, we probably don't want to see your entire program.

But can you write a small program that isolates just what you want to
do (using dummy data, not the Bio module)? If you can show us a small
example, we can understand better.

I THINK you are asking a question like this:

I have three values that I want to assign to $start1, $start2, and
$start3. But the values are assigned inside of a loop. Here's the
program:

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

my $count = 0;
for (<DATA>) {
$count++; #this shows how many times the loop has looped
#what goes here to make $start1 == 123?
}
__DATA__
123 654 789


The answer to your question (if that is your question) is that you're
not using a good data structure. If you want to assign a lot of
variables in a loop, it is not good to use individual scalar variables
(like $start1, $start2, etc) - it is better to use some sort of array,
because arrays can hold many scalar values.

So your program would look something like this (using an associative
array, AKA a hash):

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

my $count = 0;
my %start;
for (<DATA>) {
$count++; #this shows how many times the loop has looped
$start{$count} = $_;
}
__DATA__
123 654 789

# now $start{2} == 654

Or like this (using a "regular" array)

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

my $count = 0;
my @start;
for (<DATA>) {
$count++; #this shows how many times the loop has looped
push @start, $_;
}
__DATA__
123 654 789

#now $start[2] == 654
 
I

it_says_BALLS_on_your_forehead

Rita said:
you want to see my code but my all code is related to each other.
and my programm is so big if I show you i know you wouldnot give me
response i already tried it .may be my programm is not as good as you
guys but still i am trying to make something.
If you want to see my code no problam but tell me whole Programm or
just for this problam.
Thanks

it is much more difficult to help you if you don't give us any
information. the best information is your code, with the output that
you expect, and the output that you are getting.
 
T

Tad McClellan

Rita said:
Start -34
End -267

Start -1068
End -1248

Start -824
End -917
This is my values which i get from loop and I want to use this like
Start1,End1,Start2,End2,Start3,End3.


How I can do this?


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

my $str;
{ local $/ = '';
while ( <DATA> ) {
$str .= "Start$.,End$.,"
}
chop $str;
}

print "str is '$str'\n";

__DATA__
Start -34
End -267

Start -1068
End -1248

Start -824
End -917
-------------------------------



You're welcome.
 
I

it_says_BALLS_on_your_forehead

Tad said:
-------------------------------
#!/usr/bin/perl
use warnings;
use strict;

my $str;
{ local $/ = '';
while ( <DATA> ) {
$str .= "Start$.,End$.,"
}
chop $str;
}

print "str is '$str'\n";

__DATA__
Start -34
End -267

Start -1068
End -1248

Start -824
End -917
-------------------------------




You're welcome.

come on Tad, that's just mean, you know what her intent is ;-).
 
T

Tad McClellan

it_says_BALLS_on_your_forehead said:
come on Tad, that's just mean,


Some people learn by being told.

Others learn by discovering for themselves...

you know what her intent is ;-).


_my_ intent was that she spend the appropriate amount of
care in specifying what it is that she wants.

We are not her garbage dump.
 
T

Tad McClellan

it_says_BALLS_on_your_forehead said:
come on Tad, that's just mean, you know what her intent is ;-).


If she had said:

and I want to use this like
Start -34,End -267,Start -1068,End -1248,Start -824,End -917

her intent would have been clear. (but she didn't)


If she had said that, then I would have said:

1) there is probaby a better data structure than a big ol' string

2) maybe this code does what was wanted:


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

my $str;
{ local $/ = '';
while ( <DATA> ) {
tr/\n//s;
tr/\n/,/;
$str .= $_;
}
chop $str;
}

print "str is '$str'\n";

__DATA__
Start -34
End -267

Start -1068
End -1248

Start -824
End -917
 
R

Rita

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

my $str;
{ local $/ = '';
while ( <DATA> ) {
tr/\n//s;
tr/\n/,/;
$str .= $_;
}
chop $str;
}

print "str is '$str'\n";

__DATA__
Start -34
End -267

Start -1068
End -1248

Start -824
End -917


No you all are giving me code for store data in 1 string or whatever
but my question is not for that, You guys are considering that i
already have value of start and end but I am getting that value from
loop and i want to store in different variable .
as you all know that in my all programm i have 2 sequences
like
$seq1="AACTGACGACTTTATATACACTAAACTACAATTAAATGGAAGACAGATAATTTAAGAGAAAAAGATTCTTCATATGCATTCTAATTTAGGCAATTTAACATCTAGATCTCGACATAGAGATTGAC";
and
$seq2="CGAGCTGGCAGAGTTCACATGGACAAACAAGGATGCCTGGTTTCTTCATATGCATTCTTGCTGCCCTATACTGTTCAATCCATTCTGCTCAAACCACTATCGTCGACATAGAGATTGACT";

I write both sequences in temperary file called temp1 and temp 2 then
I aligned both sequence using Blast program then i use this code
my $in = Bio::SearchIO ->new(-format => 'blast',
-file => 'temp.blast');
#Print Starting and Ending Point of 1 Hsps-

my $result=$in->next_result;
my $hit =$result->next_hit;

my $hsp1 =$hit->next_hsp;
my$start1=$hsp1->query->start;
my $end1=$hsp1->query->end;

print "start First Hsp ",$start1,"\n";
print "End First Hsp ",$end1,"\n\n";

#Print Starting and Ending Point of 2 Hsps-

my $hsp2 =$hit->next_hsp;
my$start2=$hsp2->query->start;
my $end2=$hsp2->query->end;

print "start Second Hsp ",$start2,"\n";
print "End Second Hsp ",$end2,"\n\n";

#Print Gap Sequence
print "Gap Sequence- ",$seq1->subseq($end1+1,$start2-1);
This program is working and giving me output what i want

output is
Start First Hsp 66
End First Hsp 82

Start Second Hsp 109
End Second Hsp 125

Gap Sequence- AATTTAGGCAATTTAACATCTAGATC
But now i generalized that program and in this program I am giving file
name it's taking all sequences from this file and Aligning it but some
sequences has no gap some has 2 gap some has 3....and my code is good
for finding 1 gap Sequences.
that's why i want to store all values in different variable so then i
can use that in subseq function .
Hope now it will give you clear look.
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top