How to avoid duplicate entrees in the bleow text file???

J

Jim Carter

Hi all,

I have the below file (C:\text1.txt) on Windows platform:

---------------------------
Rob: ThirdParty 2002.05
Rob: ThirdParty 4.2
Mike: ThirdParty 2002.05
Mike: ThirdParty 4.2
Ed: ThirdParty 2002.05
Dave: ThirdParty 4.2
Tom: ThirdParty 2002.05
Vince: ThirdParty 4.2
Mary: ThirdParty 2002.05
Mary: ThirdParty 4.2
Lisa: ThirdParty 2002.05
Maria: ThirdParty 4.2
-------------------------

Now, I want to avoid the duplicate entry that has 2002.05. For
example, the output should be as follows:

--------------------
Rob: ThirdParty 4.2
Mike: ThirdParty 4.2
Ed: ThirdParty 2002.05
Dave: ThirdParty 4.2
Tom: ThirdParty 2002.05
Vince: ThirdParty 4.2
Mary: ThirdParty 4.2
Lisa: ThirdParty 2002.05
Maria: ThirdParty 4.2
------------------------


how can I do that with a perl script?

Thanks,
Carter
 
M

Matt Garrish

Jim Carter said:
Hi all,

I have the below file (C:\text1.txt) on Windows platform:

---------------------------
Rob: ThirdParty 2002.05
Rob: ThirdParty 4.2
Mike: ThirdParty 2002.05
Mike: ThirdParty 4.2
Ed: ThirdParty 2002.05
Dave: ThirdParty 4.2
Tom: ThirdParty 2002.05
Vince: ThirdParty 4.2
Mary: ThirdParty 2002.05
Mary: ThirdParty 4.2
Lisa: ThirdParty 2002.05
Maria: ThirdParty 4.2
-------------------------

Now, I want to avoid the duplicate entry that has 2002.05.

how can I do that with a perl script?

Should always trust my instincts. For future reference, had I realized that
you'd already posted this question before writing the code below, I wouldn't
have. I would suggest not posting here in the future unless you've made some
effort to come up with the code on your own, as I for one will ignore any
more posts from you that show such a total lack of initiative on your part.

my %user;

while (defined(my $line = <DATA>)) {
chomp $line;
next unless $line;
split(/:? +/, $line);
if ($user{$_[0]}) {
if ($_[2] == 4.2) {
$user{$_[0]} = $line;
}
}
else {
$user{$_[0]} = $line;
}
}

foreach (sort keys %user) {
print $user{$_}, "\n";
}

__DATA__

Rob: ThirdParty 2002.05
Rob: ThirdParty 4.2
Mike: ThirdParty 2002.05
Mike: ThirdParty 4.2
Ed: ThirdParty 2002.05
Dave: ThirdParty 4.2
Tom: ThirdParty 2002.05
Vince: ThirdParty 4.2
Mary: ThirdParty 2002.05
Mary: ThirdParty 4.2
Lisa: ThirdParty 2002.05
Maria: ThirdParty 4.2
 
G

gnari

Jim Carter said:
[snipped exact same question he asked a few days ago]

Hello there.

a (last) bit of friendly advice:

if you were not happy with tha answers you had when
you posted this the first time, you should have followed-up
un then, explaining in what way.
I may well have been that none of them actually solved
your problem exactly, but they should all have given
you enough clues to try something.

now you just have insulted people that spent some time
on your problem by ignoring them. these people may
be reluctant to help you in the future, if they remember you.

another down to a repost like yours, is that any new
replies you get are likely to be similar to the first ones,
because the context of the older messages is gone.

you should have followed-up with:
I tried that but bla bla bla..
or I do not understand bla bla bla
and I am sure the same people would have helped you more.

do you know what a kill-file is?

gnari

P.S.: *plonk*
 
J

Jürgen Exner

Jim said:
Hi all,

I have the below file (C:\text1.txt) on Windows platform:

---------------------------
Rob: ThirdParty 2002.05
Rob: ThirdParty 4.2
Mike: ThirdParty 2002.05
Mike: ThirdParty 4.2
Ed: ThirdParty 2002.05
Dave: ThirdParty 4.2
Tom: ThirdParty 2002.05
Vince: ThirdParty 4.2
Mary: ThirdParty 2002.05
Mary: ThirdParty 4.2
Lisa: ThirdParty 2002.05
Maria: ThirdParty 4.2
-------------------------

Now, I want to avoid the duplicate entry that has 2002.05. For
example, the output should be as follows:

--------------------
Rob: ThirdParty 4.2
Mike: ThirdParty 4.2
Ed: ThirdParty 2002.05
Dave: ThirdParty 4.2
Tom: ThirdParty 2002.05
Vince: ThirdParty 4.2
Mary: ThirdParty 4.2
Lisa: ThirdParty 2002.05
Maria: ThirdParty 4.2

Not quite sure if I understand your requirements (your description is pretty
vague when it comes to details, e.g. what is supposed to happen is if there
is a duplicate entry for 4.2), but maybe "perldoc -q duplicate": "How can I
remove duplicate elements from a list or array?" will get you started.

jue
 
J

Jim Carter

gnari said:
Jim Carter said:
[snipped exact same question he asked a few days ago]

Hello there.

a (last) bit of friendly advice:

if you were not happy with tha answers you had when
you posted this the first time, you should have followed-up
un then, explaining in what way.
I may well have been that none of them actually solved
your problem exactly, but they should all have given
you enough clues to try something.

now you just have insulted people that spent some time
on your problem by ignoring them. these people may
be reluctant to help you in the future, if they remember you.

another down to a repost like yours, is that any new
replies you get are likely to be similar to the first ones,
because the context of the older messages is gone.

you should have followed-up with:
I tried that but bla bla bla..
or I do not understand bla bla bla
and I am sure the same people would have helped you more.

do you know what a kill-file is?

gnari

P.S.: *plonk*



Hi all,

Thanks for all your kind replies. I was playing with some code and
learing perl. Pl. don't assume that I have insulted you by repeating
the same posting again. I just wanted to see more replies which have
different logic so that I can learn more on perl programming.

Thanks,
Carter.
 
M

Matt Garrish

Jim Carter said:
Thanks for all your kind replies. I was playing with some code and
learing perl. Pl. don't assume that I have insulted you by repeating
the same posting again. I just wanted to see more replies which have
different logic so that I can learn more on perl programming.

A rather specious apology, as far as I'm concerned. You could have said as
much, rather than irritate people the way you did. Simply asking: "How else
might one do it?"; or: "What would be the (dis)advantage of doing it this
way..." would have generated you a lot more good will.

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top