develooper Front page | perl.beginners | Postings from April 2003

Re: multiple sorts

Thread Previous | Thread Next
From:
Kevin Pfeiffer
Date:
April 30, 2003 07:42
Subject:
Re: multiple sorts
Message ID:
1073367.OZRaPhyDs9@sputnik.tiros.net
In article <1269940.qjuo34Iz3K@sputnik.tiros.net>, Kevin Pfeiffer wrote:

> In article <3EAEF939.2A238EE4@acm.org>, John W. Krahn wrote:
> [...]
>> You don't need a loop for that:
>> 
>>     my @tmp = splice @AoA, 0, $hdr_flag;
>> 
> 
> One more quick question - on the back end I put these back into @AoA:
> 
>    if ($hdr_flag) {
>       foreach (@tmp) {
>          unshift @AoA, $_;
>          unshift @sorted_AoA, $_;
>       }
>    }
> 
> Might there be an easier way here as well (an "unsplice")? :-)
> 
> 

I decided I should first reread splice (see my attempt below), but then 
found that in this simple test that unshift was perfectly happy to put the 
two items back in:

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

my @array = qw(
                apple
                pear
                orange
                peach
                grape
                );

my @temp = splice @array, 0, 2;

print "Here is \@temp: @temp\n";
print "Here is \@array: @array\n";

# didn't work - LENGTH 0 removes everything!
#@array = splice @array, 0, 0, @temp;

# but this does:
unshift @array, @temp;

print "Here is \@temp: @temp\n";
print "Here is \@array: @array\n";

exit;

-K
-- 
Kevin Pfeiffer
International University Bremen

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About