Google Answers Logo
View Question
 
Q: perl script: Creating a set of sets of intergers ( No Answer,   4 Comments )
Question  
Subject: perl script: Creating a set of sets of intergers
Category: Computers > Programming
Asked by: skg-ga
List Price: $5.00
Posted: 04 Jul 2004 15:43 PDT
Expires: 13 Jul 2004 03:30 PDT
Question ID: 369648
Consider an input file with following enteries:
1 3 4 5 6
1 6 4 5 3
1 2 4
4 2 1
...
...
Each row is a set without duplicates.
THe file itself represents a set of rows.

I am currently using set::Scalar to represent each row. 
And Set:: Scalar to represent the set of rows.
The problem is that it contains duplicates values of sets.
i.e. the entries of set look like this
(3,4,5), (3,4,5) ,(1,4,5),....
if (3,4,5) is added twice.

How can one force the set to contain unique elemens.

thanks
sandeep

Clarification of Question by skg-ga on 04 Jul 2004 23:38 PDT
The data could occur  at an intermediate step of the program. I posed
the problem  this way for simplicity.

To rephrase the problem:
Given  a data structure, D that stores family of sets  and a query set
nset. Check if nset is already present in D.

Ofcourse i can go through each set in D and compare it with nset. But
i want to do this more efficently.

This is how i would have done in C++. Create a function comparison(Set
a, Set b) that provides <=> over sets and then use stl::map with this
comparison.
Answer  
There is no answer at this time.

Comments  
Subject: Re: perl script: Creating a set of sets of intergers
From: hailstorm-ga on 04 Jul 2004 19:14 PDT
 
If the duplicates are in your data file, you could just pipe the data
file through a program such as uniq to filter out all of the
duplicates before processing.
Subject: Re: perl script: Creating a set of sets of intergers
From: hailstorm-ga on 04 Jul 2004 19:16 PDT
 
Of course, the functionality of uniq can be implemented fairly simply
in Perl itself:

perl -ne 'print unless $s eq $_; $s = $_'
Subject: Re: perl script: Creating a set of sets of intergers
From: skg-ga on 04 Jul 2004 23:37 PDT
 
The data could occur  at an intermediate step of the program. I posed
the problem  this way for simplicity.

To rephrase the problem:
Given  a data structure, D that stores family of sets  and a query set
nset. Check if nset is already present in D.

Ofcourse i can go through each set in D and compare it with nset. But
i want to do this more efficently.

This is how i would have done in C++. Create a function comparison(Set
a, Set b) that provides <=> over sets and then use stl::map with this
comparison.
Subject: Re: perl script: Creating a set of sets of intergers
From: skg-ga on 13 Jul 2004 03:30 PDT
 
One cheap solution is to use Hash of arrays:
$str = join(":", @arr);
$HOA{$str} = 1;

To recover:
@arr = split(/:/, $str);

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy