Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > single text file into multiple arrays

Reply
Thread Tools

single text file into multiple arrays

 
 
Faz
Guest
Posts: n/a
 
      09-08-2003
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
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      09-08-2003
>>>>> "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
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      09-08-2003
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

 
Reply With Quote
 
James E Keenan
Guest
Posts: n/a
 
      09-08-2003

"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


 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      09-08-2003
>>>>> "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:umper ;
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
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      09-08-2003
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

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      09-09-2003
>>>>> "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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to copy multi object array contents into single object arrays? Tuxedo Javascript 65 01-19-2010 11:22 AM
resolve single line with multiple items into mutliple lines, single items ela Perl Misc 12 04-06-2009 06:47 PM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Pasting multiple lines into a single line text box Adam Plocher HTML 1 06-13-2007 05:40 PM
Text files read multiple files into single file, and then recreate the multiple files googlinggoogler@hotmail.com Python 4 02-13-2005 05:44 PM



Advertisments
 



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