Check if unique pairs are in a hash

B

burdrs

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.
 
R

Rick Scott

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::Dumper 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::Dumper;

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
 
P

Paul Lalli

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.
 
T

Tad McClellan

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";
 
A

Anno Siegel

Tad McClellan said:
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
 
B

burdrs

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!
 
A

Anno Siegel

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top