Picking Element from Array one by one

R

Rita

Hi All,
I am getting some values from loop and i store all that value in array
using push function.
push @start,$start2;
push @end,$end2;
the start values are
68 801 968 40
and the End values are
320 830 995 56
then I sort values of array-
my @sort_start = sort { $a<=>$b } @start;
my @sort_end = sort { $a<=>$b } @end;
print @sort_start,"\n";
print @sort_end,"\n";

I am getting output
4068801968
56320830995

Is there any way that i can get output like this-
start -40
End - 56

start -68
End- 320

start-801
End -830

start-968
End-995
I cann't able to figure out that how i can pick elements of array one
by one.can you please help me?
Thanks.
 
J

Jürgen Exner

Rita said:
I cann't able to figure out that how i can pick elements of array one
by one.

Usually that is part of "Introduction to programming for beginners" in the
section that deals with aggregate data structures.
You simple use an index, e.g. $sort_start[5] or $sort_start[$i].

jue
 
R

Rita

Jürgen Exner said:
Rita said:
I cann't able to figure out that how i can pick elements of array one
by one.

Usually that is part of "Introduction to programming for beginners" in the
section that deals with aggregate data structures.
You simple use an index, e.g. $sort_start[5] or $sort_start[$i].

jue
yes I know that but i tried it but if I use $sort_start[5] this type of
thing then i have to give for every variable and i dont know some time
it has 2 start value some time 4 ,
and i tried foreach too but it giving me first all start value then all
end value though i want first start value then first end value then
second start value then second end value......
 
P

Paul Lalli

Rita said:
print @sort_start,"\n";
print @sort_end,"\n";

I am getting output
4068801968
56320830995

Is there any way that i can get output like this-
start -40
End - 56

start -68
End- 320

start-801
End -830

start-968
End-995
I cann't able to figure out that how i can pick elements of array one
by one.can you please help me?

Assuming, for the moment, that @start and @end have the same number of
elements, just iterate through the indices of one of them:
for my $i (0..$#sort_start){
print "start - $sort_start[$i]\n";
print "End - $sort_end[$i]\n";
}

Paul Lalli
 
M

Mahesh Asolkar

Rita said:
Hi All, ....
my @sort_start = sort { $a<=>$b } @start;
my @sort_end = sort { $a<=>$b } @end;
print @sort_start,"\n";
print @sort_end,"\n";

I am getting output
4068801968
56320830995

Is there any way that i can get output like this-
start -40
End - 56

start -68
End- 320

start-801
End -830

start-968
End-995
I cann't able to figure out that how i can pick elements of array one
by one.can you please help me?

Similar discusion happened in the following thread some days back:

http://tinyurl.com/8okzf

HTH,
Mahesh.
 
R

Rita

Paul said:
Paul Lalli
Thanks I am using for loop but i didn't think that both are same value
so i was using 2 loop one for start and one for end .
Thanks a lot.
 
P

Paul Lalli

Rita said:
Thanks I am using for loop but i didn't think that both are same value
so i was using 2 loop one for start and one for end .
Thanks a lot.

For a generalized solution, try using each_array, from the
List::MoreUtils module:

#!/usr/bin/perl
use strict;
use warnings;
use List::MoreUtils qw/each_array/;

my @start = (15, 25, 35, 45);
my @end = (18, 28, 38, 48);

my $each = each_array(@start, @end);
while (my ($start, $end) = $each->()){
print "Start - $start\n";
print "End - $end\n";
}

Paul Lalli
 
J

John Bokma

Rita said:
Hi All,
I am getting some values from loop and i store all that value in array
using push function.
push @start,$start2;
push @end,$end2;

How about:

push @values, {

start => $start2,
end => $end2
};
the start values are
68 801 968 40
and the End values are
320 830 995 56
then I sort values of array-
my @sort_start = sort { $a<=>$b } @start;
my @sort_end = sort { $a<=>$b } @end;
print @sort_start,"\n";
print @sort_end,"\n";

If the intervals are non-overlapping you could do:

