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

Array Element Deletion

by rpike (Scribe)
on Dec 04, 2008 at 21:01 UTC ( [id://728097]=perlquestion: print w/replies, xml ) Need Help??

rpike has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have an a hash and each element has an array ($hash{"SomeName"}[0], $hash{"SomeName"}2, etc..,). How can I delete a specific element from the array? How can I have it so that the last element extracted makes $#hash{"SomeName"} -1? I'm aiming to have a hash that has keys that are dates and in each have an array of values which I want to randomly select from and, once selected, delete it from the array. I'd appreciate any help you can give on this. Thanks in advance. Rob

Replies are listed 'Best First'.
Re: Array Element Deletion
by ikegami (Patriarch) on Dec 04, 2008 at 21:24 UTC
    If you'd normally usethen use
    @a = grep { ... } @a@{$h{$k}} = grep { ... } @{$h{$k}}
    splice(@a, ...)splice(@{$h{$k}}, ...)
    etc
Re: Array Element Deletion
by kennethk (Abbot) on Dec 04, 2008 at 21:15 UTC
Re: Array Element Deletion
by GrandFather (Saint) on Dec 04, 2008 at 22:24 UTC
    use strict; use warnings; use Data::Dump::Streamer; my %hash = ( somename => [1 .. 3], othername => [reverse -3 .. -1] ); Dump (\%hash); splice @{$hash{somename}}, 1, 1; Dump (\%hash);

    Prints:

    $HASH1 = { othername => [ -1, -2, -3 ], somename => [ 1, 2, 3 ] }; $HASH1 = { othername => [ -1, -2, -3 ], somename => [ 1, 3 ] };

    Perl's payment curve coincides with its learning curve.
Re: Array Element Deletion
by zentara (Archbishop) on Dec 04, 2008 at 21:09 UTC
    You should make a running example, with proper code syntax.....your hash description is invalid code and confusing.

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Array Element Deletion
by rpike (Scribe) on Dec 04, 2008 at 21:14 UTC
    I'll put it this way then. I have a hash whereby it has a key for each date in a range. Each date could have multiple people assigned. So I might have a hash entry like this :
    $hash{"DEC012008"}[0] = "Mike"; $hash{"DEC012008"}[1] = "Steve"; etc..,
    I want to randomly select one of the elements in the array assigned to the hash entry DEC012008 and then remove it from that array so, if I come back to that date, I don't reselect a person that was already selected.
      It always pays to take a look onto when searching for a solution. It looks to me that Tie::Pick is exacly what you want.

      As for the list reference you might need, it's no crime to dereference it explicitely in order to use simpler structures , like say
      my $candidates_ref = $hash{"DEC012008"};
      Hth

      Krambambuli
      ---
Re: Array Element Deletion
by matrixmadhan (Beadle) on Dec 05, 2008 at 05:42 UTC
    For the last element from an array reference you could try

    $arr->[-1]

    instead of finding the scalar of de-referred array reference and subtracting 1 from that.

    This is something that I learned here yesterday

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://728097]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 06:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found