Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > traversing a hash structure

Reply
Thread Tools

traversing a hash structure

 
 
ela
Guest
Posts: n/a
 
      08-29-2011
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};
}


 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      08-29-2011
>>>>> "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 -------
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      08-29-2011
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


 
Reply With Quote
 
ela
Guest
Posts: n/a
 
      08-30-2011

<> wrote in message
news...
> 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


 
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
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Help with traversing an XML structure Michaelp Perl Misc 0 08-06-2009 12:20 PM
Hash :: Recursive traversing and stripping the values vimal Ruby 7 02-17-2009 06:00 AM
Traversing Hash or XML? Which is Fast thomson ASP .Net 0 12-27-2005 05:57 AM
help traversing and modifying hash key and value inplace Trans Ruby 8 04-13-2005 02:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57