You might want to use a hash of hashes (HoH) data structure to hold all of your data in one place. You can also use a while loop to read from STDIN until some end condition - here I use CTRL-D to signal eof as I'm on *nix. The Data::Dumper module is helpful when trying to visualise data and I think printf (and sprintf) will do your rounding for you.
#!/usr/bin/perl # use strict; use warnings; use feature qw{ say }; use Data::Dumper; my $prompt = q{-> }; my $dePrompt = qq{\r} . q{ } x ( length( $prompt ) + 2 ) . qq{\r}; my %RNA; print qq{Please enter name, conc. and vol. (CTRL-D to finish):\n}; print $prompt; while ( 1 ) { do { print $dePrompt; last; } if eof STDIN; chomp( my $line = <STDIN> ); my( $name, $conc, $vol ) = split m{\s+}, $line; $RNA{ $name } = { conc => $conc, vol => $vol, }; print $prompt; } print Data::Dumper->Dumpxs( [ \ %RNA ], [ qw{ *RNA } ] ); say q{-} x 50; foreach my $name ( sort keys %RNA ) { printf qq{%10s%8.2f%6d%8.2f%8.2f%8.2f\n}, $name, $RNA{ $name }->{ conc }, $RNA{ $name }->{ vol }, $RNA{ $name }->{ conc } * $RNA{ $name }->{ vol }, 30 / $RNA{ $name }->{ conc }, 50 / $RNA{ $name }->{ conc }; }
Here's a run of it.
Please enter name, conc. and vol. (CTRL-D to finish): -> Ash2 1.85 150 -> Ja2_1 1.89 150 -> Ja2_2 2.49 100 -> Set1_1 1.55 150 -> Set1_2 2.88 100 %RNA = ( 'Set1_2' => { 'vol' => '100', 'conc' => '2.88' }, 'Ja2_1' => { 'vol' => '150', 'conc' => '1.89' }, 'Ash2' => { 'vol' => '150', 'conc' => '1.85' }, 'Set1_1' => { 'vol' => '150', 'conc' => '1.55' }, 'Ja2_2' => { 'vol' => '100', 'conc' => '2.49' } ); -------------------------------------------------- Ash2 1.85 150 277.50 16.22 27.03 Ja2_1 1.89 150 283.50 15.87 26.46 Ja2_2 2.49 100 249.00 12.05 20.08 Set1_1 1.55 150 232.50 19.35 32.26 Set1_2 2.88 100 288.00 10.42 17.36
Note that my use of ^D and the $dePrompt string tidies away the last prompt. I hope this is helpful.
Cheers,
JohnGG
In reply to Re: a simple work task
by johngg
in thread a simple work task
by polycomb
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |