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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Arrays for better programming

Status
Not open for further replies.

mellenburg

Programmer
Aug 27, 2001
77
US
I have a script that reads in a text file that is a series of 'Yes' or 'No' responses to questions. The script counts the number of 'Yes' votes for each question. Right now, it's very crude in that it's a series of if-then statements. How would I use an array to make the code a little more sophisticated?

For instance, I can create a list @yesno that contains the responses for each record.

Then I could use the for each @yesno loop.

However, I can't figure out how to use a different counter each time through the loop.

Thank you for any help.
 
I like to use hashes for that sort of thing, generally. Without seeing code it's hard to give you something that exactly matches what you're doing, but say for each question you read in you have the question itself stored in [tt]$question[/tt] and [tt]$answer[/tt] set to the answer (which could be either "yes" or "no").

[tt]$yesCount{$question}++ if lc $answer eq "yes";[/tt]

That's incrementing the value of [tt]$yesCount{$question}[/tt] for every yes answer (using [tt]lc[/tt] so that any casing of "yes" is allowed.)

At the end, you can print out all the counts by doing:
[tt]print "Yes Counts:\n";
foreach my $question (keys %yesCount) {
print "$question: $yesCount{$question}\n";
}
[/tt]
 
Not sure how the file your trying to read in is formatted. but hopefully this code may give your mind a spark to achieve your goal:

#!/usr/bin/perl -w

open (IN, &quot;<tstfile.txt&quot;) or die &quot;Cannot open file: $&&quot;;
@answers = <IN>;
foreach $ans (@answers){
chomp($ans);
if ($ans eq &quot;yes&quot;){
$yes++;
} else {
$no++;
}
}

print &quot;Answer\tNumber\nYes\t$yes\nNo\t$no&quot;;
close IN;

this will work if your file is in the format below

ex. tstfile.txt
yes
no
yes
no
yes
no
yes
yes

if the file is a string such as

yes yes yes no no no yes no

the code above will have errors. but without knowing how your input is formatted it is hard to help.
 
The data is stored the second way

&quot;yes yes yes no no no yes no&quot;

with each answer representing a different question. I was doing something very similar to the second approach

open (IN, &quot;<tstfile.txt&quot;) or die &quot;Cannot open file: $&&quot;;
@answers = <IN>;
foreach $ans (@answers){
chomp($ans);
if ($ans eq &quot;yes&quot;){
At this point I was stuck because it's not just $yes I need, it's $yes for Q1, then $yes for Q2. Is there a way to create variable names using a counter? Such as:

$yes$i++ ; #Where i goes from 0 to 7

where $i is a counter variable that enumerates with each element of @answers? Syntaxwise, I think that's wrong, but if I could create the names $yes0-$yes7 using code, I would be set. Any ideas on how to do that?
 
[tt]$ans[/tt] will contain the entire line for each pass of the [tt]foreach[/tt], so you need to split it up into individual answers (in an array) before you can start counting. You can then keep an array of yes counts, like:

[tt]foreach my $ans (@answers) {
# Split the answers on &quot; &quot; as delimiter
my @indAnswers = split ' ', $ans;

# count from 0 to number of answers - 1
# (0 based arrays)
foreach my $i (0..$#indAnswers) {
# Make sure none of the yesCount elements ends up
# undefined.
$yesCounts[$i] = 0 unless defined $yesCounts[$i];

# Increment the appropriate yesCounts if lowercase
# answer is &quot;yes&quot;
$yesCounts[$i]++ if lc $indAnswers[$i] eq &quot;yes&quot;;
}
}

# Print out the results
foreach my $i (0..$#yesCounts) {
print &quot;$i: $yesCounts[$i]\n&quot;;
}
[/tt]
 
Too quick with the post. You still need your [tt]chomp $ans;[/tt] in there.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top