Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, i consider myself a newbie in Perl, so i need a bit of help. There are some websites, where you got different problems to solve using various programming languages (i'm using perl). All problems got automated tests and just give you input and check the output. Well i've got the problem with the input. For example, after script was started, the input will be 1 10 8 6 - in ole line, separated with spaces. I need to store those 4 values in 4 different variables and i don't know hot to do it in terms of perl. Help me please.

Replies are listed 'Best First'.
Re: Input data problem
by Athanasius (Archbishop) on Jan 14, 2014 at 09:22 UTC
      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
        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.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        # 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