in reply to How to create array from file

You need to split your input on spaces, wrap quotes around non-numeric values, and join the values back with commas.
#!/usr/bin/perl use strict; use warnings; use Scalar::Util qw{ looks_like_number }; use Test::More tests => 1; *ARGV = *DATA{IO}; my $expected = [ ['1,2,\'hello\',\'aerf\''], ['1,2,\'hello\',\'aerf\''], ['1,2,\'hello\',\'aerf\''], ['1,2,\'hello\',\'aerf\''], ['1,2,\'hello\',\'aerf\''], ['1,2,\'hello\',\'aerf\''] ]; my $result = [ map [ join ",", map looks_like_number($_) ? $_ : "'$_'", split " " ], <> ]; is_deeply $result, $expected, 'same'; __DATA__ 1 2 hello aerf 1 2 hello aerf 1 2 hello aerf 1 2 hello aerf 1 2 hello aerf 1 2 hello aerf

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: How to create array from file
by Mjpaddy (Acolyte) on Jan 20, 2017 at 13:09 UTC

    Assume as

    I have two variable and its value changes twice like

    $var1 = 1; # next value 2 $var2 = 'Hello'; # next value 'There'

    Now I wanted it in a array ref as

    $VAR = [ [1,\'Hello\'], [2,\'There\'] ]
    How I able to do that? Please tell me.

      Your expected array ref makes no sense:
      Can't find string terminator "'" anywhere before EOF at ...

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Is this question related to the OPed question? If it is, it's vital for you to recognize that the format of the array elements in the second question
          [1,\'Hello\']
      (a reference to an anonymous array containing two elements: a number and a string) is significantly different from the format of the array elements in the OPed question
          ['1,2,\'hello\',\'aerf\'']
      (a reference to an anonymous array containing one element: a mixed alphanumeric string). Once the structure of the elements of the anonymous array is nailed down (both in our understanding and in your own), useful help can be given.

      Update: Also, you seem to be altering your posts without leaving any note in the altered post that a change has been made (something like this paragraph). Please see How do I change/delete my post? for site etiquette and protocol WRT such changes.

      Update 2 (some days later): Just realized the point choroba was making here, that  [1,\'Hello\'] doesn't even compile! I have to admit I don't really understand what you want (although you seem to have abandoned this thread, so I guess it doesn't matter).


      Give a man a fish:  <%-{-{-{-<