Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Check if unique pairs are in a hash

Reply
Thread Tools

Check if unique pairs are in a hash

 
 
burdrs@gmail.com
Guest
Posts: n/a
 
      03-22-2006
I have a hash that I created:


@uniq=qw(a b c e f g);
my $list;
my @list = @uniq;
@hash{@list} = ();


I now want to check if the following pairs are contained within the
hash:

a b (yes, it is)
a h (no, it isn't)
b g (yes, it is)

I would like the output contain the pair number (e.g., pair a b is the
first pair, a h the second, etc.) and either 1 or 0 to indicate
membership:

1.1 2.0 3.1

Sorry if this question is really basic. I've study old message and the
FAQ for hours with not luck.

Thanks.
Thanks.

 
Reply With Quote
 
 
 
 
Rick Scott
Guest
Posts: n/a
 
      03-22-2006
((E-Mail Removed) <(E-Mail Removed)> uttered
> I have a hash that I created:


First things first:

use warnings;
use strict;

Trust me, put these two pragmas at the beginning of every Perl
program you write. They will save you a whole lot of pain.
You do need to predeclare your variables under strict, though:

my @uniq = qw(a b c e f g);
my %hash;

> @uniq=qw(a b c e f g);
> my $list;
> my @list = @uniq;
> @hash{@list} = ();


Three points:

$list is a new, empty *scalar* variable -- it has nothing at all to do
with @list other than having a similar name. In fact, you don't seem
to use $list at all.

You assign the values of @uniq to @list, but I'm not sure why.
Why not just write the next line as

@hash{@uniq} = ();

What this line of code means is "Take all of the pairs in %hash with
keys in @uniq, and assign undef to them". So you don't have a hash
with 'a' => 'b' type pairs in them. What you have in %hash is this:

$VAR1 = {
'e' => undef,
'c' => undef,
'a' => undef,
'g' => undef,
'b' => undef,
'f' => undef
};


As an aside -- Data:umper is an easy way to see what's actually in
a variable. You can use it to see what's inside your %hash like this:


#!/usr/bin/perl

use warnings;
use strict;
use Data:umper;

my @uniq = qw(a b c e f g);
my %hash;
@hash{@uniq} = ();

print Dumper \%hash;


> I now want to check if the following pairs are contained within the
> hash:
>
> a b (yes, it is)
> a h (no, it isn't)
> b g (yes, it is)
>
> I would like the output contain the pair number (e.g., pair a b is the
> first pair, a h the second, etc.) and either 1 or 0 to indicate
> membership:
>
> 1.1 2.0 3.1


I can't help you with this (yet), because I'm not sure what your
%hash is supposed to contain. If you'd like to tune up your code
so that your %hash is coming out more like you expect, then post it
here, we'd be able to give you a bit more guidance.


> Sorry if this question is really basic.


We all had to start somewhere. What matters is that you're making
some effort. =)


> I've study old message and the FAQ for hours with not luck.


The fact that you did that is very much appreciated. Thanks!




Cheers,
Rick
--
key CF8F8A75 / print C5C1 F87D 5056 D2C0 D5CE D58F 970F 04D1 CF8F 8A75
It is a bad plan that admits of no modifications.
ublius Syrus
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      03-22-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I have a hash that I created:
>
> @uniq=qw(a b c e f g);
> my $list;
> my @list = @uniq;
> @hash{@list} = ();
>
>
> I now want to check if the following pairs are contained within the
> hash:
>
> a b (yes, it is)
> a h (no, it isn't)
> b g (yes, it is)


I don't think your code does what you think it does. You seem to be
assuming that your hash contains key-value pairs where the key are the
even-indexed items in the original array and the values are the
corresponding odd-indexed items. That's not what
@hash{@list} = () does. That line will assign each element of @list to
be a key in %hash with an undefined value.

I think you meant to do:
%hash = @list;

> I would like the output contain the pair number (e.g., pair a b is the
> first pair, a h the second, etc.)


well here we have a problem, because hashes in Perl are not ordered.
There is no "first" key/value pair. If you want to know that
information, you'll have to either find the key back in the original
array, or use a CPAN module like Tie::IxHash, which preserves insertion
order.

> and either 1 or 0 to indicate membership:
>
> 1.1 2.0 3.1


This, on the other hand, is not problematic at all. Simply use the
exists() function to determine if a given key exists in the hash:
print 1 if exists $hash{a};
or if you only want 1 to print if the key a has the value b, then:
print $hash{a} eq 'b' ? 1 : 0

> Sorry if this question is really basic. I've study old message and the
> FAQ for hours with not luck.


That is very appreciated.

Once you're able to produce a short-but-complete script taking these
factors into account, feel free to post here again if it doesn't do
what you want.

Don't forget to read the Posting Guidelines for this group, to help
maximize your potential assistance from this group.

Paul Lalli.

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      03-22-2006
(E-Mail Removed) <(E-Mail Removed)> wrote:

> I now want to check if the following pairs are contained within the
> hash:
>
> a b (yes, it is)
> a h (no, it isn't)
> b g (yes, it is)
>
> I would like the output contain the pair number (e.g., pair a b is the
> first pair, a h the second, etc.) and either 1 or 0 to indicate
> membership:



--------------------
#!/usr/bin/perl
use warnings;
use strict;

my @uniq=qw(a b c e f g);
my %hash;
@hash{@uniq} = ();

my @pairs = ( ['a','b'], ['a','h'], ['b','g'] );

foreach my $i ( 0 .. $#pairs ) {
print $i+1,
'.',
(exists $hash{$pairs[$i][0]} and exists $hash{$pairs[$i][1]}) ? 1 : 0,
' ';
}
print "\n";
--------------------


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      03-22-2006
Tad McClellan <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> (E-Mail Removed) <(E-Mail Removed)> wrote:
>
> > I now want to check if the following pairs are contained within the
> > hash:
> >
> > a b (yes, it is)
> > a h (no, it isn't)
> > b g (yes, it is)
> >
> > I would like the output contain the pair number (e.g., pair a b is the
> > first pair, a h the second, etc.) and either 1 or 0 to indicate
> > membership:

>
>
> --------------------
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> my @uniq=qw(a b c e f g);
> my %hash;
> @hash{@uniq} = ();
>
> my @pairs = ( ['a','b'], ['a','h'], ['b','g'] );
>
> foreach my $i ( 0 .. $#pairs ) {
> print $i+1,
> '.',
> (exists $hash{$pairs[$i][0]} and exists $hash{$pairs[$i][1]}) ? 1 : 0,
> ' ';
> }
> print "\n";
> --------------------


Once again you're the first to make sense of the question. I sure didn't
get it.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
burdrs@gmail.com
Guest
Posts: n/a
 
      03-23-2006
Thanks to all. I used Tad's solution since it it seems to be most
intuitive--works perfectly. Now one more problem:

I need to generate @pairs. I have a list of elements a1, a2....., an. I
would like to find all unique pairs of these elements and enter them
into @pairs:

my @pairs={['a1,'a2'], ['a1', 'a3'],....['a1','an'],['a2','a3'],......}

Can I do this starting with the list
$variables=qw{a1 a2 ....an}?

Thanks!

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      03-23-2006
(E-Mail Removed) <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Thanks to all. I used Tad's solution since it it seems to be most
> intuitive--works perfectly. Now one more problem:
>
> I need to generate @pairs. I have a list of elements a1, a2....., an. I
> would like to find all unique pairs of these elements and enter them
> into @pairs:
>
> my @pairs={['a1,'a2'], ['a1', 'a3'],....['a1','an'],['a2','a3'],......}
>
> Can I do this starting with the list
> $variables=qw{a1 a2 ....an}?


No, but you can start with

@variables = qw{a1 a2 ....an};

Please make an effort to solve your problem yourself before asking for
a solution.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a unique method in python to unique a list? Token Type Python 9 09-09-2012 02:13 PM
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Can I check the a hash contain another hash in deep check? Siratinee Sukachai Ruby 1 04-22-2011 07:43 PM
list question... unique values in all possible unique spots ToshiBoy Python 6 08-12-2008 05:01 AM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM



Advertisments