How to prevent duplicated entry in array of the hash

Discussion in 'Perl Misc' started by Cyrus, Dec 20, 2006.

  1. Cyrus

    Cyrus Guest

    Hi folks,
    I'm new in this group, I would like to ask all of the Perl experts out
    there, if any one knows how to prevent a duplicated entry in array of
    the hash: here is what I need to do. The script periodically check for
    disk status if there is an error it adds it to array of the hash with
    keys & value, what I need is before it push/append it to the array of
    the hash it check to see if the key & value exists if not then
    push/append it else do other stuff or next
    Thanks in advance & hop I can get an answer from one of you guys..
    Cheers

    Here is a portion of the code:
    my $sh = $c->show_disk();
    foreach my $sys (@sys_arry) {
    foreach my $el (@$sh) {
    if ($el->{'status'} ne "OK")
    {
    # Here I know I should check to see if the keys & value exits
    but don't know how!!
    push @DiskErr, {sys=>$sys, disk=>$el->{'id'},
    status=>$el->{'status'}};
    } else
    {
    print "$sys: Status OK\n";
    }
    }
    }
    Cyrus, Dec 20, 2006
    #1
    1. Advertising

  2. Cyrus

    Cyrus Guest

    Re: How to prevent duplicated entry in array of the hash-Help PLEASE PLEASE

    One more thing to add is a system has multiple disks, & I have more
    than 200 systems to check for system, disk & disk status. to make it
    more clear if script finds an error say on system-X with disk 2 &
    status No-In-Service it notify user and go to the next system, when it
    hits same system again it checks to see if this is a same system if so
    if this is a same disk if this is same system & same disk & same status
    then don't notify user simply go to the next till the status of the
    disk gets change when it hits that system again!
    Cyrus
    Jim Gibson wrote:
    > In article <>,
    > Cyrus <> wrote:
    >
    > > Hi folks,
    > > I'm new in this group, I would like to ask all of the Perl experts out
    > > there, if any one knows how to prevent a duplicated entry in array of
    > > the hash: here is what I need to do. The script periodically check for
    > > disk status if there is an error it adds it to array of the hash with
    > > keys & value, what I need is before it push/append it to the array of
    > > the hash it check to see if the key & value exists if not then
    > > push/append it else do other stuff or next
    > > Thanks in advance & hop I can get an answer from one of you guys..
    > > Cheers

    >
    > Use a hash to record saved entries. It is not clear what should not be
    > duplicated in your program below. Just $sys, or the $sys/$el->{'id}
    > combination.
    >
    > Assuming it is just $sys (untested):
    >
    > >
    > > Here is a portion of the code:

    >
    > my %bad;
    >
    > > my $sh = $c->show_disk();
    > > foreach my $sys (@sys_arry) {
    > > foreach my $el (@$sh) {
    > > if ($el->{'status'} ne "OK")
    > > {
    > > # Here I know I should check to see if the keys & value exits
    > > but don't know how!!

    >
    > if( ! exists $bad{$sys} ) {
    >
    > > push @DiskErr, {sys=>$sys, disk=>$el->{'id'},
    > > status=>$el->{'status'}};

    >
    > $bad{$sys} = $status; # any value, even 1
    >
    > > } else
    > > {
    > > print "$sys: Status OK\n";
    > > }
    > > }
    > > }
    > >

    >
    > Posted Via Usenet.com Premium Usenet Newsgroup Services
    > ----------------------------------------------------------
    > ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    > ----------------------------------------------------------
    > http://www.usenet.com
    Cyrus, Dec 20, 2006
    #2
    1. Advertising

  3. Cyrus

    Cyrus Guest

    Re: How to prevent duplicated entry in array of the hash-Help PLEASE PLEASE

    Sorry folks for muliple posting, as I said before I'm new & it won't
    happen again..
    cheers
    Jim Gibson wrote:
    > Please do not top-post in this newsgroup. Try to put your responses
    > below that to which you are responding. And please trim irrelevant
    > material. Thanks. (Top-posting has been fixed.)
    >
    > In article <>, Cyrus
    > <> wrote:
    > > Jim Gibson wrote:
    > > > In article <>,
    > > > Cyrus <> wrote:
    > > >
    > > > > Hi folks,
    > > > > I'm new in this group, I would like to ask all of the Perl experts out
    > > > > there, if any one knows how to prevent a duplicated entry in array of
    > > > > the hash: here is what I need to do. The script periodically check for
    > > > > disk status if there is an error it adds it to array of the hash with
    > > > > keys & value, what I need is before it push/append it to the array of
    > > > > the hash it check to see if the key & value exists if not then
    > > > > push/append it else do other stuff or next
    > > > > Thanks in advance & hop I can get an answer from one of you guys..
    > > > > Cheers
    > > >
    > > > Use a hash to record saved entries. It is not clear what should not be
    > > > duplicated in your program below. Just $sys, or the $sys/$el->{'id}
    > > > combination.
    > > >

    >
    > [program snipped]
    >
    >
    > > One more thing to add is a system has multiple disks, & I have more
    > > than 200 systems to check for system, disk & disk status. to make it
    > > more clear if script finds an error say on system-X with disk 2 &
    > > status No-In-Service it notify user and go to the next system, when it
    > > hits same system again it checks to see if this is a same system if so
    > > if this is a same disk if this is same system & same disk & same status
    > > then don't notify user simply go to the next till the status of the
    > > disk gets change when it hits that system again!

    >
    > How are you finding these systems? Why will you potentially be checking
    > a system/disk more than once? What about just saving up all of the bad
    > status indications, putting them into one e-mail, and sending that.
    > That way, duplicates will not be such a problem. (Not much Perl content
    > in this post. Sorry.)
    >
    > Posted Via Usenet.com Premium Usenet Newsgroup Services
    > ----------------------------------------------------------
    > ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    > ----------------------------------------------------------
    > http://www.usenet.com
    Cyrus, Dec 21, 2006
    #3
  4. Cyrus

    Cyrus Guest

    Re: How to prevent duplicated entry in array of the hash-Help PLEASE PLEASE

    Q: How are you finding these systems?
    A: ssh to the system
    Q: Why will you potentially be checking a system/disk more than once?
    A: Disk might go out of commission .
    Q: What about just saving up all of the bad status indications, putting
    them into one e-mail, and sending that.That way, duplicates will not be
    such a problem.
    A: That's What I'm doing, the problem is I don't want to send muliple
    email on the same system, just to make a note here all script check
    same systems over and over gain.


    Cyrus wrote:
    > Sorry folks for muliple posting, as I said before I'm new & it won't
    > happen again..
    > cheers
    > Jim Gibson wrote:
    > > Please do not top-post in this newsgroup. Try to put your responses
    > > below that to which you are responding. And please trim irrelevant
    > > material. Thanks. (Top-posting has been fixed.)
    > >
    > > In article <>, Cyrus
    > > <> wrote:
    > > > Jim Gibson wrote:
    > > > > In article <>,
    > > > > Cyrus <> wrote:
    > > > >
    > > > > > Hi folks,
    > > > > > I'm new in this group, I would like to ask all of the Perl experts out
    > > > > > there, if any one knows how to prevent a duplicated entry in array of
    > > > > > the hash: here is what I need to do. The script periodically check for
    > > > > > disk status if there is an error it adds it to array of the hash with
    > > > > > keys & value, what I need is before it push/append it to the array of
    > > > > > the hash it check to see if the key & value exists if not then
    > > > > > push/append it else do other stuff or next
    > > > > > Thanks in advance & hop I can get an answer from one of you guys..
    > > > > > Cheers
    > > > >
    > > > > Use a hash to record saved entries. It is not clear what should not be
    > > > > duplicated in your program below. Just $sys, or the $sys/$el->{'id}
    > > > > combination.
    > > > >

    > >
    > > [program snipped]
    > >
    > >
    > > > One more thing to add is a system has multiple disks, & I have more
    > > > than 200 systems to check for system, disk & disk status. to make it
    > > > more clear if script finds an error say on system-X with disk 2 &
    > > > status No-In-Service it notify user and go to the next system, when it
    > > > hits same system again it checks to see if this is a same system if so
    > > > if this is a same disk if this is same system & same disk & same status
    > > > then don't notify user simply go to the next till the status of the
    > > > disk gets change when it hits that system again!

    > >
    > > How are you finding these systems? Why will you potentially be checking
    > > a system/disk more than once? What about just saving up all of the bad
    > > status indications, putting them into one e-mail, and sending that.
    > > That way, duplicates will not be such a problem. (Not much Perl content
    > > in this post. Sorry.)
    > >
    > > Posted Via Usenet.com Premium Usenet Newsgroup Services
    > > ----------------------------------------------------------
    > > ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    > > ----------------------------------------------------------
    > > http://www.usenet.com
    Cyrus, Dec 21, 2006
    #4
  5. Cyrus

    Keith Keller Guest

    Re: How to prevent duplicated entry in array of the hash-Help PLEASE PLEASE

    Q: Why are you *still* top-posting, even when you've been asked
    not to do so?
    A: Either I'm not paying enough attention, or I don't actually
    want help.

    On the off-chance that you do decide to pay attention (and if you
    don't, watch more people killfile you):

    On 2006-12-21, Cyrus <> wrote:

    > Q: What about just saving up all of the bad status indications, putting
    > them into one e-mail, and sending that.That way, duplicates will not be
    > such a problem.
    > A: That's What I'm doing, the problem is I don't want to send muliple
    > email on the same system, just to make a note here all script check
    > same systems over and over gain.


    If you'll be re-running this script, and want the script to be
    able to reference past data, you need to store the previous data
    somewhere, either on disk or in a database. For the former, read
    perldoc -q persistent ; for the latter, you can start with the
    DBI docs (though there's a pretty steep learning curve, especially
    if you don't have database experience).

    --keith

    --
    -francisco.ca.us
    (try just my userid to email me)
    AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
    see X- headers for PGP signature information
    Keith Keller, Dec 21, 2006
    #5
  6. On 12/20/2006 01:36 PM, Cyrus wrote:
    > Hi folks,
    > I'm new in this group, I would like to ask all of the Perl experts out
    > there, if any one knows how to prevent a duplicated entry in array of
    > the hash: here is what I need to do. The script periodically check for
    > disk status if there is an error it adds it to array of the hash with
    > keys & value, what I need is before it push/append it to the array of
    > the hash it check to see if the key & value exists if not then
    > push/append it else do other stuff or next
    > Thanks in advance & hop I can get an answer from one of you guys..
    > Cheers
    >
    > Here is a portion of the code:
    > my $sh = $c->show_disk();


    my %DiskErr;

    > foreach my $sys (@sys_arry) {
    > foreach my $el (@$sh) {
    > if ($el->{'status'} ne "OK")
    > {
    > # Here I know I should check to see if the keys & value exits
    > but don't know how!!
    > push @DiskErr, {sys=>$sys, disk=>$el->{'id'},
    > status=>$el->{'status'}};


    Replace the above with this:

    push @{$DiskErr{$sys}}, { sys => $sys, disk => $el->{'id'}, status =>
    $el->{'status'} };

    That autovivifies a hash entry for $DiskErr{$sys} that is an array
    reference. The array then has an anonymous hash pushed onto it.

    Note, the code is untested.

    > } else
    > {
    > print "$sys: Status OK\n";
    > }
    > }
    > }
    >


    By their natures, hashes do not allow duplicate keys, so if you use a
    hash, you don't have to check for duplicates.

    Read the documentation:
    perldoc perlref
    perldoc perldsc


    --

    http://home.earthlink.net/~mumia.w.18.spam/
    Mumia W. (on aioe), Dec 21, 2006
    #6
  7. Cyrus

    Cyrus Guest

    Thanks for the reply, I tried that before but didn't wotk for me I need
    to check if there is an existing entry in the hash of the array for
    sys, disk & status witout goiing through the nested for loops!
    cz
    Mumia W. (on aioe) wrote:
    > On 12/20/2006 01:36 PM, Cyrus wrote:
    > > Hi folks,
    > > I'm new in this group, I would like to ask all of the Perl experts out
    > > there, if any one knows how to prevent a duplicated entry in array of
    > > the hash: here is what I need to do. The script periodically check for
    > > disk status if there is an error it adds it to array of the hash with
    > > keys & value, what I need is before it push/append it to the array of
    > > the hash it check to see if the key & value exists if not then
    > > push/append it else do other stuff or next
    > > Thanks in advance & hop I can get an answer from one of you guys..
    > > Cheers
    > >
    > > Here is a portion of the code:
    > > my $sh = $c->show_disk();

    >
    > my %DiskErr;
    >
    > > foreach my $sys (@sys_arry) {
    > > foreach my $el (@$sh) {
    > > if ($el->{'status'} ne "OK")
    > > {
    > > # Here I know I should check to see if the keys & value exits
    > > but don't know how!!
    > > push @DiskErr, {sys=>$sys, disk=>$el->{'id'},
    > > status=>$el->{'status'}};

    >
    > Replace the above with this:
    >
    > push @{$DiskErr{$sys}}, { sys => $sys, disk => $el->{'id'}, status =>
    > $el->{'status'} };
    >
    > That autovivifies a hash entry for $DiskErr{$sys} that is an array
    > reference. The array then has an anonymous hash pushed onto it.
    >
    > Note, the code is untested.
    >
    > > } else
    > > {
    > > print "$sys: Status OK\n";
    > > }
    > > }
    > > }
    > >

    >
    > By their natures, hashes do not allow duplicate keys, so if you use a
    > hash, you don't have to check for duplicates.
    >
    > Read the documentation:
    > perldoc perlref
    > perldoc perldsc
    >
    >
    > --
    >
    > http://home.earthlink.net/~mumia.w.18.spam/
    Cyrus, Dec 21, 2006
    #7
  8. Cyrus

    Cyrus Guest

    Re: How to prevent duplicated entry in array of the hash-Help PLEASE PLEASE

    Keith I do reply to the message, do I need to use other option, please
    walk me through this so it won't happen again..
    cyrus
    Keith Keller wrote:
    > Q: Why are you *still* top-posting, even when you've been asked
    > not to do so?
    > A: Either I'm not paying enough attention, or I don't actually
    > want help.
    >
    > On the off-chance that you do decide to pay attention (and if you
    > don't, watch more people killfile you):
    >
    > On 2006-12-21, Cyrus <> wrote:
    >
    > > Q: What about just saving up all of the bad status indications, putting
    > > them into one e-mail, and sending that.That way, duplicates will not be
    > > such a problem.
    > > A: That's What I'm doing, the problem is I don't want to send muliple
    > > email on the same system, just to make a note here all script check
    > > same systems over and over gain.

    >
    > If you'll be re-running this script, and want the script to be
    > able to reference past data, you need to store the previous data
    > somewhere, either on disk or in a database. For the former, read
    > perldoc -q persistent ; for the latter, you can start with the
    > DBI docs (though there's a pretty steep learning curve, especially
    > if you don't have database experience).
    >
    > --keith
    >
    > --
    > -francisco.ca.us
    > (try just my userid to email me)
    > AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
    > see X- headers for PGP signature information
    Cyrus, Dec 21, 2006
    #8
  9. Cyrus

    DJ Stunks Guest

    Re: How to prevent duplicated entry in array of the hash-Help PLEASE PLEASE

    Cyrus wrote:
    > Keith I do reply to the message, do I need to use other option, please
    > walk me through this so it won't happen again..
    > Keith Keller wrote:
    > > Q: Why are you *still* top-posting, even when you've been asked
    > > not to do so?
    > > A: Either I'm not paying enough attention, or I don't actually
    > > want help.


    you're top posting, that means you're writing your response on top, and
    quoting (in full) the replies beneath it.

    Stop doing that.

    Instead, snip everything from the replies except the attribution (who
    said it) and some relevant context, and post your response underneath
    so that if one reads from top to bottom (like the english language
    works) the complete message makes sense.

    -jp
    DJ Stunks, Dec 21, 2006
    #9
  10. Cyrus

    Cyrus Guest

    Re: How to prevent duplicated entry in array of the hash-Help PLEASE PLEASE

    Got it thank for verification...something like this one right?
    Cyrus, Dec 21, 2006
    #10
  11. Re: How to prevent duplicated entry in array of the hash-Help PLEASE PLEASE

    Cyrus wrote:
    > Got it


    Got what?

    > thank for verification...something like this one right?


    Like what?
    Please quote some context when replying or nobody will have a clue what you
    are talking about.

    jue
    Jürgen Exner, Dec 21, 2006
    #11
  12. Cyrus

    Cyrus Guest

    Re: How to prevent duplicated entry in array of the hash-Help PLEASE PLEASE

    Jürgen Exner wrote:
    > Cyrus wrote:
    > > Got it

    >
    > Got what?

    Are you guys polling my leg :-( I was replying back to Keith concern!
    anyway I haven't got any solution for the problem I'm facing any body
    out there can help me on this or just call it quit?
    >
    Cyrus, Dec 21, 2006
    #12
  13. Cyrus

    Cyrus Guest

    Re: How to prevent duplicated entry in array of the hash-Help PLEASE PLEASE

    > An easier approach is to take advantage of the fact that hash keys are
    > unique - in fact, whenever I hear the words "unique" or "duplicate" with
    > respect to managing entries in a collection, I immediately think of a
    > hash. Example:
    >
    > # Hash, and key to add
    > my %test_hash;
    > my $new_value = 'foo';
    >
    > # Just add it - dups are eliminated automatically because it's a hash
    > $test_hash{$new_value} = 0;
    >
    > sherm--


    Thanks... below is the out puthere is output from Dumper, & here is my
    appand to hash of array.
    push @{$DiskErr{$sys}}, {disk => [$el->{'id'}], status =>
    [$el->{'status'}] };

    As you can see there is duplicated entries for disk 5 and a ststem can
    have up 20 disks that's why I used above push! any idea how to prevent
    a duplicate entry?
    The DiskErr is $VAR1 = {
    'BR5500' => [
    {
    'disk' => [
    '5'
    ],
    'status' => [
    'NOT-IN-SERVICE'
    ]
    },
    {
    'disk' => [
    '5'
    ],
    'status' => [
    'NOT-IN-SERVICE'
    ]
    },
    {
    disk' => [
    '2'
    ],
    'status' => [
    'NOT-IN-SERVICE'
    ]
    },

    ],

    >
    > --
    > Web Hosting by West Virginians, for West Virginians: http://wv-www.net
    > Cocoa programming in Perl: http://camelbones.sourceforge.net
    Cyrus, Dec 21, 2006
    #13
  14. Cyrus

    Cyrus Guest

    Re: How to prevent duplicated entry in array of the hash-Help PLEASE PLEASE

    >
    > $DiskErr->{$sys}->{$el->{'id'}} = { status => $el->{'status'} };
    >
    > That will give you a data structure that looks like this:
    >
    > {
    > 'BR5500' => {
    > '5' => {
    > 'status' => 'NOT-IN-SERVICE',
    > }
    > },
    > }
    >
    >

    Thank you so much Sherm, I will use the above per your request as one
    disk can have only one status at a time...before I rest my case How do
    I access disk , is it sompthing like this:

    my $diskStatus = $DiskErr->->{$sys}->{$el->{'id'}} ;

    Again thank you & you are my hero & life saver....Oh by the way How am
    I doing with reply with toping :)
    Cyrus, Dec 21, 2006
    #14
  15. Cyrus

    Cyrus Guest

    Re: How to prevent duplicated entry in array of the hash-Help PLEASE PLEASE

    >
    > Like I said, I'd use a hash, with the disk number as the key:
    > $DiskErr->{$sys}->{$el->{'id'}} = { status => $el->{'status'} };
    >

    I get an error: Global symbol "$DiskErr" requires explicit package name
    at....
    I declare that as: my %DiskErr;

    > sherm--
    >
    Cyrus, Dec 21, 2006
    #15
  16. Cyrus

    Cyrus Guest

    Re: How to prevent duplicated entry in array of the hash-Help PLEASE PLEASE

    > I declare that as: my %DiskErr;
    >
    > Sorry 'bout that. Your original code didn't declare it, so I figured were
    > declaring it somewhere else.
    >
    > sherm--


    Still getting error even after declaring my %DiskErr;
    any idea..
    Global symbol "$DiskErr" requires explicit package name a
    Cyrus, Dec 21, 2006
    #16
  17. Cyrus

    Paul Lalli Guest

    Re: How to prevent duplicated entry in array of the hash-Help PLEASE PLEASE

    Sherm Pendley wrote:
    > "Cyrus" <> writes:
    >
    > > Still getting error even after declaring my %DiskErr;
    > > any idea..
    > > Global symbol "$DiskErr" requires explicit package name a

    >
    > You're declaring it, but not in the same scope in which you're using it.


    No, he's not declaring it. He's declaring %DiskErr, when he's using
    $DiskErr. Two completely unrelated variables.

    Paul Lalli
    Paul Lalli, Dec 21, 2006
    #17
  18. Cyrus

    Cyrus Guest

    Re: How to prevent duplicated entry in array of the hash-Help PLEASE PLEASE

    > You're declaring it, but not in the same scope in which you're using it.

    There was extra ->, it should be like below I fixed it...thanks a lot:
    Coorect: $DiskErr{$sys}->{$el->{'id'}} = { status => $el->{'status'}
    };
    WRONG: $DiskErr->{$sys}->{$el->{'id'}} = { status => $el->{'status'}
    };

    Cheers & Have Great Holiday...
    Cyrus, Dec 21, 2006
    #18
  19. Re: How to prevent duplicated entry in array of the hash-Help PLEASE PLEASE

    Cyrus <> wrote:

    > Still getting error even after declaring my %DiskErr;
    > any idea..
    > Global symbol "$DiskErr" requires explicit package name a



    Don't declare %DiskErr (a hash).

    Instead declare $DiskErr (a scalar).


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 22, 2006
    #19
  20. Cyrus

    Cyrus Guest

    Re: How to prevent duplicated entry in array of the hash-Help PLEASE PLEASE

    > Don't declare %DiskErr (a hash).
    >
    > Instead declare $DiskErr (a scalar).
    >

    Thanks..
    Cyrus, Dec 22, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michelle
    Replies:
    8
    Views:
    158
    Chris Barber
    Aug 2, 2003
  2. rp
    Replies:
    1
    Views:
    516
    red floyd
    Nov 10, 2011
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    612
    David A. Black
    Jul 2, 2008
  4. Cyrus
    Replies:
    1
    Views:
    85
    A. Sinan Unur
    Dec 20, 2006
  5. Cyrus
    Replies:
    1
    Views:
    77
    A. Sinan Unur
    Dec 20, 2006
Loading...

Share This Page