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;

to

my $fh = \*STDIN;

and you’re there!

Hope that helps,

Athanasius <°(((><contra mundum


In reply to Re: I'm trying to get a numeric array by Athanasius
in thread I'm trying to get a numeric array by fatmac

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.