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: Beginner:
Comparing a two lines in one text file.

 



Qwest
Novice

Sep 30, 2008, 11:01 AM

Post #1 of 14 (1560 views)
Comparing a two lines in one text file. Can't Post

Hi,

I am trying to compare two lines in a text file, and then print the difference.



Basically, I have a text file as follows:



Type1 "ObjName1" "Proj1"

Type1 "ObjName1" "Proj2"

Type2 "ObjName2" "Proj3"

Type2 "ObjName3" "Proj4"

Type3 "ObjName4" "Proj5"

Type3 "ObjName4" "Proj6"

...

I want to find the instances where "ObjectNameX" repeats, and print out "ProjX"

Basically, I want to take the above, and Make it look like:

Type1 "ObjName1" "Proj1" "Proj2"

Type2 "ObjName2" "Proj3"

Type2 "ObjName3" "Proj4"

Type3 "ObjName4" "Proj5" "Proj6"

...

The type is moot, (put needs to be printed) as the name has an Identifier that matches the type. The file is already sorted by Type, then ObjName, so duplicate names should be sequential.

I would really appreciate some help on this, I have tried so many different things, and now I am completely baffled.

Thanks!!



[Edit] I also need to add another project to the end "#All" to every line.

(This post was edited by Qwest on Sep 30, 2008, 11:24 AM)


KevinR
Veteran


Sep 30, 2008, 11:24 AM

Post #2 of 14 (1558 views)
Re: [Qwest] Comparing a two lines in one text file. [In reply to] Can't Post

lets see some of the things you tried
-------------------------------------------------


Qwest
Novice

Oct 1, 2008, 11:29 AM

Post #3 of 14 (1546 views)
Re: [KevinR] Comparing a two lines in one text file. [In reply to] Can't Post

Thanks Kevin.

I don't have internet access with the computer that the script is on, and I lost my work when trying to manually type it out.



Rather than type it out again, I will wait till I have internet connection on the pc with perl.



Thanks,
Q


Qwest
Novice

Oct 9, 2008, 8:38 AM

Post #4 of 14 (1513 views)
Re: [KevinR] Comparing a two lines in one text file. [In reply to] Can't Post

Ok, sorry for the long delay, here is what I have so far:

Code
 
open( TEMP, "< END-TEMP.txt" ) ;
while (<TEMP>) {
$i=0; #array counter


foreach $ent(@ent) {
@obj = split( /\s+/); # separate columns by white space characters
$k = $obj[0]; # object type
$o = $obj[1]; #object name
$p = $obj[2]; # Project

if($i=0) {
print STDOUT "$ENT" ;
$i++; # increment array counter.
$oo = $o;
}

elsif($i!=0 && $o eq $oo ){
print STDOUT " $p ";
$i++; # increment array counter.
$oo = $o;
}

else {
print STDOUT " \"#ESI-Anchor\"\n$ENT";
$i++; # increment array counter.
$oo = $o;
}
}
}

close( DATAFILE );
close( PCF );


Let me know if you have any questions.


sycoogtit
User

Oct 9, 2008, 3:20 PM

Post #5 of 14 (1508 views)
Re: [Qwest] Comparing a two lines in one text file. [In reply to] Can't Post

For your sake and for ours,

Code
use strict;


One reason I say that is because you're using @ent, but we have no idea what that is. It could be defined earlier in the program, but we don't know that. If it is, tell us what it is. Also try to fix the indenting. That's too hard to read.

One thing that I can see by glancing at the code is this line


Code
if($i=0) {


That will:
1) Assign 0 to $i, and
2) Evaluate to true

You want

