chomp $_; my $line = $_; my @array = map { s/^\s+//; # strip leading spaces s/\s+$//; # strip tailing spaces $_ # return the modified string } split '\s+', $line;

You don't need anything this complicated.    Just do this:

my @array = split;


print @array."\n"; # prints number of elements instead of whole array

The concatenation operator (.) forces scalar context on its operands and an array in scalar context returns the number of elements in the array.    You need to use list context instead:

print @array, "\n";


$hash->{$line_num}=@array; # does not works because of previous issue

Again you are forcing scalar context on the array which evaluates to the number of elements in the array.    If you want to store the contents of the array then you have to either store a reference of the array:

$hash->{ $line_num } = \@array;

or copy the array to an anonymous array.

$hash->{ $line_num } = [ @array ];


In other words your while loop could be written more simply as:

while ( <FH> ) { $hash->{ $. } = [ split ]; }


But if you are using the line number as hash keys then you should probably be using an array instead of a hash.

my @data; while ( <FH> ) { push @data, [ split ]; }

Or:

my @data = map [ split ], <FH>;

In reply to Re: help with split into array by jwkrahn
in thread help with split into array by dusoo

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.