single text file into multiple arrays

F

Faz

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
 
U

Uri Guttman

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
 
G

Gunnar Hjalmarsson

Uri said:
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
 
J

James E Keenan

Uri Guttman said:
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
 
U

Uri Guttman

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
 
G

Gunnar Hjalmarsson

Uri said:
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.
 
U

Uri Guttman

GH> --^^^^^^^^^^
GH> Shouldn't be there.
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top