Code
if ($i == 0) {


--
http://bunsooter.com


KevinR
Veteran


Oct 9, 2008, 3:56 PM

Post #6 of 14 (1506 views)
Re: [Qwest] Comparing a two lines in one text file. [In reply to] Can't Post

I think what you want is a hash of arrays. The type and object name are the hash keys and the project would be the array that holds all the projects associated with them.

It s hard to tell by looking at the code you posted which seems to be incomplete or a snippet.
-------------------------------------------------


(This post was edited by KevinR on Oct 9, 2008, 4:05 PM)


KevinR
Veteran


Oct 9, 2008, 4:08 PM

Post #7 of 14 (1503 views)
Re: [Qwest] Comparing a two lines in one text file. [In reply to] Can't Post

Using your sample data this is the basic concept:


Code
my %hash; 
my @order;
while (<DATA>) {
my ($k, $o, $p) = split( /\s+/); # separate columns by white space characters
push @order, "$k $o" unless exists $hash{"$k $o"};
push @{$hash{"$k $o"}},$p;
}
foreach my $type (@order) {
print "$type @{$hash{$type}}\n";
}
__DATA__
Type1 "ObjName1" "Proj1"
Type1 "ObjName1" "Proj2"
Type2 "ObjName2" "Proj3"
Type2 "ObjName3" "Proj4"
Type3 "ObjName4" "Proj5"
Type3 "ObjName4" "Proj6"


Not saying this is the best way, but it should be OK.
-------------------------------------------------


Qwest
Novice

Oct 10, 2008, 5:50 AM

Post #8 of 14 (1492 views)
Re: [KevinR] Comparing a two lines in one text file. [In reply to] Can't Post

Thanks Kevin, Sycoogtit,

I'll give those a try. I am still very new to all of this so bear with me.

I'll add the Use Strict.

And no, @ent is not defined anywhere else, cause I am teh suck.

Thanks,
Qwest


Qwest
Novice

Oct 14, 2008, 5:29 AM

Post #9 of 14 (1439 views)
Re: [KevinR] Comparing a two lines in one text file. [In reply to] Can't Post

Thanks KevinR,

That works great!


Qwest
Novice

Oct 30, 2008, 9:13 AM

Post #10 of 14 (1376 views)
Re: [KevinR] Comparing a two lines in one text file. [In reply to] Can't Post

Hi KevinR,

So, it seems that there are a number of places in the report I am being given, where there are duplicates in the "Proj" values as well. Any way to check for those types of duplicates as well?

For example, there exist a few items like:

Type1 "ObjName1" "Proj1"
Type1 "ObjName1" "Proj1"
Type1 "ObjName1" "Proj2"

Obviously the first 2 entries are completely dupes, and should be ignored.

Any Ideas?

Thanks,
Kent


KevinR
Veteran


Oct 30, 2008, 10:14 AM

Post #11 of 14 (1374 views)
Re: [Qwest] Comparing a two lines in one text file. [In reply to] Can't Post

Don't you want both Proj1 and Proj2 associated with Type1 ObjName1 but no duplicates of ProjX in each TypeX/ObjNameX?
-------------------------------------------------


Qwest
Novice

Oct 30, 2008, 10:33 AM

Post #12 of 14 (1372 views)
Re: [KevinR] Comparing a two lines in one text file. [In reply to] Can't Post

That is exactly right.

Thanks KevinR


KevinR
Veteran


Oct 30, 2008, 12:45 PM

Post #13 of 14 (1369 views)
Re: [Qwest] Comparing a two lines in one text file. [In reply to] Can't Post

Try this:


Code
my %hash;  
my @order;
while (<DATA>) {
my ($k, $o, $p) = split( /\s+/); # separate columns by white space characters
push @order, "$k $o" unless exists $hash{"$k $o"};
push @{$hash{"$k $o"}},$p if ++$hash{"$k $o $p"}<2;
}
foreach my $type (@order) {
print "$type @{$hash{$type}}\n";
}
__DATA__
Type1 "ObjName1" "Proj1"
Type1 "ObjName1" "Proj1"
Type1 "ObjName1" "Proj2"
Type1 "ObjName1" "Proj1"
Type1 "ObjName1" "Proj2"
Type2 "ObjName2" "Proj3"
Type2 "ObjName3" "Proj4"
Type3 "ObjName4" "Proj5"
Type3 "ObjName4" "Proj6"


As long as your file isn't gigantic, should work OK.
-------------------------------------------------


(This post was edited by KevinR on Oct 30, 2008, 12:49 PM)


Qwest
Novice

Oct 30, 2008, 2:25 PM

Post #14 of 14 (1363 views)
Re: [KevinR] Comparing a two lines in one text file. [In reply to] Can't Post

Thanks kevin R, you are a guru among men!

 
 


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

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