views:

291

answers:

1

I am having a lot of IDs and I want to store them for a temporary purpose and need to search that record for some operation. Which data structure is good for this operation in Perl? Should I use a hash or an array, or is there any other module I could use to do this efficiently?

The records are 4343, 34343, 34343, 2323, 232, ....

+3  A: 

A little more information regarding your record layout would go a long way in helping people help you. If your records are linked to id numbers then you can use a hash with the 'id' as the key and store the record as a string or an array reference as the hash value

my %records;
$records{ $id_number } = "Record for $id_number";
## OR
$records{ $id_number } = ['Record', 'for', $id_number];

This will allow you to lookup id's with complexity O(1) and easily manipulate the corresponding record.

# Assuming the records are stored in @records
for my $record (@records) {
  $recStore{$record}++;
}

# To search for a record
my $recToFind = 4343;
my $recExists = $recStore{$recToFind} || 0;

The keys of the hash are the id's retrieved from your database and the corresponding values are the number of times the id was found, so for repeating records $recExists will be greater than 1, and for non-existent records it will be equal to 0. To get a list of all id's sorted numerically you could write

my @sortedID = sort {$a <=> $b} keys %records;
muteW
How to store and ids and search id .. for example . I want to search 4343 for the stored has. Could u please explain
joe
Could you include the details of any one record? This would allow me to formulate a storage mechanism suited to the task.
muteW
I am getting the value in $Reocrd =10
joe
And keep on getting different record whcih numeric and store to hash and some time in need to search some other numeric value with this hash
joe