473,480 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dirty Arrays and how to clean them up!

I have this array with duplicate entries. Hundreds to be in fact.

For example:Array = 17177 9661 9661 9535 9533 9533 9533 9533 9533 9533
9533 9533 9533 9533 9533 9533 9533 9533 9532 9532 9532 9532 9531 9096
9096 9096 9095 9095 9095 8345 8345 8344 8344 8226 8226 8225 8225 8198
8198 8198 8198 8198 8198 8198 8198 8198 8198 8198 8198 8198 8198 8198
8198 8198 8198 8198 8198 8198 8198 8198 8198 8198..............

This is what i am doing. i am reading file1 that has these entries in
the file mutiple times. I am comparing it against file2. if i do not
find a match i write the record to file3 and then to an array. when i
read file1 again and compare it against file2 i check to see if the
recorrd has been written by rereading the array and looking for it.
if it finds the same entry in the array then read the next record do
not wirte to file3. if not then write the record to file3 and the
array.

i either want to only write once to the array per record or before i
check remove dupliactes.

here some code example:
while file1
while file2
if record1 != record2
if record2 is not in array
write to file3
write to new record to array
last
else
next
else
next

Here is another example that
shows a poor example but you should get the idea. at least i
hope!!!!

@SORTED = ();
@CLEANED = ();
$X = 0;
$R = 0;
while ($R < 4)
{
while ($X <= 100)
{
$X++;
# print "I am in X\n";
# # i really do not want to write to the array if
# # it already exists in the array.
# # if array already has X then do not right to the
# # array.
push @CHECKED, $X;
@SORTED = sort {$a <=> $b} @CHECKED;
}
# print "*********I am in R\n";
$R++;
$X = 0;
next;
}

print "Array = @SORTED\n";

foreach $Row (@SORTED)
{
$Hold = $Row;
if ($Hold == $Row)
{
@CLEANED = shift @SORTED;
}
else
{
next;
}
# # How do i remove duplicates from the array?
# # I know this is wrong but here is my delima!
}

print "Array = @CLEANED\n";

Jul 19 '05 #1
4 6950
Bob
I have been working on the solution myself, but i am still having
problems. Here is where I am so far!

Still lost!

@SORTED = ();
@CLEANED = ();
$X = 0;
$R = 0;
while ($R < 4)
{
while ($X <= 100)
{
$X++;
# print "I am in X\n";
# # i really do not want to write to the array if
# # it already exists in the array.
# # if array already has X then do not right to the
# # array.
push @CHECKED, $X;
@SORTED = sort {$a <=> $b} @CHECKED;
}
# print "*********I am in R\n";
$R++;
$X = 0;
next;
}

print "Array = @SORTED\n";

$Element2 = 0;

for ($Element1 = 0; $Element1 < @SORTED; $Element++)
{
$Element2 = ($Element1 + 1);
if ($SORTED[$Element1] == $SORTED[$Element2])
{
print "$SORTED[$Element1] == $SORTED[$Element2]\n";
delete $SORTED[$Element2];
$Element1--;
next;
}
else
{
next;
}
}
# # How do i remove duplicates from the array?
# # I know this is wrong but here is my delima!

print "Array = @CLEANED\n";

Jul 19 '05 #2
Bob
Sorry i cut and pasted only half of the code!

@SORTED = ();
@CLEANED = ();
$X = 0;
$R = 0;
while ($R < 4)
{
while ($X <= 100)
{
$X++;
# print "I am in X\n";
# # i really do not want to write to the array if
# # it already exists in the array.
# # if array already has X then do not right to the
# # array.
push @CHECKED, $X;
@SORTED = sort {$a <=> $b} @CHECKED;
}
# print "*********I am in R\n";
$R++;
$X = 0;
next;
}

print "Array = @SORTED\n";

$Element2 = 0;