@values = sort { $a->{ start } said:
I am getting output
4068801968
56320830995

Is there any way that i can get output like this-
start -40
End - 56

print "Start - $_->{ start }\nEnd - $_->{ end }\n"
for @values;
 
T

Tad McClellan

Rita said:
Jürgen Exner said:
Rita said:
I cann't able to figure out that how i can pick elements of array one
by one.

Usually that is part of "Introduction to programming for beginners" in the
section that deals with aggregate data structures.
You simple use an index, e.g. $sort_start[5] or $sort_start[$i].
yes I know that but i tried it but


If you post your broken code, then we can help you fix it.
 
R

robic0

Rita said:
Hi All,
I am getting some values from loop and i store all that value in array
using push function.
push @start,$start2;
push @end,$end2;
the start values are
68 801 968 40
and the End values are
320 830 995 56
then I sort values of array-
my @sort_start = sort { $a<=>$b } @start;
my @sort_end = sort { $a<=>$b } @end;
print @sort_start,"\n";
print @sort_end,"\n";

I am getting output
4068801968
56320830995

Is there any way that i can get output like this-
start -40
End - 56

start -68
End- 320

start-801
End -830

start-968
End-995
I cann't able to figure out that how i can pick elements of array one
by one.can you please help me?
Thanks.

"start2" and end2 shouldn't exist in a vacuum, thier linked somewhere.
A modified Bokma code:

use strict;
use warnings;

## -- what you want to get --
# 40 68 801 968
# 56 320 830 995

## -- what you have --
my @se_array = (68,320, 801,830, 968,995, 40,56);

my %StartEnd_hash = ();

for (my $i = 0; $i < @se_array; $i+=2) {
my ($start2, $end2) = @se_array[$i..($i+1)];
$StartEnd_hash{$start2} = $end2;
}

for (sort {$a <=> $b} keys %StartEnd_hash) {
print "start/end = $_,$StartEnd_hash{$_}\n";
}
__END__

output:

start/end = 40,56
start/end = 68,320
start/end = 801,830
start/end = 968,995
 
R

robic0

"start2" and end2 shouldn't exist in a vacuum, thier linked somewhere.
A modified Bokma code:

use strict;
use warnings;

## -- what you want to get --
# 40 68 801 968
# 56 320 830 995

## -- what you have --
my @se_array = (68,320, 801,830, 968,995, 40,56);
Please note that you have to have apriori knowledge as
to the nature of the link between start and end.
You can write code till you blue in the face, but
unless its got a strategy for sucess, theres no use
writing it.

You mentioned obtaining start2, end2 somewhere
else. Then you portray them as seperate entities
to be correlated later as if you can't link the
two at the time of accuisition. If thats the
case, and as you mention, sometimes theres
multiple start or end values, that totally
invalidates any coding effort whatsoever.
All you have is two dissimilar arrays that have
no correllation.

Please Rita, get it together woman.
 
R

robic0

Please note that you have to have apriori knowledge as
to the nature of the link between start and end.
You can write code till you blue in the face, but
unless its got a strategy for sucess, theres no use
writing it.

You mentioned obtaining start2, end2 somewhere
else. Then you portray them as seperate entities
to be correlated later as if you can't link the
two at the time of accuisition. If thats the ^^ s/cc/qu/
case, and as you mention, sometimes theres
multiple start or end values, that totally
invalidates any coding effort whatsoever.
All you have is two dissimilar arrays that have
no correllation.

Please Rita, get it together woman.
 
J

Jahagirdar Vijayvithal S

Rita said:
Hi All,
I am getting some values from loop and i store all that value in array
using push function.
try $start->{$start2}=$end2;# will work only of elements are not repeated
OR push $start->{$start2},$end2;# gets rid of restriction above.
then sort on keys of %{$start} and print key value pair
push @start,$start2;
push @end,$end2;
the start values are
68 801 968 40
and the End values are
320 830 995 56
then I sort values of array-
my @sort_start = sort { $a<=>$b } @start;
my @sort_end = sort { $a<=>$b } @end;
print @sort_start,"\n";
print @sort_end,"\n";

I am getting output
4068801968
56320830995

Is there any way that i can get output like this-
start -40
End - 56

start -68
End- 320

start-801
End -830

start-968
End-995
I cann't able to figure out that how i can pick elements of array one
by one.can you please help me?
Thanks.

Regards
Jahagirdar Vijayvithal S
 
R

robic0

On Thu, 1 Dec 2005 19:22:40 +0530, Jahagirdar Vijayvithal S

Don't follow up my post, I never quoted any of this shit asshole!
 

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,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top