Home: Perl Programming Help: Beginner:
New Program


Sep 28, 2012, 6:29 AM

Views: 3705
New Program

Hi Guys / Girls,

I want to make a program for myself / school project:

In plain words it is like this:

Start stopwatch

Create typing space (like <>)

Make the stopwatch note the time every time the "space-button" is pressed.

I am not asking anyone to make this for me, but I would like some info at what I could you best for this. I've made like 30 kinds of different programs for this, but my timer from HiRes is colliding with my input, so it didn't come out just yet.

Anyway hope some of you can help.



Veteran / Moderator

Sep 28, 2012, 7:00 AM

Views: 3701
Re: [HarryAchterdeur] New Program

Please post your script and a specific question on the part that is giving you trouble and include any errors/warnings that your script generates.


Sep 28, 2012, 7:24 AM

Views: 3698
Re: [FishMonger] New Program

Ok I allready tried something like that but it didn't help me much.

But ill try again because I couldnt wright my problem down the way I wanted it, but here it comes:


#! usr/local/bin/perl
use diagnostics;
local $/ = " "; #this changes the input seperator from "enter" to "space"

use Time::HiRes; #this is the module I use for the Time Function

$start1 = [ Time::HiRes::gettimeofday( ) ]; #Here I start the time function

print " "; #I just let it stay here
$input1 = <>; #here I ask for the first word; remember, when I type a sentence it is devided bij the RS by space. (so each input is 1 word).

$elapsed1 = Time::HiRes::tv_interval( $start1 ); #here i redirect the function to the elapsed time

print "Woord 1: $input1\n"; #print the word

print "Tijd van woord 1: $elapsed1 seconden!\n"; #here i print the amount of seconds it took me to type the word

#Now I want to start the second word, and this is exactly where it goes wrong. If you look to the output of the screen you can see that the first
#timer function is going on till the input in total stops (so until the last of my functions). The rest of the functions are worthless as you can see.
#Only the first timer works, and it does not stop here.
$start2 = [ Time::HiRes::gettimeofday( ) ];

print " ";
$input2 = <>;

$elapsed2 = Time::HiRes::tv_interval( $start2 );

print "Woord 2: $input2\n";

print "Tijd van woord 2: $elapsed2 seconden!\n";

#Now I want to start the third word
$start3 = [ Time::HiRes::gettimeofday( ) ];
print " ";
$input3 = <>;

$elapsed3 = Time::HiRes::tv_interval( $start3 );

print "Woord 3: $input3\n";

print "Tijd van woord 3: $elapsed3 seconden!\n";

#Now I want to start the fourth word
$start4 = [ Time::HiRes::gettimeofday( ) ];
print " ";
$input4 = <>;

$elapsed4 = Time::HiRes::tv_interval( $start4 );

print "Woord 4: $input4\n";

print "Tijd van woord 4: $elapsed4 seconden!\n";

#Now I want to start the fifth word
$start5 = [ Time::HiRes::gettimeofday( ) ];
print " ";
$input5 = <>;

$elapsed5 = Time::HiRes::tv_interval( $start5 );

print "Woord 1: $input5\n";

print "Tijd van woord 5: $elapsed5 seconden!\n";

#Now I want to start the sixth word (sorry for spelling)
$start6 = [ Time::HiRes::gettimeofday( ) ];
print " ";
$input6 = <>;

$elapsed6 = Time::HiRes::tv_interval( $start6 );

print "Woord 6: $input6\n";

print "Tijd van woord 6: $elapsed6 seconden!\n";

#Now I want to start the seventh word
$start7 = [ Time::HiRes::gettimeofday( ) ];
print " ";
$input7 = <>;

$elapsed7 = Time::HiRes::tv_interval( $start7 );

print "Woord 7: $input7\n";

print "Tijd van woord 7: $elapsed7 seconden!\n";

#Now I want to start the eight word
$start8 = [ Time::HiRes::gettimeofday( ) ];
print " ";
$input8 = <>;

$elapsed8 = Time::HiRes::tv_interval( $start8 );

print "Woord 8: $input8\n";

print "Tijd van woord 8: $elapsed8 seconden!\n"

Output (txt):
Woord 1: Hi
Tijd van woord 1: 12.09375 seconden!
Woord 2: there
Tijd van woord 2: 1.8e-005 seconden!
Woord 3: my
Tijd van woord 3: 1.3e-005 seconden!
Woord 4: name
Tijd van woord 4: 1.2e-005 seconden!
Woord 1: is
Tijd van woord 5: 1.1e-005 seconden!
Woord 6: harry
Tijd van woord 6: 1.1e-005 seconden!
Woord 7: and
Tijd van woord 7: 1e-005 seconden!
Woord 8: im
Tijd van woord 8: 1.1e-005 seconden!

Can you please help me (problem is in code).

With kind regards,


Veteran / Moderator

Sep 28, 2012, 8:00 AM

Views: 3695
Re: [HarryAchterdeur] New Program

You have too much code duplication.

Since this is your class assignment, I can't provide the code, but I can give you hints/advice.

Write a while loop that 1) initializes the timer, 2) gets the input, 3) calculates the interval, 4) outputs the info.

Since you're having the user enter a single word each time instead of a string, there is no need to reset the input record separator.

If you import the function names in the use statement, you wont need to use the full package name when calling the functions.

Veteran / Moderator

Sep 28, 2012, 8:36 AM

Views: 3691
Re: [HarryAchterdeur] New Program

Rather than using so many variables ($input1, $input2, $input3, ...$elapsed1, $elapsed2, ..., $start1, $start2)), I think you should at least use a series of arrays. For example, $input[1], $start[1], etc., and loop on the indices.

That would enable you to reduce drastically the code replication (thereby limiting the risk of errors).

In fact a more complex data structure (array of hashes or hash or arrays) would probably be better, but at the very least, use arrays for those three series of variables.

Veteran / Moderator

Sep 28, 2012, 8:48 AM

Views: 3690
Re: [Laurent_R] New Program

IMO, using multiple arrays only make sense if you need/want to access each individual piece of data at any point in the script. From my understanding of the requirements, that's not needed.

From what I see, the key vars should be 3 scalars ($input, $start, $elapsed) and possible one array (@input) to build "a sentence".

Veteran / Moderator

Sep 28, 2012, 10:47 AM

Views: 3683
Re: [FishMonger] New Program

Yes, you are right, from what we can see of the code, the same variables could be simply reused throughout, rather than having to duplicate them as $start1, $start2, etc. I was thinking of arrays in the event that the values need to be used at a later point not shown in the posted code, in which case they would need to be stored.


Oct 1, 2012, 12:27 AM

Views: 3620
Re: [FishMonger] New Program

Hi guys,

Thanks for the help with this. @FishMonger the problem is that I want the users to write down an entire text without interruptions. So they have to type a text that is like 300 words. Then I want the program to brake down the words and sentences and put a timestamp on each spacebar that is pushed (so I can calculate what time has passed for that significant word). But with the input function I don't think it is possible, or at least very difficult for me. Maybe it works better if I let the program spin when a user is typing in MSword or something like that. But then again I have no idea how to do that either..

Btw im not programming at school, or learing to program at school. I created my own project and thought I could script this into my project... but I underestimated it.

I will try to use it with the while function... maybe I'll have more questions then to post..

Anyway thank you guys very much



Oct 3, 2012, 5:25 AM

Views: 3451
Re: [HarryAchterdeur] New Program



Oct 4, 2012, 12:24 AM

Views: 3404
Re: [HarryAchterdeur] New Program

printing solved does not help curious minds that stumble onto your request for help.
please post the solved code so we can all see :)