Check if unique pairs are in a hash

Discussion in 'Perl Misc' started by burdrs@gmail.com, Mar 22, 2006.

  1. Guest

    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.
     
    , Mar 22, 2006
    #1
    1. Advertising

  2. Rick Scott Guest

    ( <> 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::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
    --
    key CF8F8A75 / print C5C1 F87D 5056 D2C0 D5CE D58F 970F 04D1 CF8F 8A75
    It is a bad plan that admits of no modifications.
    :publius Syrus
     
    Rick Scott, Mar 22, 2006
    #2
    1. Advertising

  3. Paul Lalli Guest

    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.
     
    Paul Lalli, Mar 22, 2006
    #3
  4. <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 22, 2006
    #4
  5. Anno Siegel Guest

    Tad McClellan <> wrote in comp.lang.perl.misc:
    > <> 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.
     
    Anno Siegel, Mar 22, 2006
    #5
  6. Guest

    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!
     
    , Mar 23, 2006
    #6
  7. Anno Siegel Guest

    <> 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.
     
    Anno Siegel, Mar 23, 2006
    #7
    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. ToshiBoy
    Replies:
    6
    Views:
    876
    ToshiBoy
    Aug 12, 2008
  2. rp
    Replies:
    1
    Views:
    562
    red floyd
    Nov 10, 2011
  3. Trans

    Hash pairs at?

    Trans, Jan 21, 2007, in forum: Ruby
    Replies:
    25
    Views:
    298
    Trans
    Jan 23, 2007
  4. Siratinee Sukachai
    Replies:
    1
    Views:
    147
    7stud --
    Apr 22, 2011
  5. Token Type
    Replies:
    9
    Views:
    384
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page