Yoda_Oz has asked for the wisdom of the Perl Monks concerning the following question:
i want to be able to choose wHether to output this to the screen or to a file. at the moment i can only work out how to output it to the screen. how can i change my code so that instead of printing to the screen it returns a value. ie the section of the code here:#!usr/local/bin/perl my $path='CustData.txt'; open(DATA, "<$path") || die "Couldn't open $path for reading: $!\n"; while (<DATA>) { chomp; my ($id, $title, $surname, $forename, $sex, $dob, $vision) = split +(/,/); $HoH{$id} = {'title' => $title, 'surname' => $surname, 'forename' +=> $forename, 'sex' => $sex, 'dob' => $dob, 'vision' => $vision,}; } close(DATA); $range1=1; $range2=10; $range3=1; $range4=10; $person_count=0; print("Range\t10-20\t20-30\t30-40\t41-50\t51-60\t61-70\t71-80\t81-90\t +91-99\n"); print("Count"); for ($counter=0;$counter<9;$counter++) { $range3=$range3+10; $range4=$range4+10; print("\t ".personCount($range3,$range4)); } print("\n"); print("Percent\t"); for ($counter=0;$counter<9;$counter++) { $range1=$range1+10; $range2=$range2+10; printf(" %.0f\t",(percent($range1,$range2))); } print("\n"); sub personCount { ($x,$y) = @_; my ($vision_total,$person_count)=0; foreach my $id (sort keys %HoH) { if( ($HoH{$id}->{'vision'}>=$x) & ($HoH{$id}->{'vision'}<=$y) +) { ++$person_count; $vision_total=$vision_total+$HoH{$id}->{'vision'}; } } return int($person_count); } sub percent { ($x,$y) = @_; $percent = 0; my ($total_person_count,$vision_total,$person_count)=0; foreach my $id (sort keys %HoH) { ++$total_person_count; } foreach my $id (sort keys %HoH) { if( ($HoH{$id}->{'vision'}>=$x) & ($HoH{$id}->{'vision'}<=$y) +) { ++$person_count; $vision_total=$vision_total+$HoH{$id}->{'vision'}; } } return $percent = ($person_count/$total_person_count)*100; }
instead of using print and printf, how do i make it so that that little bit to be printed is stored for later so that when the user chooses to output you can choose to print or save to a file. basically my question is can you have a print statement made into a variable to be used for later? or is this the wrong way of going about this???$range1=1; $range2=10; $range3=1; $range4=10; $person_count=0; print("Range\t10-20\t20-30\t30-40\t41-50\t51-60\t61-70\t71-80\t81-90\t +91-99\n"); print("Count"); for ($counter=0;$counter<9;$counter++) { $range3=$range3+10; $range4=$range4+10; print("\t ".personCount($range3,$range4)); } print("\n"); print("Percent\t"); for ($counter=0;$counter<9;$counter++) { $range1=$range1+10; $range2=$range2+10; printf(" %.0f\t",(percent($range1,$range2))); } print("\n");
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: print to data
by Hue-Bond (Priest) on Sep 06, 2006 at 11:39 UTC | |
by imp (Priest) on Sep 06, 2006 at 12:10 UTC | |
by Yoda_Oz (Sexton) on Sep 06, 2006 at 11:55 UTC | |
|
Re: print to data
by GrandFather (Saint) on Sep 06, 2006 at 11:29 UTC | |
|
Re: print to data
by shmem (Chancellor) on Sep 06, 2006 at 12:00 UTC | |
by Yoda_Oz (Sexton) on Sep 06, 2006 at 12:15 UTC | |
by shmem (Chancellor) on Sep 06, 2006 at 14:12 UTC |