Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

need help to create array dynamically

by vkp (Novice)
on Jan 23, 2013 at 18:35 UTC ( #1014980=perlquestion: print w/replies, xml ) Need Help??
vkp has asked for the wisdom of the Perl Monks concerning the following question:

Hi, i am doing some code. In this i have to break the lines in a file in chunks and push them in arrays. For example chink start form [ abababa bcbcbcb ] [ dedede gfgfgf ] like this the many chunks are predefined in file. I have to put each chunk in different arrays. Please suggest some path to do so. A thing i was thinking to count number of "[" from file and create that number of arrays. But i don't know create array using for loop with adding the loop variable number with their name.

Replies are listed 'Best First'.
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?

      sorry for confusion.

      [ 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.

        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?

Re: need help to create array dynamically
by blue_cowdawg (Monsignor) on Jan 23, 2013 at 18:42 UTC
        Hi, i am doing some code.

    Show us what you got...

    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

      my full work is to collect that chunks. Do some modification in them individually and then reprint them in a file. Thats it !!. I have just hoping some help to how to collect chunks

        You said you are doing some code. Someone followed up asking you to show what you've got. In other words, show the code you've written so far. You seem to have forgotten in your follow-up to do that. Show the code you've written so far so we know what to help you with.


Re: need help to create array dynamically
by dasgar (Curate) 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:

Re: need help to create array dynamically
by 2teez (Priest) 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

      sorry for so much confusion in my question

Re: need help to create array dynamically
by ww (Archbishop) on Jan 23, 2013 at 18:54 UTC
            ... or, if it's homework, label it thus!

    ... and in addition make your SOPW responsive to all the other replies.

      sorry what is SOPW

        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.

Re: need help to create array dynamically
by choroba (Chancellor) on Jan 24, 2013 at 09:42 UTC
    To dynamically create arrays, just create an array of arrays. See perldsc - Perl Data Structures Cookbook and perllol - Manipulating Arrays of Arrays in Perl for details.
    #!/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 ]
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: need help to create array dynamically
by aitap (Deacon) on Jan 23, 2013 at 19:03 UTC
    Start with split and read perlretut to know about pattern matching.
    Sorry if my advice was wrong.

      I have no problem with pattern matching. and can u please elaborate where to use split in this case

        Well, split your example by \n. You may also want to play with $/ to split a sequence of these examples from a file to smaller parts to be splitted by \n. Or you may read your file by for lines if you check the value of the $. variable.
        Sorry if my advice was wrong.
Re: need help to create array dynamically
by clueless newbie (Chaplain) on Jan 24, 2013 at 13:10 UTC

    Doesn't the data look close to a data structure already? A few regexes and an eval ...

    #!/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 line 13, <DATA> chunk 1.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1014980]
Approved by blue_cowdawg
[Lady_Aleena]: Corion, so do you think the gods would be open to taking over Cookies should I disappear for too long a time?
[Corion]: Lady_Aleena: Yes, I think so, but maybe someone else might also take care of the cookies ;)
[Lady_Aleena]: I'll put it out there.

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2017-04-26 08:38 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (471 votes). Check out past polls.