for ($Element1 = 0; $Element1 < @SORTED; $Element++)
{
$Element2 = ($Element1 + 1);
if ($SORTED[$Element1] == $SORTED[$Element2])
{
print "$SORTED[$Element1] == $SORTED[$Element2]\n";
delete $SORTED[$Element2];
$Element1--;
next;
}
else
{
next;
}
}
# # How do i remove duplicates from the array?
# # I know this is wrong but here is my delima!

print "Array = @CLEANED\n";

Jul 19 '05 #3
bob

the easiest way to do this is to use all elements of your dirty as keys
to a hash. there are shorter forms to this solution, but i'll leave it
verbose for clarity;

hope this helps

-r

################################################## #############
# see
# http://perlmonks.com/index.pl?node=609
# for reference
################################################## #############

use strict;

my @dirty = ( 1, 2, 3, 2, 3, 4, 9, 8, 7, 6, 5, 4, 3, 2, 1);
#
# the hash we will use to track unique elements in @dirty
#
my %occurred;
foreach my $dirtyElement ( @dirty )
{
$occurred{$dirtyElement} = 1;
}
#
# we can now retrieve the unique elements from %occurred
#
my @unique = keys %occurred;

print @unique;

################################################## #############


"Bob" <sp***@realconsultants.com> wrote in news:1109737474.473572.207520
@g14g2000cwa.googlegroups.com:
Sorry i cut and pasted only half of the code!

@SORTED = ();
@CLEANED = ();
$X = 0;
$R = 0;
while ($R < 4)
{
while ($X <= 100)
{
$X++;
# print "I am in X\n";
# # i really do not want to write to the array if
# # it already exists in the array.
# # if array already has X then do not right to the
# # array.
push @CHECKED, $X;
@SORTED = sort {$a <=> $b} @CHECKED;
}
# print "*********I am in R\n";
$R++;
$X = 0;
next;
}

print "Array = @SORTED\n";

$Element2 = 0;

for ($Element1 = 0; $Element1 < @SORTED; $Element++)
{
$Element2 = ($Element1 + 1);
if ($SORTED[$Element1] == $SORTED[$Element2])
{
print "$SORTED[$Element1] == $SORTED[$Element2]\n";
delete $SORTED[$Element2];
$Element1--;
next;
}
else
{
next;
}
}
# # How do i remove duplicates from the array?
# # I know this is wrong but here is my delima!

print "Array = @CLEANED\n";


Jul 19 '05 #4
Here is my attempt:

my %temp = ();
print grep !$temp{$_}++, ((1..10) x 5);

....which is essentially the same concept as you described, but just a
different approach.

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5071
by: Jason | last post by:
I have a number of arrays that are populated with database values. I need to determine which array has the highest ubound out of all the arrays. The array size will always change based on the...
4
5365
by: RSMEINER | last post by:
I'm having a little problem with using Select Not Exists between 2 tables. I get the infamous Primary key "Cannot insert duplicate key" error. This is on MS Sql 2000. I'm trying to create a temp...
4
1971
by: k-man | last post by:
Hi! I'll try to make it short: 1) is memset() optimised is some way (I suppose at various level depending of implementations) and if so, to what level? Is it faster to memset() 4kbytes of...
19
2815
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
6
6825
by: Sean C. | last post by:
Helpful folks, I am having a hard time figuring out how to reduce my percentage of dirty page steal activity. Below are statistics for three fairly normal days, with the bufferpool hit ratios...
2
3584
by: nutthatch | last post by:
I want to be able to import an Excel spreadsheet into Access 2K using the macro command Transferspreadsheet. However, the file I am importing (over which I have no control) contains some records...
2
2269
by: Thomas Baruchel | last post by:
Hi, wondering about: func1: setjmp() ; func2(); func2: {FILE *f; f = fopen(); func3(); fclose(f)} func3 : if() longjmp; else return; Note that FILE *fis a local variable in func2.
8
1772
by: jodleren | last post by:
Hi! I have a function, a part of my code which I can use as a function. It will return 2 arrays, and I am wondering what way to do so. Both arrays hold strings, there are no special keys. 1)...
127
4746
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
0
7041
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7043
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7081
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6737
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4481
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.