Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

sort while push in hash

by Murcia (Monk)
on Apr 07, 2004 at 13:40 UTC ( #343275=perlquestion: print w/replies, xml ) Need Help??
Murcia has asked for the wisdom of the Perl Monks concerning the following question:

Hi Conferes, if i push an element in push( @{$hash{'a'}}, $ele); # automatically sorted?
can this be sorted while the push?
$hash{'a'} = [ 1, 2, 4, 5, 6];

i want to insert an element '3' not at the end, but after 2.
Thanks
Murcia

Replies are listed 'Best First'.
Re: sort while push in hash
by gjb (Vicar) on Apr 07, 2004 at 14:24 UTC

    You might be interested in Tie::Array::Sorted which keeps the array sorted while you add things to it.

    However, using autovivication won't work: if the array for a certain key doesn't exist yet, you'll have to create the tied array first (unless there's a clever trick I don't know).

    Hope this helps, -gjb-

Re: sort while push in hash
by Fletch (Chancellor) on Apr 07, 2004 at 14:20 UTC

    If you maintain the invariant that the hash starts out sorted just scan and insert it.

    sub sorted_insert (\@$) { my( $a, $item ) = @_; unless( @{$a} ) { push @{$a}, $item; return } my $i = 0; while( $i <= $#{$a} ) { last if $a->[$i] > $item; $i++; } splice( @{$a}, $i, 0, $item ); return $a } @a = qw( 1 2 4 6 ); sorted_insert @a, 3; sorted_insert @a, 7; print join( ", ", @a ), "\n";
Re: sort while push in hash
by tinita (Parson) on Apr 07, 2004 at 14:01 UTC
    based on your vague input:
    @{ $hash{a} } = sort (@{ $hash{a} }, $ele);
    (which might be inefficient if you do this often; then i'd rather sort at the end)
Re: sort while push in hash
by Art_XIV (Hermit) on Apr 07, 2004 at 14:16 UTC

    If you want to insert an element into an array at a certain position, then check out the splice function in your docs.

    shift,unshift, push and pop add or remove elements from the beginning and end of an array, respectively.

    Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"
Re: sort while push in hash
by borisz (Canon) on Apr 07, 2004 at 13:46 UTC
    No, push puts the element at the end of the array.
    Boris
Re: sort while push in hash
by pelagic (Priest) on Apr 07, 2004 at 13:46 UTC
    What exactly do ypu want sorted?
    The Hash (in this case consider node Re: How to sort HASH? ) or the Array within each Hashelement?

    pelagic
      Hi, Monks I know how to sort, but sometimes are there clever tricks around to do things in one step Thanks Murcia
Re: sort while push in hash
by eserte (Deacon) on Apr 08, 2004 at 09:01 UTC
    Looks like you need a heap instead. Look for the Heap:: modules at CPAN (or, my preferred choice, Array::Heap2).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://343275]
Approved by broquaint
Front-paged by sgifford
help
Chatterbox?
[jrmcc]: Your problem was pregnant, missing a period!

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (13)
As of 2018-05-22 16:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?