Name: Tina Date: December 9, 2003 at 20:52:31 Pacific Subject: Perl: Split Function & Other Proble OS: WIN98 CPU/Ram: 64K
Comment:
I could really use some help on this code as I have been working on it day and night for days now.
Basically, what I am trying to do is 1) Read the file "usernames" into an array and print (usernames was created from field #4 of ourpasswd); 2) Read the file "ourpasswd" into an array and use the split function, using ":" as the field delimiter, to perform various tasks including the following: a) print out to total number of records, b) print out duplicated user names with the number of records for each such name, and c) the percentage of records corresponding to all duplicates in total number of records.
Here's what I've come up with so far:
#!/usr/bin/perl
#open file usernames
open (INP, "usernames") || die ("Cannot open file"); @file=<INP>; #create array while($file=<INP>) {$table{$line}=0; } #initialize table print "List usernames file:\n\n "; #print contents print @file;
#open file ourpasswd
open (INFILE, "ourpasswd" ) || die ("Cannot open file") ; @line=<INFILE>; #create array $counter=0; #set counter to zero
print ("There are $counter records in the file\n");
close(INP);
close(INFILE);
I'm not sure if I've got the while loops set up correctly and I am also having trouble figuring our the split function. I've also done some testing on printing the variables but that's not working either. I'm stuck!!! Any help would be greatly appreciated.
My first recommendation would be to enable warnings and use the strict pragma. So, the beginning of the script would look like this:
#!/usr/bin/perl -w
use strict;
#open file usernames
This will allow you to see several of the problems and forces you to declare your vars (which helps to maintain proper scoping of the vars). From what you're describing, since the usernames file is extracted from the "ourpasswd" file, there is no need to read-in both files (unless it's a requirement for a homework assignment). Here's an example of how to use split (within the loop).
@fields = split /:/, $line;
I'm late for work so, I'll check back-in later with more info/help.
Hopes this helps you to understand better what I am working with. Again, I'm not sure if I've got the while loops set up correctly and I am also having trouble figuring out the split function. I've also done some testing on printing the variables but that's not working either. I'm still stuck!!! Any help would be greatly appreciated.
OK.. I am looking at the piece of code starting from #open file ourpasswd
you open the file giving it a file handle <INFILE>
Then you attempt to put it in a list: @line=<INFILE>;
Then start your loop with the file handle again.. ignoring the poor list!: while ($line=<INFILE>)
So you are now examining the file line by line each time putting it in the variable $line.
from here you can start splitting the variable into a list of fields..
Heres a start: ##################################
$maincounter=0; #set the counter to zero
#open the file or create error open (INFILE,"ourpasswd.txt") or die("Could not open file [ourpasswd.txt].");
#read the file line by line while ($line = <INFILE>) { chomp($line); # remove the newline char from $line.
(@fields)= split(/:/,$line); # split var into list
#lets see what we have in field 4 print "DEBUG: field name is: [$fields[4]]\n"; $maincounter++; } #Fallen out of loop and print total count print "total count = [$counter]";
################################
Now you can start counting the results of the field with your if statement.
If you get stuck the following may help: (copy and paste to a txt file and it will be more user friendly:)) If you need me to explain it just shout!
################################
#!/usr/bin/perl -w use strict;
my $maincounter=0; #set the counter to zero my $line; #var to hold file line while processing my @fields; #list to hold split line var my %user; #Hash to count usernames my $duplicates; #Var to count the totalnumber of duplicate my $percent; #Var to calculate the percentage of duplicates
#open the file or create error open (INFILE,"ourpasswd.txt") or die("Could not open file [ourpasswd.txt].");
#read the file line by line while ($line = <INFILE>) { chomp($line); # remove the newline char from $line.
(@fields)= split(/:/,$line); # split var into list
#if field 4 has a value then add it to the array $user if ($fields[4]){ $user{$fields[4]}++; } $maincounter++; #end of processing this line increment counter }#end of while
$duplicates = 0;# declare var and set to 0
#for each name in the hash "user", if the count is greater than 1 it is a duplicate so print foreach $a(keys %user){ if ($user{$a}>1){ print "count for duplicate user [$a] is [$user{$a}]\n";
$duplicates = ($duplicates+($user{$a}-1)); # add to total duplicate count (minus 1 because 3 entries means only 2 duplicates) }#end of if }#end of foreach
$percent = (100/$maincounter)*$duplicates;#calculate percentage from results above
#Lets print our results: print "\ntotal count = [$maincounter]\n"; print "total number of duplicates = [$duplicates]\n"; print "percentage of duplicates = [$percent]\n";
Since this is clearly a homework assignment, we're only allowed to guide Tina so that she can learn by writing the script herself. By rewriting and providing an entire (homework) script, you violate computing.net's user agreement. If Tina submits your rewrite as her work, it's plagiarism which helps no-one.
Chris -- thank you for your help. I haven't had a chance to really look over your response yet but I will take a look at it this evening or tomorrow. I just wanted to post a response to let you know that I appreciate your help. I also sincerely appreciate your taking the time to explain the code with all of your comment inserts.
Fish -- I can appreciate your concerns about plagarism. Actually, my assignment was due a couple of days ago (Tuesday by midnight). After the due date of every assignment the instructor usually gives tips (very obvious tips in which sometimes he just comes right out and tells us what to do) to help revise/complete the assignment and, once complete, deducts 11 points from the total score for being late. At any rate, I agree with your remarks.
The information on Computing.Net is the opinions of its users. Such
opinions may not be accurate and they are to be used at your own risk.
Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE