Read File into multi-dimensional array ignore whitespace

Discussion in 'Perl Misc' started by vorticitywolfe@gmail.com, Jun 27, 2007.

  1. Guest

    Hello, a newbie question...

    I have a file that looks like this:
    ########test.txt#########
    28 0742 1715 0656 1800 0602 1839 0506 1920 0430 1956 0427 2011
    29 0741 1716 0600 1840 0505 1922 0429 1957 0427 2011
    0454
    30 0740 1718 0558 1842 0503 1923 0429 1958 0428 2011
    0455
    31 0739 1719 0556 1843 0428
    1959 0457
    #######################

    and I read it with this code:

    ###########test.pl###########
    while(my $line = <INPUT>){
    push @AoA, [ split ];
    }
    ###########################

    I am trying to read it into a multi dimensional array, much like the
    perldoc perllol; however, I'm running into a couple of problems.

    1.) What does this mean in the documentation, i.e. I don't get what
    function the line should be calling , and it is not explained? I see
    that it is trying to index $AoA with row $x and column $y, but the
    function is throwing me off.

    for $x (1 .. 10) {
    for $y (1 .. 10) {
    $AoA[$x][$y] = func($x, $y);
    }
    }

    2) the push@AoA, [ split ]; thinks the 3rd column is blank space and
    combines the array into 8 elements (for the last line ) as opposed to
    13 for the first).

    3.) If I print @AoA; I get the: ARRAY(BLAH BLAH) ARRAY(BLAH BLAH). I
    know this is because I am not printing a scalar. But the question here
    is: Is there a way to print an array without for looping through each
    element?

    Thanks for all your help in advance!

    Jonathan
    , Jun 27, 2007
    #1
    1. Advertising

  2. On Jun 27, 2:26 pm, wrote:
    > Hello, a newbie question...
    >
    > I have a file that looks like this:
    > ########test.txt#########
    > 28 0742 1715 0656 1800 0602 1839 0506 1920 0430 1956 0427 2011
    > 29 0741 1716 0600 1840 0505 1922 0429 1957 0427 2011
    > 0454
    > 30 0740 1718 0558 1842 0503 1923 0429 1958 0428 2011
    > 0455
    > 31 0739 1719 0556 1843 0428
    > 1959 0457
    > #######################
    >
    > and I read it with this code:
    >
    > ###########test.pl###########
    > while(my $line = <INPUT>){
    > push @AoA, [ split ];}
    >
    > ###########################
    >
    > I am trying to read it into a multi dimensional array, much like the
    > perldoc perllol; however, I'm running into a couple of problems.
    >
    > 1.) What does this mean in the documentation, i.e. I don't get what
    > function the line should be calling , and it is not explained? I see
    > that it is trying to index $AoA with row $x and column $y, but the
    > function is throwing me off.
    >
    > for $x (1 .. 10) {
    > for $y (1 .. 10) {
    > $AoA[$x][$y] = func($x, $y);
    > }
    > }


    Don't worry about the function call. That's just an example to show
    what's possible.

    > 2) the push@AoA, [ split ]; thinks the 3rd column is blank space and
    > combines the array into 8 elements (for the last line ) as opposed to
    > 13 for the first).
    >


    right, you're creating jagged arrays because the split function
    doesn't care what whitespace character to split on or how many if
    called with no explicit delimiter. there's no way for perl to know how
    many columns there SHOULD be. you need to be more precise in your
    description of your data. is it only one space character between each
    value? is it a tab?

    > 3.) If I print @AoA; I get the: ARRAY(BLAH BLAH) ARRAY(BLAH BLAH). I
    > know this is because I am not printing a scalar. But the question here
    > is: Is there a way to print an array without for looping through each
    > element?
    >


    you can always use Data::Dumper.
    e.g.
    print Dumper( \@AoA ), "\n";

    > Thanks for all your help in advance!
    >
    > Jonathan
    it_says_BALLS_on_your forehead, Jun 27, 2007
    #2
    1. Advertising

  3. Guest

    On Jun 27, 6:51 pm, it_says_BALLS_on_your forehead
    <> wrote:
    > On Jun 27, 2:26 pm, wrote:
    >
    >
    >
    > > Hello, a newbie question...

    >
    > > I have a file that looks like this:
    > > ########test.txt#########
    > > 28 0742 1715 0656 1800 0602 1839 0506 1920 0430 1956 0427 2011
    > > 29 0741 1716 0600 1840 0505 1922 0429 1957 0427 2011
    > > 0454
    > > 30 0740 1718 0558 1842 0503 1923 0429 1958 0428 2011
    > > 0455
    > > 31 0739 1719 0556 1843 0428
    > > 1959 0457
    > > #######################

    >
    > > and I read it with this code:

    >
    > > ###########test.pl###########
    > > while(my $line = <INPUT>){
    > > push @AoA, [ split ];}

    >
    > > ###########################

    >
    > > I am trying to read it into a multi dimensional array, much like the
    > > perldoc perllol; however, I'm running into a couple of problems.

    >
    > > 1.) What does this mean in the documentation, i.e. I don't get what
    > > function the line should be calling , and it is not explained? I see
    > > that it is trying to index $AoA with row $x and column $y, but the
    > > function is throwing me off.

    >
    > > for $x (1 .. 10) {
    > > for $y (1 .. 10) {
    > > $AoA[$x][$y] = func($x, $y);
    > > }
    > > }

    >
    > Don't worry about the function call. That's just an example to show
    > what's possible.
    >
    > > 2) the push@AoA, [ split ]; thinks the 3rd column is blank space and
    > > combines the array into 8 elements (for the last line ) as opposed to
    > > 13 for the first).

    >
    > right, you're creating jagged arrays because the split function
    > doesn't care what whitespace character to split on or how many if
    > called with no explicit delimiter. there's no way for perl to know how
    > many columns there SHOULD be. you need to be more precise in your
    > description of your data. is it only one space character between each
    > value? is it a tab?
    >
    > > 3.) If I print @AoA; I get the: ARRAY(BLAH BLAH) ARRAY(BLAH BLAH). I
    > > know this is because I am not printing a scalar. But the question here
    > > is: Is there a way to print an array without for looping through each
    > > element?

    >
    > you can always use Data::Dumper.
    > e.g.
    > print Dumper( \@AoA ), "\n";
    >
    > > Thanks for all your help in advance!

    >
    > > Jonathan


    Thanks for your help, I'm kind of stuck with the jagged arrays,
    because the files I'm reading are created elsewhere, and I just want
    to chunk through them getting each value. I was trying to parse it
    with split since the format is XX--XXXX-XXXX--XXXX-XXXX--
    where the X are numbers and the - are whitespace. So I used a split(/
    \s?\s?/) command, but even then I can't get around the single then
    double space syntax. what a mess.

    The function thing makes sense now, I thought it may have been some
    other syntax that I was unaware of. So thanks! I wish it were a csv
    file :) I have much more experience with those, though in IDL... oh
    well.
    , Jun 27, 2007
    #3
  4. <> wrote:
    > Hello, a newbie question...
    >
    > I have a file that looks like this:
    > ########test.txt#########
    > 28 0742 1715 0656 1800 0602 1839 0506 1920 0430 1956 0427 2011
    > 29 0741 1716 0600 1840 0505 1922 0429 1957 0427 2011
    > 0454
    > 30 0740 1718 0558 1842 0503 1923 0429 1958 0428 2011
    > 0455
    > 31 0739 1719 0556 1843 0428
    > 1959 0457
    > #######################
    >
    > and I read it with this code:
    >
    > ###########test.pl###########
    > while(my $line = <INPUT>){
    > push @AoA, [ split ];



    Errr, you are splitting the string that is in $_ .

    Where is it that you put a string into $_ ?


    > 3.) If I print @AoA; I get the: ARRAY(BLAH BLAH) ARRAY(BLAH BLAH). I
    > know this is because I am not printing a scalar.



    No, it is because you _are_ printing a scalar (a reference).


    > But the question here
    > is: Is there a way to print an array without for looping through each
    > element?



    No.

    local $" = ',';
    print map "@$_\n", @AoA;

    but map() is just a foreach() in disguise, so you are still looping.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad McClellan, Jun 27, 2007
    #4
  5. On Wed, 27 Jun 2007 11:26:19 -0700, wrote:

    >1.) What does this mean in the documentation, i.e. I don't get what
    >function the line should be calling , and it is not explained? I see
    >that it is trying to index $AoA with row $x and column $y, but the
    >function is throwing me off.
    >
    > for $x (1 .. 10) {
    > for $y (1 .. 10) {
    > $AoA[$x][$y] = func($x, $y);
    > }
    > }


    It is a *generic* subroutine or builtin function. It's an example
    after all. Try any expression involving $x and $y e.g. $x*($y-$x).

    >2) the push@AoA, [ split ]; thinks the 3rd column is blank space and
    >combines the array into 8 elements (for the last line ) as opposed to
    >13 for the first).


    Well, that's because the first line contains 13 whitespace separated
    numbers and the last one 8. Did you expect something different? From
    your sample it is not entirely possible to understand whether your
    data is supposed to be whitespace-separated or has fixed width fields
    (with possibly blank ones). In the latter case, you have to follow a
    radically different approach.

    >3.) If I print @AoA; I get the: ARRAY(BLAH BLAH) ARRAY(BLAH BLAH). I
    >know this is because I am not printing a scalar. But the question here
    >is: Is there a way to print an array without for looping through each
    >element?


    perldoc Data::Dumper


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Jun 28, 2007
    #5
  6. Jan Pluntke Guest

    writes:

    > Thanks for your help, I'm kind of stuck with the jagged arrays,
    > because the files I'm reading are created elsewhere, and I just want
    > to chunk through them getting each value. I was trying to parse it
    > with split since the format is XX--XXXX-XXXX--XXXX-XXXX--
    > where the X are numbers and the - are whitespace. So I used a split(/
    > \s?\s?/) command,


    That will also split between digits (as the empty string also matches
    \s?\s?). If you want to split at (and forget about) any whitespace,
    use /\s+/.

    > but even then I can't get around the single then double space
    > syntax. what a mess.


    Best,
    Jan
    Jan Pluntke, Jul 6, 2007
    #6
  7. Dr.Ruud Guest

    Jan Pluntke schreef:

    > If you want to split at (and forget about) any whitespace,
    > use /\s+/.


    Even better: ' '. (See perldoc -f split.)

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Jul 6, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oli Filth
    Replies:
    9
    Views:
    3,312
    Uncle Pirate
    Jan 17, 2005
  2. Venkat
    Replies:
    4
    Views:
    959
    Venkat
    Dec 5, 2003
  3. Justme
    Replies:
    9
    Views:
    600
    clayne
    Oct 1, 2006
  4. Wirianto Djunaidi
    Replies:
    2
    Views:
    194
    Wirianto Djunaidi
    Apr 29, 2008
  5. PWGSC/TPSGC

    Splitting into a multi-dimensional array

    PWGSC/TPSGC, Oct 16, 2003, in forum: Javascript
    Replies:
    1
    Views:
    64
    Thomas 'PointedEars' Lahn
    Oct 16, 2003
Loading...

Share This Page