Re: need help to create array dynamically
by Kenosis (Priest) on Jan 23, 2013 at 18:49 UTC
|
...and provide a good sample of your data (e.g., is [ abababa bcbcbcb ] on one line or are there multiple instances per line or can one be spread across two lines?). If it contains information that's not suitable for public consumption, redact that...
Also, help me understand the following:
I have to put each chunk in different arrays.
Do you really mean one array per chunk (i.e., creating one-element arrays) or did you mean that each chunk needs to be an array element?
| [reply] [d/l] |
|
|
[
abababa
bcbcbcb
]
Data is in this format. It is not in a line. But each start with "[" and end with "]". the array in which i shall put all these 4 lines would not be a one element array then. I shall put this 4 lines in one array , another 4 lines in another array... like this fashion, i need to reads whole file. | [reply] [d/l] |
|
|
Thank you for the clarification.
Why a separate array for each 'chunk?' How will you manage these? What's the end result? Are there blank lines between these chunks? Also, how large is this file?
| [reply] |
Re: need help to create array dynamically
by blue_cowdawg (Monsignor) on Jan 23, 2013 at 18:42 UTC
|
| [reply] |
|
|
| [reply] |
|
|
| [reply] |
Re: need help to create array dynamically
by dasgar (Priest) on Jan 23, 2013 at 20:04 UTC
|
Perhaps I'm misunderstanding your post, but it kind of sounds like you're trying to determine the size of the array for the purpose of creating an array with that number of elements. In Perl, you don't need to know the planned/needed size of the array to create the array. In your code, you just need something like my @list; to create an empty array named 'list'.
You might find the following perldocs useful:
| [reply] [d/l] |
Re: need help to create array dynamically
by 2teez (Vicar) on Jan 23, 2013 at 19:06 UTC
|
Hi vkp,
If I may add, To get good answers to your question, please check How do I post a question effectively?
..Please suggest some path to do so..
You can check this documentation perldsc
If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
| [reply] |
|
|
| [reply] |
Re: need help to create array dynamically
by choroba (Cardinal) on Jan 24, 2013 at 09:42 UTC
|
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
my @arrays;
my $count = -1;
while (<DATA>) {
chomp;
$count++ if $_ eq '[';
next if /[][]/;
push @{ $arrays[$count] }, $_;
}
print Dumper \@arrays;
__DATA__
[
aaaa
bbbbb
]
[
ccc
dddd
eeeee
]
[
f
]
| [reply] [d/l] |
Re: need help to create array dynamically
by ww (Archbishop) on Jan 23, 2013 at 18:54 UTC
|
| [reply] |
|
|
| [reply] |
|
|
Explained in a message, but FTR and for future readers:
SOPW is "Seekers of Perl Wisdom," the section where you made your "OP" or "original post" -- the first one in the thread. You can open your OP, scroll down, and edit it with the answers requested above. Make a note that that text is an UPDATE.
| [reply] |
Re: need help to create array dynamically
by aitap (Curate) on Jan 23, 2013 at 19:03 UTC
|
| [reply] |
|
|
| [reply] |
|
|
| [reply] [d/l] [select] |
Re: need help to create array dynamically
by clueless newbie (Curate) on Jan 24, 2013 at 13:10 UTC
|
#!/usr/bin/perl
use Data::Dumper;
use strict;
use warnings;
local $/;
my $data=<DATA>;
$data=~s{\[}{[qw(}sg;
$data=~s{\]}{)],}sg;
eval '$data=\['.$data.']';
warn Data::Dumper->Dump([\$data],[qw(*data)]).' ';
__DATA__
[
abc
def
]
[
ghi
jkl
mno
]
and we have ...
$data = \\[
[
'abc',
'def'
],
[
'ghi',
'jkl',
'mno'
]
];
at pmdata.pl line 13, <DATA> chunk 1.
| [reply] [d/l] [select] |
|
|
| [reply] |