CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Duplicates

 



perlkid
stranger

May 24, 2000, 3:10 AM

Post #1 of 6 (874 views)
Duplicates Can't Post

 
I've tried this many times and I have never been able to get it to work. I need to get lines to stop writing to a file when they already exist. Does anyone have any ideas? I have four fields in the data base, and I need my program to skip the line if the first field is equal to the one in the data base. Should I open the same data base twice and read it once to some how compare. I think I tried that before though.

Thanks,

perlkid


monocle
User

May 24, 2000, 8:19 AM

Post #2 of 6 (874 views)
Re: Duplicates [In reply to] Can't Post

I don't know if there is an more eligant way of doing this but I think this should work. If the new line is in fact a "new Line", it will be appended to the end of the file.

I assumed a two things:
1) your data file is a ":" delimited txt file.
2) your four data fields are coming from a form.
-adjust script accordingly-

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


#Open File And Read All Lines
open(LINES,"$datafile");
flock(LINES, 2);
@items = <LINES>;
flock(LINES, 8);
close(LINES);


#Compare Data Fields Line By Line
$line_is_there = "0";
foreach (@items) {
($data_field_1, $data_field_2, $data_field_3, $data_field_4) = split(/\:/,$_);
if ($data_field_1 eq "$FORM{'data_field_1'}") {
$line_is_there = "1";
last;
}
}


#Open File To Append New Line
if ($line_is_there == "0") {
open(LINES,">>$datafile");
flock(LINES, 2);
print LINES "$FORM{'data_field_1'}:$FORM{'data_field_2'}:$FORM{'data_field_3'}:$FORM{'data_field_4'}\n";
flock(LINES, 8);
close(LINES);
}
</pre><HR></BLOCKQUOTE>

Right before posting this I questioned whether I had misunderstood your post, but I decided to reply anyway. Hope this helps. If not, oops on my part.

Monocle
Hear techno music by Monocle here: http://www.mp3.com/monocle


perlkid
stranger

May 24, 2000, 2:02 PM

Post #3 of 6 (874 views)
Re: Duplicates [In reply to] Can't Post

 
Thank you.

That looks like what I've been looking for. I'll play with that for a while and let you know if that works.

Thanks

Perlkid


perlkid
stranger

May 25, 2000, 2:09 AM

Post #4 of 6 (874 views)
Re: Duplicates [In reply to] Can't Post

 
I tried that and I couldn't get it to work. So now I would like to do someting else.

I want to open a file full of data diliminated by | into four fields. There are duplicates it the file. Then I want my program to look at field four and if there is more than one line with the same value it will not print the line to an out put file.

Something like this,

open(DATA, "<data.db");
@all=<DATA>;
close(DATA);

foreach $line (@all)
{
($field1, $field2, $field3, $field4)=split(/\|/, $line);
if ($field4 = /next; $field4/)
{
next;
}
open(OUT, ">>out.db");
print OUT "$line";
close(OUT);
}

Of course that wouldn't work but It's just to you to understand what I'm trying to do.

Thanks A lot,

perlkid


TheGame+
Deleted

May 25, 2000, 3:28 AM

Post #5 of 6 (874 views)
Re: Duplicates [In reply to] Can't Post

Use a hash :

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

open(INPUT,"<data.db") &#0124; &#0124; die "Oops, can't open data.db - $!\n";
open(OUTPUT,">out.db") &#0124; &#0124; die "Oops, can't open out.db - $!\n";
while (<INPUT> ) {
chomp;
next if /^$/; # remember this ?
($field1, $field2, $field3, $field4)=split(/\|/);
if (exists($seen{$field4})) {
# hmmm, we've seen this one before
}
else {
print OUTPUT "$field1\t$field2\$field3\t$field4\n";
$seen{$field4} = 'gotcha!';
}
}
close(INPUT);
close(OUTPUT);
</pre><HR></BLOCKQUOTE>


perlkid
stranger

May 25, 2000, 1:41 PM

Post #6 of 6 (874 views)
Re: Duplicates [In reply to] Can't Post

 
Thank You So Much The Game,

But how can I have it ignore case and/or periods and other unimportant characters when removing duplicates.

Not A terribly urgent question.

Thank You Once Again TheGame,

It Works So Well,

Perlkid

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives