Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Parse txt file into array of array

by Dr Manhattan (Beadle)
on Jun 06, 2013 at 09:46 UTC ( #1037424=perlquestion: print w/ replies, xml ) Need Help??
Dr Manhattan has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

I want to parse a text file into an array of array, one line per small array, one letter per element of small array

So if I have a text file containg:

Hello

my

name

is

Jack

The array has to look like this:

@MainArray element 1 ---> elements of small array ---> H, e, l, l, o

@MainArray element 2 ---> elements of small array ---> m, y

@MainArray element 3 ---> elements of small array ---> n, a, m, e

@MainArray element 4 ---> elements of small array ---> i, s

@MainArray element 5 ---> elements of small array ---> J, a, c, k

I have something along these lines, but I don't how to add the part where each new line in the text file is added as a new small array in @MainArray. All this does is add every letter to one large @MainArray

open (A1, "<2 normaal.txt") or die "can't open"; my @MainArray; while (<A1>) { my $in = $_; chomp $in; push (@array1, split(//, $in)); }

Thanks in advance for any help, much appreciated

Comment on Parse txt file into array of array
Download Code
Re: Parse txt file into array of array
by hdb (Parson) on Jun 06, 2013 at 09:49 UTC

    You nearly had it:

    push @MainArray, [ split(//, $in) ];
Re: Parse txt file into array of array
by kcott (Abbot) on Jun 06, 2013 at 10:01 UTC

    G'day Dr Manhattan,

    You were almost there. You need to push an arrayref (not an array) of the split characters. Here's my test:

    #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my @MainArray; while (<DATA>) { chomp; push @MainArray, [ split // ]; } print Dumper \@MainArray; __DATA__ Hello my name is Jack

    Output:

    $ pm_file_chars_to_AoA.pl $VAR1 = [ [ 'H', 'e', 'l', 'l', 'o' ], [ 'm', 'y' ], [ 'n', 'a', 'm', 'e' ], [ 'i', 's' ], [ 'J', 'a', 'c', 'k' ] ];

    See also: perldsc - Perl Data Structures Cookbook

    -- Ken

      Thanks that works great! Next question- How do I reference an element if I want to splice it?

      my $length = $#MainArray; for (my $x = 0; $x <= $length; $x++) + { my $smallLength = $MainArray[$#$x]; for(my $q = 0; $q <= $smallLength; $q++) { my $element = $MainArray[$x][$q]; if(some if statement concerning $element) { splice(How do I reference this part?); } } }

        I see hdb has shown the syntax you requested. Be aware that splice can be slow and there are often better alternatives. For instance, if you had intended to use splice to remove lowercase characters, you could have rewritten that entire block of code above as:

        for (@MainArray) { @$_ = grep { ! /[a-z]/ } @$_; }

        Here's my test:

        #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my @MainArray; # Original array populating code while (<DATA>) { chomp; push @MainArray, [ split // ]; } # New alternative to 'splice' code for (@MainArray) { @$_ = grep { ! /[a-z]/ } @$_; } print Dumper \@MainArray; __DATA__ Hello my name is Jack

        Output:

        $ pm_file_chars_to_AoA_2.pl $VAR1 = [ [ 'H' ], [], [], [], [ 'J' ] ];

        -- Ken

Re: Parse txt file into array of array
by jwkrahn (Monsignor) on Jun 06, 2013 at 10:31 UTC
    open (A1, "<2 normaal.txt") or die "can't open"; my @MainArray; while (<A1>) { my $in = $_; chomp $in; push (@array1, split(//, $in)); }

    Another way to do it:

    open my $A1, '<', '2 normaal.txt' or die "can't open '2 normaal.txt' b +ecause: $!"; my @MainArray; while ( my $in = <$A1> ) { push @MainArray, [ $in =~ /[[:alpha:]]/g ]; }
Re: Parse txt file into array of array
by hdb (Parson) on Jun 06, 2013 at 10:52 UTC

    Or as a one-liner:

    my @MainArray = map { chomp; [ split // ] } <A1>;

    UPDATE: or even

    my @MainArray = map [ /(.)/g ], <A1>;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1037424]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2014-08-28 00:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (253 votes), past polls