Ask a Question related to PERL Miscellaneous, Design and Development.
-
Faz #1
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
-
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.... -
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),... -
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... -
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... -
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 -
Uri Guttman #2
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
-
Gunnar Hjalmarsson #3
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
-
James E Keenan #4
Re: single text file into multiple arrays
"Uri Guttman" <uri@stemsystems.com> wrote in message
news:x78yoyj3yy.fsf@mail.sysarch.com...Uri:>> >>>>> "F" == Faz <lfazal@hotmail.com> writes:
>
> untested
>
> local $/ ;
>
> push @{$buckets{uc $2}, $1 while <> =~ /(([a-z]+)\d*)/g ;
>
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
-
Uri Guttman #5
Re: single text file into multiple arrays
>>>>> "GH" == Gunnar Hjalmarsson <noreply@gunnar.cc> writes:
GH> --^^^^^^^^^^>> untested
>> local $/ ;
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> ------------------------^>> push @{$buckets{uc $2}, $1 while <> =~ /(([a-z]+)\d*)/g ;
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
-
Gunnar Hjalmarsson #6
Re: single text file into multiple arrays
Uri Guttman wrote:
Well, since all it results in is:>>>>>>> "GH" == Gunnar Hjalmarsson <noreply@gunnar.cc> writes:> GH> --^^^^^^^^^^> >> local $/ ;
> 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.
%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
-
Uri Guttman #7
Re: single text file into multiple arrays
>>>>> "GH" == Gunnar Hjalmarsson <noreply@gunnar.cc> writes:
GH> --^^^^^^^^^^>> >> local $/ ;
GH> Shouldn't be there.GH> Well, since all it results in is:>> and why not? i wanted a full file slurp, not a line by line one. it
>> would be faster this way than yours.
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



Reply With Quote

