Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

removing element from hash array

by jithoosin (Scribe)
on Nov 16, 2005 at 13:47 UTC ( #509030=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks,
I want to remove an element from a hash array , for eg:
'list' => { 'find' => [ { 'type' => 'error', 'column' => '106', }, { 'type' => 'warning', 'column' => '1', } ]
Could somebody help me how to remove the second array element . That is
{ 'type' => 'warning', 'column' => '1', }
should be removed.

Janitored: Copied ownership from non-anonymous reaped dup

Replies are listed 'Best First'.
Re: removing element from hash array
by wfsp (Abbot) on Nov 16, 2005 at 14:04 UTC

    or:

    #!/bin/perl5 use strict; use warnings; use Data::Dumper; my %h = ( 'list' => { 'find' => [ { 'type' => 'error', 'column' => '106', }, { 'type' => 'warning', 'column' => '1', } ] } ); delete $h{list}{find}[1]; die Dumper(\%h); __DATA__ ---------- Capture Output ---------- > "c:\perl\bin\perl.exe" _new.pl $VAR1 = { 'list' => { 'find' => [ { 'type' => 'error', 'column' => '106' } ] } }; > Terminated with exit code 255.
      Weird. All this time I thought delete only worked on hashes and you had to use splice for arrays, but I was obviously wrong.
Re: removing element from hash array
by holli (Abbot) on Nov 16, 2005 at 13:59 UTC
    use strict; use warnings; use Data::Dumper; my %h = ( 'list' => { 'find' => [ { 'type' => 'error', 'column' => '1', }, { 'type' => 'warning', 'column' => '2', }, { 'type' => 'warning', 'column' => '3', } ] }, ); print Dumper (\%h); #use pop for last element #pop @{$h{list}->{find}}; #use slice for any other element @{$h{list}->{find}} = @{$h{list}->{find}}[0,2]; print Dumper (\%h);


    holli, /regexed monk/
Re: removing an element of a array which is element of hash
by merlyn (Sage) on Nov 16, 2005 at 14:29 UTC
    Since you didn't tell me the name of your top hash, I'm calling it %UNNAMED_TOP_THINGY in the following code:
    @$_ = grep {not $_->{type} eq "warning"} @$_ for $UNNAMED_TOP_THINGY{l +ist}{find};
    I also don't know from your example whether you want to (a) remove all "warning" (which is my code) or (b) keep all "error". It would be better if you had a better example, or a text description of what you had wanted.

    I mean, based on your example, this would also have worked:

    %UNNAMED_TOP_THINGY = ( 'list' => { 'find' => [ { 'type' => 'error', 'column' => '106', } ] } );
    But I doubt that would have satisfied you. {grin}

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.


    update: Aha... see, I would have never thought that you wanted items with column = 1 removed. OK, here's that:
    @$_ = grep {not $_->{column} == 1} @$_ for $UNNAMED_TOP_THINGY{list}{f +ind};
Re: removing element from hash array
by Samy_rio (Vicar) on Nov 16, 2005 at 14:06 UTC

    Hi, Try this,

    use warnings; use strict; use Data::Dumper; my %hoh; %hoh = ('list' => { 'find' => [ { 'type' => 'error', 'column' => '106', }, { 'type' => 'warning', 'column' => '1', } ]}); delete ($hoh{'list'}->{'find'}[1]->{'type'}); delete ($hoh{'list'}->{'find'}[1]->{'column'}); print Dumper (\%hoh); __END__ $VAR1 = { 'list' => { 'find' => [ { 'type' => 'error', 'column' => '106' }, {} ] } };

    Updated :This will delete full second array element. Above code will delete particular element in array.

    delete ($hoh{'list'}->{'find'}[1]);

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: removing an element of a array which is element of hash
by Moron (Curate) on Nov 16, 2005 at 14:40 UTC
    There are an infinite number of rules for which this might be an example, including:

    - "keep only the first one"

    - "delete only last one"

    - "consolidate to the highest column and lowest type"

    - "if the array size is odd numbered remove the last one, otherwise remove all those whose 11-bit checkdigit of the type doesn't match the 13-bit checksum of the current username",

    and so on ad infinitum. For clarity can you tell us which of the possible rules you don't mean? (j/k).

    -M

    Free your mind

Re: removing element from hash array
by davorg (Chancellor) on Nov 16, 2005 at 14:16 UTC

    It's really not very clear what you want to do.

    Here's one possible solution.

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash = (list => { find => [ {type => 'error', column => 106 }, {type => 'warning', column => 1 } ] } +); print Dumper \%hash; pop @{$hash{list}{find}}; print Dumper \%hash;
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2023-01-27 15:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?