View Full Version : Perl -Array Compares
NDBACommish
06-24-2000, 12:51 AM
I need to know if there is a faster way to compare and or merge 2 arrays in Perl. I have done it by making 2 loops one inside the other but this seems tedious. FYI the arrays are around 470 elements and 150 elements... I have been comparing each of the 470 to each of the 150 to merge and compare the 2... does this make sense??
Thanks for any help you can give..
Vincent Puglia
06-24-2000, 03:09 AM
Hi,
Don't know perl (c/c++/pascal/etc), but have you tried sorting and hashing/indexing them first? So that if array1[251].substring(0,1) == 'm', you don't go through the first 50 or so elements of array2 (because they only include a->l).
The sorting may take a little time but it should result in faster times in the long run.
Vinny
------------------
My site:GrassBlade (http://members.aol.com/grassblad)
NDBACommish
06-24-2000, 05:41 PM
Yes I have tried sorting them first (good Idea by the way). Each array is really a flat database from a text file that I have loaded into the each. I want to merge them, some of the lines (array elements) have the same names in them (part of the information per line) and some may be unique. Sorting them alphabetically by the name portion of the element (as in the substring part) helps but it will still go all the way through the array IF the name is unique. This slows it all down. Still it does it all in a few seconds BUT still seems cumbersome.
Thanks for the tip though...
Still has to be a faster way.. I think?
Vincent Puglia
06-25-2000, 04:38 AM
Hi,
If they are in a text file (comma-delimited?), pull all of the data into a javascript array. sort(). then compare.
1) text file must be named *.js
2) enclose the data as such:
var rawData = new Array("text1","text2",...,"textN");
3) within an html page:
<script language="javascript" src="someName.js"></script>
<script language='javascript'>
<!--
function doIt()
{
var i = j = 0;
var mergedData = new Array();
rawData.sort();
do {
if (rawData[i] != rawData[i+1])
{
mergedData[j] = rawData[i];
j++;
}
i++;
} while (i != rawData.length);
if (rawData[i] != rawData[i-1])
mergedData[j] = rawData[i];
}
//-->
</script>
<body onload='doIt()'>
Javascript's sort is in-place. As long as your elements do not begin with numbers, it should work. Once you get the mergedData array, you should be able to write to disk, if necessary, with perl
Vinny
------------------
My site:GrassBlade (http://members.aol.com/grassblad)
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.