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

Hello All!

I have a question, how to create array as follows:

input file

1 2 hello aerf 1 2 hello aerf 1 2 hello aerf 1 2 hello aerf 1 2 hello aerf 1 2 hello aerf
$VAR1 = [ ['1,2,\'hello\',\'aerf\''], ['1,2,\'hello\',\'aerf\''], ['1,2,\'hello\',\'aerf\''], ['1,2,\'hello\',\'aerf\''], ['1,2,\'hello\',\'aerf\''], ['1,2,\'hello\',\'aerf\''] ];

Really appreciate any help.

use Data::Dumper; open FH, "sap.txt" or die "$!"; while(<FH>){ chomp $_; push(@data,$_); } print Dumper \@data; close FH; __DATA__ $VAR1 = [ '1 2 hello aerf', '1 2 hello aerf', '1 2 hello aerf', '1 2 hello aerf', '1 2 hello aerf', '1 2 hello aerf' ]; #now how I can convert this into the above output

Replies are listed 'Best First'.
Re: How to create array from file
by choroba (Cardinal) on Jan 20, 2017 at 12:29 UTC
    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,

      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:  <%-{-{-{-<

Re: How to create array from file
by Corion (Patriarch) on Jan 20, 2017 at 12:14 UTC

    Your intended output format makes little sense at first glance. Maybe you really want to use Text::CSV_XS to read in comma-separated files into arrays?

    If you really, really want to read a file into an array of arrayrefs all of which contain only one element, see open, and readline.

    You can help us give you more appropriate advice by showing us what code you have already written and what input data you give it and how your code fails to do what it should.

      Hi Corion, I edit my question with code please suggest out to get that output.

        If you really, really want single-element arrayrefs, don't push the string, but push an array reference containing the string:

        while(<FH>){ chomp $_; push(@data,[$_]); }

        But really, reconsider using Text::CSV_XS instead, because soon you will want to split up that string on commas whitespace using split and then you'll find that Text::CSV_XS is much better. Also see perldsc on data structures maybe.

        Upon reading chorobas reply, I see that I completely forgot about quoting the other parts on output. Oh well.