Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Picking Element from Array one by one

0 views
Skip to first unread message

Rita

unread,
Nov 30, 2005, 10:14:35 AM11/30/05
to
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ürgen Exner

unread,
Nov 30, 2005, 10:25:49 AM11/30/05
to
Rita wrote:
> 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


Rita

unread,
Nov 30, 2005, 10:49:37 AM11/30/05
to
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......

Paul Lalli

unread,
Nov 30, 2005, 10:55:52 AM11/30/05
to
Rita wrote:

> 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

Mahesh Asolkar

unread,
Nov 30, 2005, 11:17:31 AM11/30/05
to
Rita wrote:
> 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.

Rita

unread,
Nov 30, 2005, 11:24:14 AM11/30/05
to

Paul Lalli wrote:
> 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.

Paul Lalli

unread,
Nov 30, 2005, 12:28:38 PM11/30/05
to

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

John Bokma

unread,
Nov 30, 2005, 1:39:06 PM11/30/05
to
"Rita" <ritu_...@yahoo.co.in> wrote:

> 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 } <=> $b->{ start } } @values;

> 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;

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com :-)

Tad McClellan

unread,
Nov 30, 2005, 4:56:08 PM11/30/05
to
Rita <ritu_...@yahoo.co.in> wrote:
> Jürgen Exner wrote:
>> Rita wrote:
>> > 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.


--
Tad McClellan SGML consulting
ta...@augustmail.com Perl programming
Fort Worth, Texas

rob...@yahoo.com

unread,
Nov 30, 2005, 10:00:12 PM11/30/05
to

"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

robic0

unread,
Dec 1, 2005, 1:57:30 AM12/1/05
to
On 30 Nov 2005 19:00:12 -0800, "rob...@yahoo.com" <rob...@yahoo.com>
wrote:

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.


robic0

unread,
Dec 1, 2005, 2:36:27 AM12/1/05
to

^^ s/cc/qu/

Jahagirdar Vijayvithal S

unread,
Dec 1, 2005, 8:52:40 AM12/1/05
to
Rita wrote:
> 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

Regards
Jahagirdar Vijayvithal S
--
Is OS/2 only half an operating system?
Jahagirdar .V.S 91-80-25099129(O) 91-80-28540394(R)
IC Design Engineer RGES-WLAN,
Texas Instruments (India) Ltd.

robic0

unread,
Dec 3, 2005, 4:21:07 AM12/3/05
to
On Thu, 1 Dec 2005 19:22:40 +0530, Jahagirdar Vijayvithal S
<j...@india.ti.com> wrote:

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

Jahagirdar Vijayvithal S

unread,
Dec 5, 2005, 2:24:27 AM12/5/05
to
* robic0 <robic0> wrote:
> On Thu, 1 Dec 2005 19:22:40 +0530, Jahagirdar Vijayvithal S
> <j...@india.ti.com> wrote:
>
> Don't follow up my post, I never quoted any of this shit asshole!
Duh! if you say so.
http://groups.google.com/group/comp.lang.perl.misc/browse_thread/thread/2ea159e6f5b383a1/881332f07aa6be68?lnk=st&q=Re%3A+Picking+Element+from+Array+one+by+one&rnum=1#881332f07aa6be68http://groups.google.com/group/comp.lang.perl.misc/browse_thread/thread/2ea159e6f5b383a1/881332f07aa6be68?lnk=st&q=Re%3A+Picking+Element+from+Array+one+by+one&rnum=1#881332f07aa6be68

Regards
Jahagirdar Vijayvithal S
--
Why doesn't DOS ever say "EXCELLENT command or filename!"

0 new messages