in reply to Re: Input data problem
in thread Input data problem

I am not quite sure i understand your solution. Is there an easier way? Something like: @data = <STDIN>; #or it may be not an array Then somehow the data from array is shifted into separate variable values? Is there any way to do it? I've

Replies are listed 'Best First'.
Re^3: Input data problem
by choroba (Cardinal) on Jan 14, 2014 at 10:20 UTC
    Input is separated by $/, which is a newline "\n" by default. You can set it to something else, but using split is more common:
    my (@data) = split ' ', <>; # ' ' means split on any whitespace.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Great, thanks a lot. that helped. One more thing for this:
      my $data = <STDIN>; my (@data) = split (" ", $data); my $n = shift (@data); my $m = shift (@data); my $a = shift (@data); my $ma = $m/$a; my $na = $n/$a; if (int($ma) != $ma) { $ma = int($ma) +1; } if (int($na) != $na) { $na = int($na) +1; } $x = int($ma*$na); print $x;

      #This script solves the problem of counting the amount of a*a sized block to cover the surface area of the city square, sized n*m.

      When i check n = 1000000000, m = 1000000000, a = 1 i get a result x = 1e+18 instead of 1000000000000000000, despite the fact that i use int operator. could anyone help with printing the exact number?

      thanks in advance, the rest cases pass ok.

        Integer has an upper limit. Perl automatically converts to float if integer cannot be used. I was able to get the desired output with bigint:
        #!/usr/bin/perl use warnings; use strict; my ($n, $m, $i) = split ' ', <>; my $ma = $m / $i; my $na = $n / $i; $ma = int($ma) + 1 if int $ma != $ma; $na = int($na) + 1 if int $na != $na; use bigint; my $x = int($ma * $na); print 0 + $x, "\n";
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^3: Input data problem
by Anonymous Monk on Jan 14, 2014 at 13:21 UTC
    # read one line of input my $line = <STDIN>; # remove the line ending chomp $line; # then either my ($foo, $bar, $baz, $quux) = split(/ +/, $line); # or my (@data) = split(/ +/, $line); # the latter is better if you don't know how many elements you'll get