×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Compare fields in rows from CSV file

Compare fields in rows from CSV file

Compare fields in rows from CSV file

(OP)
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...


$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

RE: Compare fields in rows from CSV file

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:
@data = ();
while (<CVS>) {
  push(@data, [split(",", chomp($_))]);
}

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

"If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."

RE: Compare fields in rows from CSV file

(OP)
Are you suggesting that I push $row[3] into another array..

Here is what I have...



# Tell Perl we require cgi-lib.pl
#require "./cgi-bin/cgi-lib.pl";

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

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

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

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

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

$num = 0;

while (<CSV>) {

    #Run function to count number of broken states
    ##############################################
    # Remove all the "'s
        s/\"//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 "$row[3] went into a $text[6] state \$num times.\n";
        
}

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


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.

RE: Compare fields in rows from CSV file

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]):

%temp;
map {$temp{$_->[2]} = 1} @data;
if ((scalar(keys(%temp))) > 1)
{
    &more_than_one_unique_name;
}
else
{
    &only_one_unique_name;
}

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:

foreach (@data) {print join(" ", @$_)}

or whatever you need to do.  "perldoc perlref" if you need more help with that...

"If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close