single text file into multiple arrays

Discussion in 'Perl Misc' started by Faz, Sep 8, 2003.

  1. Faz

    Faz Guest

    hello all

    I have a text file as below

    a
    b
    c
    d
    e
    a1
    b1
    c1
    d1
    e1
    a2
    b2
    c2
    d2
    e2

    I want to read the text file and put them into multiple arrays
    grouping them. at the end I want to have

    arrayA should contain a a1 a2
    arrayB should contain b b1 b2
    arrayC should contain c c1 c2

    Thanks
    Faz
    Faz, Sep 8, 2003
    #1
    1. Advertising

  2. Faz

    Uri Guttman Guest

    >>>>> "F" == Faz <> writes:

    F> a
    F> b
    F> c
    F> d
    F> e
    F> a1
    F> b1
    F> c1
    F> d1
    F> e1
    F> a2
    F> b2
    F> c2
    F> d2
    F> e2

    untested

    local $/ ;

    push @{$buckets{uc $2}, $1 while <> =~ /(([a-z]+)\d*)/g ;

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
    Uri Guttman, Sep 8, 2003
    #2
    1. Advertising

  3. Uri Guttman wrote:
    >>>>>>"F" == Faz <> writes:

    >
    >
    > F> a
    > F> b
    > F> c
    > F> d
    > F> e
    > F> a1
    > F> b1
    > F> c1
    > F> d1
    > F> e1
    > F> a2
    > F> b2
    > F> c2
    > F> d2
    > F> e2
    >
    > untested
    >
    > local $/ ;

    --^^^^^^^^^^
    Shouldn't be there.

    > push @{$buckets{uc $2}, $1 while <> =~ /(([a-z]+)\d*)/g ;

    ------------------------^
    Missing right curly bracket.

    After those corrections, the above line creates the hash %buckets
    containing anonymous arrays. It can be printed like this:

    for (sort keys %buckets) {
    print "\@{\$buckets{$_}}: @{$buckets{$_}}\n";
    }

    which results in the following output:

    @{$buckets{A}}: a a1 a2
    @{$buckets{B}}: b b1 b2
    @{$buckets{C}}: c c1 c2
    @{$buckets{D}}: d d1 d2
    @{$buckets{E}}: e e1 e2

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 9, 2003
    #3
  4. "Uri Guttman" <> wrote in message
    news:...
    > >>>>> "F" == Faz <> writes:

    >
    >
    > untested
    >
    > local $/ ;
    >
    > push @{$buckets{uc $2}, $1 while <> =~ /(([a-z]+)\d*)/g ;
    >

    Uri:

    I only get the OP's intended result if I comment out 'local $/;' That
    slurps everything in and the while loop shuts off after the first match.

    jimk
    James E Keenan, Sep 9, 2003
    #4
  5. Faz

    Uri Guttman Guest

    >>>>> "GH" == Gunnar Hjalmarsson <> writes:

    >> untested
    >> local $/ ;

    GH> --^^^^^^^^^^
    GH> Shouldn't be there.

    and why not? i wanted a full file slurp, not a line by line one. it
    would be faster this way than yours.

    >> push @{$buckets{uc $2}, $1 while <> =~ /(([a-z]+)\d*)/g ;

    GH> ------------------------^
    GH> Missing right curly bracket.

    untested as promised!

    GH> After those corrections, the above line creates the hash %buckets
    GH> containing anonymous arrays. It can be printed like this:

    GH> for (sort keys %buckets) {
    GH> print "\@{\$buckets{$_}}: @{$buckets{$_}}\n";
    GH> }

    use Data::Dumper ;
    print Dumper \%buckets ;

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
    Uri Guttman, Sep 9, 2003
    #5
  6. Uri Guttman wrote:
    >>>>>> "GH" == Gunnar Hjalmarsson <> writes:

    >
    > >> local $/ ;

    > GH> --^^^^^^^^^^
    > GH> Shouldn't be there.
    >
    > and why not? i wanted a full file slurp, not a line by line one. it
    > would be faster this way than yours.


    Well, since all it results in is:

    %buckets = ( A => ['a'] );

    it's probably faster. ;-) Maybe it's possible to modify it in some
    other way to fulfil your intention, but in that case I think you'd
    better show us.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 9, 2003
    #6
  7. Faz

    Uri Guttman Guest

    >>>>> "GH" == Gunnar Hjalmarsson <> writes:

    >> >> local $/ ;

    GH> --^^^^^^^^^^
    GH> Shouldn't be there.
    >> and why not? i wanted a full file slurp, not a line by line one. it
    >> would be faster this way than yours.


    GH> Well, since all it results in is:

    GH> %buckets = ( A => ['a'] );

    GH> it's probably faster. ;-) Maybe it's possible to modify it in some
    GH> other way to fulfil your intention, but in that case I think you'd
    GH> better show us.

    this works with or without the scalar:
    local $/ ;
    map { push @{$buckets{uc $2}}, $1 while /(([a-z]+)\d*)/g } scalar <> ;

    note the horrid use of map in a oid context. i needed something to set
    $_ without a full foreach block or whatever. this is an odd case for
    wanting multiple statement modifiers but i know p6 won't get them. it
    would look too contrived to write that like:
    push @{$buckets{uc $2}}, $1 while /(([a-z]+)\d*)/g } for scalar <> ;

    here is a shorter one. golf anyone> the names could be shortedn and the
    regex is not well defined by the OP (i wrote a simple general alphas
    followed by optional digits).

    $_ = <> ; push @{$buckets{uc $2}}, $1 while /(([a-z]+)\d*)/g ;

    the problem with my untested code was that it was executing <> each time
    through the while and i just wanted the regex to execute.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
    Uri Guttman, Sep 9, 2003
    #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. Replies:
    4
    Views:
    950
    M.E.Farmer
    Feb 13, 2005
  2. Kevin
    Replies:
    1
    Views:
    542
    dorayme
    Apr 15, 2007
  3. Adam Plocher
    Replies:
    1
    Views:
    836
    Jonathan N. Little
    Jun 13, 2007
  4. Philipp
    Replies:
    21
    Views:
    1,123
    Philipp
    Jan 20, 2009
  5. ela
    Replies:
    12
    Views:
    345
    Uri Guttman
    Apr 6, 2009
Loading...

Share This Page