Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare fields in rows from CSV file

Status
Not open for further replies.

AcidHawk

Technical User
Mar 14, 2001
17
ZA
Hi,

I am wanting to find uniqueness in a csv file.

Detail:

the csv file looks loke this..

DATE,Time,Name,Location
03/03/2001,08:00:00,Server1,DBN
04/03/2001,13:20:13,Server1,DBN
04/03/2001,15:35:00,Server2,CPT
04/03/2001,16:00:16,Server1,JHB

etc...


I want to do something different if there are other names in the Name field.. if there is only 1 unique name I want to count how many DBN's there are ... if there are more than 1 name in the Name field I want to do something else..

I am most interested in finding whether the name in the Name field is unique or if there are others in the same field also...

here is what I have so far...

Code:
$CSV='C:/TEMP/tst.csv";


# Open CSV that was created Earlier
open CSV or die "Cannot open $CSV for read $!";

$num = 0;
while (<CSV>) {

# Create an array from each line with the ',' as the seperator		

		@row = split /,/ ;
	
		$num++ ;
}

now I can see $row[3] but how do I compare it to the next row or the ones after it ...??

Rgds
Sean
 
a couple of ways, however, the best (in my opinion) would be to put them all into a single two-dimentional array. make the reading in of the file a single while loop:[tt]
@data = ();
while (<CVS>) {
push(@data, [split(&quot;,&quot;, chomp($_))]);
}[/tt]

from here, there's a perl FAQ on tek-tips for determining uniqueness, or you could do other comparisons, and then do the stuff you wanted to do with the records, probly with a foreach loop. remember to dereference the entries to compare them...
HTH &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Are you suggesting that I push $row[3] into another array..

Here is what I have...

Code:
# Tell Perl we require cgi-lib.pl
#require &quot;./cgi-bin/cgi-lib.pl&quot;;

$CSV=&quot;C:/mydocs/myweb/occ/tst.csv&quot;;

# Create Header for CGI
print &quot;Content-type:text/html\n\n&quot;; 

print &quot;<html><head><title>Summary Downtime Report</title></head>\n&quot;;
print &quot;<body><center><h2>Summary Downtime Report</h2>\n&quot;;

#Read Start Date and End Date Still need to work on this one...
print &quot;Period: ..start-date.. to ..end-date.. </center>\n&quot;;
print &quot;<hr><p><p><DIV>\n&quot;;

# Open CSV that was created Earlier
open CSV or die &quot;Cannot open $CSV for read $!&quot;;

$num = 0;

while (<CSV>) {

	#Run function to count number of broken states
	##############################################
	# Remove all the &quot;'s
		s/\&quot;//ig;
		
	# Create an array from each line with the ',' as the seperator		
		@row = split /,/ ;
		
		@text = split / /, $row[23];

	# Function to count the number of times the machine went broken
		if ($text[6] eq Broken) {
				
	#foreach server name that was selected print uniq names and number out...
                     $num++;
                }
		print &quot;$row[3] went into a $text[6] state \$num times.\n&quot;;
		
}

# Finish the HTML stuff
print &quot;</DIV><hr><p>&quot;;
print &quot;<br></body></html>&quot;;

I am wanting to send the data to a html page.. thus the CGI stuff...

As you can see a BRAND SPANKING NEWBIE programmer...

If you run this on the CSV file I have .. you will see the problem...

Rgds
Sean

I have looked into a FAQ but am not sure that this is a hash..

----
Of All the things I've lost in my life it's my mind I miss the most.
 
well, basically, yes, but my code actually pushed the whole array that is each row onto another array, but as a reference. either way, before you even start printing out things, you need to have all the data from the file in one your program. this is really the only way to be able to compare all the data against the rest of it. after the file has been read, you want to see if there are different names or not, for which you'll make a hash, then count the keys (simplest way to counting unique elements)(also note, the third element of the array, the name field, is indexed as [2]):
[tt]
%temp;
map {$temp{$_->[2]} = 1} @data;
if ((scalar(keys(%temp))) > 1)
{
&more_than_one_unique_name;
}
else
{
&only_one_unique_name;
}
[/tt]

you'd of course then make the subroutines, or just replace them with the code needed. then, to use a 2-D array to print things out, you need to dereference it as you go:
[tt]
foreach (@data) {print join(&quot; &quot;, @$_)}
[/tt]

or whatever you need to do. &quot;perldoc perlref&quot; if you need more help with that... &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top