single text file into multiple arrays

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default single text file into multiple arrays

    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 Guest

  2. Similar Questions and Discussions

    1. Scrambled text after converting multiple pages into a single pdf
      Hi After combining 30 single page pdfs into a single document using "Create PDF from multiple pages", the text in the combined pdf is all scrambled....
    2. Creating multiple files into a single file
      I need to know how to create a multiple file from several single files using Acrobat 7.0. In other words, I have three A4 pdf pages (all single),...
    3. Saving multiple pictures in a single file
      When saving three or four Illustrator files as PDF is there a way to save them as separate pages in a single file instead of separate files. Could...
    4. form: single line -- multiple text boxes -- one typing field - how?
      please excuse my ignorance. am a newbie. I saw a fillable form, that shows:- a single line with multiple text boxes for entering each letter of a...
    5. multiple arrays from one text file with one column
      Hello I have a text file in a single column which has name address, phone age of each people as below Name Address phone age Name
  3. #2

    Default Re: single text file into multiple arrays

    >>>>> "F" == Faz <lfazal@hotmail.com> 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 ------ [email]uri@stemsystems.com[/email] -------- [url]http://www.stemsystems.com[/url]
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url]
    Damian Conway Class in Boston - Sept 2003 -- [url]http://www.stemsystems.com/class[/url]
    Uri Guttman Guest

  4. #3

    Default Re: single text file into multiple arrays

    Uri Guttman wrote:
    >>>>>>"F" == Faz <lfazal@hotmail.com> 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: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]

    Gunnar Hjalmarsson Guest

  5. #4

    Default Re: single text file into multiple arrays


    "Uri Guttman" <uri@stemsystems.com> wrote in message
    news:x78yoyj3yy.fsf@mail.sysarch.com...
    > >>>>> "F" == Faz <lfazal@hotmail.com> 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 Guest

  6. #5

    Default Re: single text file into multiple arrays

    >>>>> "GH" == Gunnar Hjalmarsson <noreply@gunnar.cc> 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 ------ [email]uri@stemsystems.com[/email] -------- [url]http://www.stemsystems.com[/url]
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url]
    Damian Conway Class in Boston - Sept 2003 -- [url]http://www.stemsystems.com/class[/url]
    Uri Guttman Guest

  7. #6

    Default Re: single text file into multiple arrays

    Uri Guttman wrote:
    >>>>>> "GH" == Gunnar Hjalmarsson <noreply@gunnar.cc> 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: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]

    Gunnar Hjalmarsson Guest

  8. #7

    Default Re: single text file into multiple arrays

    >>>>> "GH" == Gunnar Hjalmarsson <noreply@gunnar.cc> 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 ------ [email]uri@stemsystems.com[/email] -------- [url]http://www.stemsystems.com[/url]
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url]
    Damian Conway Class in Boston - Sept 2003 -- [url]http://www.stemsystems.com/class[/url]
    Uri Guttman Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139