[SOLVED] Perl script to continuously compare value from lines
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Perl script to continuously compare value from lines
I have a file called "data.txt". And inside the data file, I have briefly
Code:
PacketID 1 temperature 25
PacketID 2 temperature 26
PacketID 3 temperature 26
PacketID 4 temperature 27
PacketID 5 temperature 28
PacketID 6 temperature 28
May I know the perl script that I need to write so the result will be:
Code:
PacketID 1 temperature 25
PacketID 2 temperature 26
PacketID 4 temperature 27
PacketID 5 temperature 28
The perl script will eliminated the PacketID 3 because PacketID 2 and PacketID 3 have the same value. Same goes to PacketID 6, because it has the same value with PacketID 5.
So, your algorithm is intended to eliminate all records that are not unique in the second column, or just the ones that are consecutive with the record(s) immediately preceding, or.... It is impossible to know without some explanation. In part, I ask because I want to coerce you to think about the problem so that you will better learn how to solve them for yourself, or at the very least, how to compose a question about a problem.
I have a file called "data.txt". And inside the data file, I have briefly
Code:
PacketID 1 temperature 25
PacketID 2 temperature 26
PacketID 3 temperature 26
PacketID 4 temperature 27
PacketID 5 temperature 28
PacketID 6 temperature 28
May I know the perl script that I need to write so the result will be:
Code:
PacketID 1 temperature 25
PacketID 2 temperature 26
PacketID 4 temperature 27
PacketID 5 temperature 28
The perl script will eliminated the PacketID 3 because PacketID 2 and PacketID 3 have the same value. Same goes to PacketID 6, because it has the same value with PacketID 5.
Code:
$id = 0;
open FILE, "id.txt" or die $!;
while (my $line = <FILE>) {
($a1, $a2) = split ' ', $line;
($b1, $b2) = split ' ', $line;
if ($a2 eq $b2) {
print "$a1 $a2\n";
$id = $a1;
}
}
close(FILE);
So, your algorithm is intended to eliminate all records that are not unique in the second column, or just the ones that are consecutive with the record(s) immediately preceding, or.... It is impossible to know without some explanation. In part, I ask because I want to coerce you to think about the problem so that you will better learn how to solve them for yourself, or at the very least, how to compose a question about a problem.
--- rod.
I want to eliminate the consecutive record(s). Any idea? really need some helps...
$ cat LQeminempark.dat
PacketID 1 temperature 25
PacketID 2 temperature 26
PacketID 3 temperature 26
PacketID 4 temperature 27
PacketID 5 temperature 28
PacketID 6 temperature 28
$ ./LQeminempark.pl LQeminempark.dat
PacketID 1 temperature 25
PacketID 2 temperature 26
PacketID 4 temperature 27
PacketID 5 temperature 28
Is your sample data accurately transcribed? What happens if you copy/paste back to a datafile from the LQ posting? Maybe there is some invisible data being lost between you and LQ. Are you sure your copy of the program was accurately transcribed? Looking back at the code, I don't see anything that looks like an assumption or other trap. Maybe someone else can pinpoint a problem.
$ cat LQeminempark.dat
PacketID 1 temperature 25
PacketID 2 temperature 26
PacketID 3 temperature 26
PacketID 4 temperature 27
PacketID 5 temperature 28
PacketID 6 temperature 28
$ ./LQeminempark.pl LQeminempark.dat
PacketID 1 temperature 25
PacketID 2 temperature 26
PacketID 4 temperature 27
PacketID 5 temperature 28
Is your sample data accurately transcribed? What happens if you copy/paste back to a datafile from the LQ posting? Maybe there is some invisible data being lost between you and LQ. Are you sure your copy of the program was accurately transcribed? Looking back at the code, I don't see anything that looks like an assumption or other trap. Maybe someone else can pinpoint a problem.
--- rod.
YEAH!!! it is working now. My command to execute it was wrong. Thank you everyone especially theNbomr...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.