traversing a hash structure

Discussion in 'Perl Misc' started by ela, Aug 29, 2011.

  1. ela

    ela Guest

    For a market basket analysis, I'd like to traverse a hash structure
    containing entries like:

    my %list;
    $list{"c1"} = "milk%cheese%coca cola";
    $list{"c2"} = "coca cola";
    $list{"c3"} = "beef";
    ....
    $list{"cn-1"} = "apple%cheese";
    $list{"cn"} = "beef%milk";

    so the knowledge derived may look like:
    c1 is associated with c2, .., cn-1, cn
    c3 is associated with cn
    ....

    i.e. the total number of checking is "n + n-1 + ... + 2 + 1" .
    While this can be easily achieved if the structure is array, the following
    looks clumsy... any better implementation?

    while (%list) {
    $assign = 1;
    foreach $key (keys %list) {
    if ($assign == 1) {
    $head = $key;
    $assign = 0;
    next;
    }
    if ($list{$head} =~ /$list{$key}/ or $list{$key} =~
    /$list{$head}/ ) {
    print "$list{$head}\t$list{$key}\t1\n";
    }
    }
    undef $list{$head};
    }
    ela, Aug 29, 2011
    #1
    1. Advertising

  2. ela

    Uri Guttman Guest

    >>>>> "HL" == Henry Law <> writes:

    HL> On 29/08/11 05:01, ela wrote:
    >> For a market basket analysis, I'd like to traverse a hash structure
    >> containing entries like:
    >>
    >> my %list;
    >> $list{"c1"} = "milk%cheese%coca cola";


    HL> Just by the way, is this real code? Surely Perl would have tried to
    HL> parse %cheese etc as the names of hash variables? Confusing at best.

    confusing maybe but hashes don't interpolate in strings.

    uri

    --
    Uri Guttman -- uri AT perlhunter DOT com --- http://www.perlhunter.com --
    ------------ Perl Developer Recruiting and Placement Services -------------
    ----- Perl Code Review, Architecture, Development, Training, Support -------
    Uri Guttman, Aug 29, 2011
    #2
    1. Advertising

  3. ela

    Guest

    On Mon, 29 Aug 2011 12:01:40 +0800, "ela" <> wrote:

    >For a market basket analysis, I'd like to traverse a hash structure
    >containing entries like:
    >
    >my %list;
    >$list{"c1"} = "milk%cheese%coca cola";
    >$list{"c2"} = "coca cola";
    >$list{"c3"} = "beef";
    >...
    >$list{"cn-1"} = "apple%cheese";
    >$list{"cn"} = "beef%milk";
    >
    >so the knowledge derived may look like:
    >c1 is associated with c2, .., cn-1, cn
    >c3 is associated with cn
    >...
    >
    >i.e. the total number of checking is "n + n-1 + ... + 2 + 1" .
    >While this can be easily achieved if the structure is array, the following
    >looks clumsy... any better implementation?
    >
    >while (%list) {
    > $assign = 1;
    > foreach $key (keys %list) {
    > if ($assign == 1) {
    > $head = $key;
    > $assign = 0;
    > next;
    > }
    > if ($list{$head} =~ /$list{$key}/ or $list{$key} =~
    >/$list{$head}/ ) {
    > print "$list{$head}\t$list{$key}\t1\n";
    > }
    > }
    > undef $list{$head};
    >}
    >


    Its still being treated as a list in the foreach loop.
    In your current implementation, the %list keys never
    change so you sit in the while() loop forever, and
    $head is assigned the same $key every time, doing the
    same operation indefinitely and generating uninitialized
    value warnings in the regex.

    change this:
    > undef $list{$head};


    to this:
    delete $list{$head};

    As a final note, the target of a regular expression
    doesen't transparently translate into a regular expression itself.

    -sln
    , Aug 29, 2011
    #3
  4. ela

    ela Guest

    <> wrote in message
    news:eek:...
    > Its still being treated as a list in the foreach loop.
    > In your current implementation, the %list keys never
    > change so you sit in the while() loop forever, and
    > $head is assigned the same $key every time, doing the
    > same operation indefinitely and generating uninitialized
    > value warnings in the regex.
    >
    > change this:
    >> undef $list{$head};

    >
    > to this:
    > delete $list{$head};
    >
    > As a final note, the target of a regular expression
    > doesen't transparently translate into a regular expression itself.
    >
    > -sln


    Thanks sln. I hope you have already got your favorite job. Thanks again :)
    ela, Aug 30, 2011
    #4
    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. thomson
    Replies:
    0
    Views:
    316
    thomson
    Dec 27, 2005
  2. rp
    Replies:
    1
    Views:
    491
    red floyd
    Nov 10, 2011
  3. Trans
    Replies:
    8
    Views:
    143
    Trans
    Apr 13, 2005
  4. vimal
    Replies:
    7
    Views:
    117
    vimal
    Feb 17, 2009
  5. Michaelp

    Help with traversing an XML structure

    Michaelp, Aug 6, 2009, in forum: Perl Misc
    Replies:
    0
    Views:
    98
    Michaelp
    Aug 6, 2009
Loading...

Share This Page