Home > Archive > PERL Miscellaneous > December 2006 > Re: How to prevent duplicated entry in array of the hash-Help PLEASE PLEASE
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
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 <1166643368.429390.247300@n67g2000cwd.googlegroups.com>,
> Cyrus <cyrusnt@hotmail.com> wrote:
>
>
> 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):
>
>
> my %bad;
>
>
> if( ! exists $bad{$sys} ) {
>
>
> $bad{$sys} = $status; # any value, even 1
>
>
> Posted Via Usenet.com Premium Usenet Newsgroup Services
> ----------------------------------------------------------
> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
> ----------------------------------------------------------
> http://www.usenet.com
| |
| Jim Gibson 2006-12-20, 7:03 pm |
|
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 <1166654413.117524.67740@48g2000cwx.googlegroups.com>, Cyrus
<cyrusnt@hotmail.com> wrote:
> Jim Gibson wrote:
[program snipped]
[color=darkred]
> 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
| |
| Keith Keller 2006-12-20, 10:02 pm |
| 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 <cyrusnt@hotmail.com> 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
--
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information
| |
| Jürgen Exner 2006-12-21, 7:02 pm |
| 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
| |
| Sherm Pendley 2006-12-21, 7:02 pm |
| "Cyrus" <cyrusnt@hotmail.com> writes:
> Jürgen Exner wrote:
> Are you guys polling my leg :-(
No, they're not.
> I was replying back to Keith concern!
But you didn't quote the relevant bits you were replying to. See how I've
quoted parts of your message, and Jürgen's, above this?
The reason that quoting is important is that we're not all using Google
Groups to access usenet. It's entirely possible that your message may get
to me before Keith's, that Keith's may not get to me at all, or that it
may be gone from my local server before yours arrives.
> anyway I haven't got any solution for the problem I'm facing
The problem in the subject, you mean? If you want to prevent duplicates,
one simple way to do that is to use a hash instead of an array. That is,
instead of doing this:
# Array, and value to add
my @test_array;
my $new_value = 'foo';
# Don't add if it would be a dup
push(@test_array, $new_value)
unless grep { $_ eq $new_value } @test_array;
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--
--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
| |
| Sherm Pendley 2006-12-21, 7:02 pm |
| "Cyrus" <cyrusnt@hotmail.com> writes:
>
> 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'
> ]
> },
>
> ],
Like I said, I'd use a hash, with the disk number as the key:
$DiskErr->{$sys}->{$el->{'id'}} = { status => $el->{'status'} };
That will give you a data structure that looks like this:
{
'BR5500' => {
'5' => {
'status' => 'NOT-IN-SERVICE',
}
},
}
Note that each disk now has only one status entry. If you want to allow for
multiple and/or duplicate status entries, you can push them onto an array:
push @{$DiskErr->{$sys}->{$el->{'id'}}->{'status'}}, $el->{'status'};
This would give you a data structure like this:
{
'BR5500' => {
'5' => {
'status' => [
'NOT-IN-SERVICE',
'NOT-IN-SERVICE',
],
},
},
}
You might want to eliminate duplicate status lines too - again, the answer is
to use a hash instead of an array:
$DiskErr->{$sys}->{$el->{'id'}}->{'status'}->{$el->{'status'}}++;
The result:
{
'BR5500' => {
'5' => {
'status' => {
'NOT-IN-SERVICE' => 1, # counting starts at 0!
},
},
},
}
This is typed into my news reader, untested, etc. But it should be enough to
get you started down the right track, at least. See also:
perldoc perllol
perldoc perldsc
sherm--
--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
| |
| Sherm Pendley 2006-12-21, 7:02 pm |
| "Cyrus" <cyrusnt@hotmail.com> writes:
> 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'}} ;
^^
Too many ->'s here --------^
That'll give you the entire disk hashref - a structure like this:
$diskStatus = {
'status' => 'NOT-IN-SERVICE',
};
If what you want is just the status, you'd want to do:
my $diskStatus = $DiskErr->{$sys}->{$el->{'id'}}->{'status'};
> Again thank you & you are my hero & life saver
I must be hungry, that makes me think of sandwiches and candy. :-)
>....Oh by the way How am
> I doing with reply with toping :-)
Much better now, thank you.
sherm--
--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
| |
| Sherm Pendley 2006-12-21, 7:02 pm |
| "Cyrus" <cyrusnt@hotmail.com> writes:
> I get an error: Global symbol "$DiskErr" requires explicit package name
> at....
> 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--
--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
| |
| Sherm Pendley 2006-12-21, 7:02 pm |
| "Cyrus" <cyrusnt@hotmail.com> 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. If
it's in another package, then like the message says, you'll need to specify
the full name, including the package. Or, you can declare it using "our" at
the top of each file that needs it.
Have a look at:
perldoc -f my
perldoc -f our
Keep in mind that the example I gave was just that - an example. It wasn't
supposed to be something you could just copy-n-paste into your code, it was
meant to illustrate the concept.
sherm--
--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
| |
| Paul Lalli 2006-12-21, 7:02 pm |
| Sherm Pendley wrote:
> "Cyrus" <cyrusnt@hotmail.com> writes:
>
>
> 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
| |
|
| > 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...
| |
| Tad McClellan 2006-12-21, 10:02 pm |
| Cyrus <cyrusnt@hotmail.com> 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
tadmc@augustmail.com Perl programming
Fort Worth, Texas
| |
|
| > Don't declare %DiskErr (a hash).
>
> Instead declare $DiskErr (a scalar).
>
Thanks..
|
|
|
|
|