Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: I'm trying to get a numeric array

by Athanasius (Chancellor)
on Sep 03, 2012 at 14:11 UTC ( #991439=note: print w/replies, xml ) Need Help??

in reply to I'm trying to get a numeric array

Hello, fatmac, and welcome to the Monastery!

As you’ve no doubt found, typing in test data from the console quickly becomes tedious when you’re developing a script. What you need is a way to input data from the script itself but using the same syntax you would use for reading from the console (or from a separate data file). In Perl, that way is provided by the DATA filehandle, which reads input from the bottom of the script file beginning immediately after a line consisting of the __DATA__ token. The following script (which incorporates much of the advice of Gangabass and philiprbrenan, above) illustrates the use of DATA:

#! perl use strict; use warnings; my $fh = \*DATA; # For user input, change to: \*STDIN print "\nMulti Gear Calculator\n\n"; print "Enter Wheelsize (inches): "; my $wheel = <$fh>; print $wheel; chomp $wheel; print "Enter Chainwheel Teeth: "; my $chwhs = <$fh>; print $chwhs; my @chwhs = split /\s+/, $chwhs; print "Enter Cog Teeth: "; my $cogs = <$fh>; print $cogs; my @cogs = split /\s+/, $cogs; my @gears; for (0 .. 8) { $gears[$_ + 1] = $wheel * $chwhs[0] / $cogs[$_]; $gears[$_ + 10] = $wheel * $chwhs[1] / $cogs[$_]; $gears[$_ + 19] = $wheel * $chwhs[2] / $cogs[$_]; } print "\nYour gears are:\n\n"; print 'Wheelsize: ', $wheel, "\n"; print 'Chainwheels: ', join(' ', @chwhs), "\n"; print 'Sprockets: ', join(' ', @cogs), "\n\n"; for (1 .. 27) { printf "%6.2f ", $gears[$_]; print "\n\n" if $_ % 9 == 0; } __DATA__ 26.5 42 32 22 11 13 15 17 19 21 24 28 32

When you’re finished testing, and want to use the script to input data from the console, simply change the line

my $fh = \*DATA;


my $fh = \*STDIN;

and you’re there!

Hope that helps,

Athanasius <°(((><contra mundum

Replies are listed 'Best First'.
Re^2: I'm trying to get a numeric array
by fatmac (Acolyte) on Sep 03, 2012 at 18:49 UTC
    Thankyou very much for your reply Athanasius, that is very interesting, & I have made a note for future reference. I shall try that out in a copy of my program.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991439]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2018-04-23 19:54 GMT
Find Nodes?
    Voting Booth?