hellworld has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks, I'm having a bit of a hard time with the Arrays. I'm trying to group certain strings -filtered by regex- in an array but it doesn't work. An example of what I'm doing is like the following:
BLABLABLA 2 BLABLA BLABLABLA 2 BLABLA BLABLABLA 2 BLABLA BLABLALBA 3 BLABLA BLABLABLA 3 BLABLA
I want the BLABLA's with the integer '2' assigned to the @array[1] so when I call @array[1] all BLABLA's that have 2 should be printed and BLABLAS with '3' should be printed when @array[2] is called and so on... Any ideas ? Thanks.

Replies are listed 'Best First'.
Re: to group array items or not to group array items
by ssandv (Hermit) on Aug 09, 2009 at 23:47 UTC
    First idea: put <code></code> tags around your code so the brackets and formatting don't get eaten.

    Second idea: state your problem more clearly, and don't use unclear fake examples. I don't understand the purpose of the first "BLABLABLA" on each line--does it need to get assigned based on the number, or not? A better fake example would have clearly different elements, so that you could explain which one was supposed to end up where. You should provide the desired output from your example, so we don't have to read your mind.

    Third: Tell us what you tried. Better yet, *show* us what you've tried.

    If you think you want an array of arrays, check out perllol. It's a good introduction to complex data structures. You say "it doesn't work", which doesn't give us much to go on in figuring out where you're getting stuck.
Re: to group array items or not to group array items
by ig (Vicar) on Aug 10, 2009 at 02:45 UTC

    Arrays are easy enough, once you get to know them.

    Managing sets of arrays can be more challenging and quite difficult if you try to tackle them before you clearly understand the basic data structures.

    Therefore, I recommend that you review perldata to make sure you have a good understanding of the basic data types: scalars, arrays and hashes. Next you should understand perlref to gain an understanding of references. Finally, you can tackle perllol and perldsc to learn how to put these basic elements together to create and use more complex data structures.

    If you find these a bit difficult to follow, check out the tutorials in Getting Started with Perl. There are lots of examples and different ways of expressing and looking at things - you should be able to find something that suits your style and level of understanding.

    In the mean time, I will make some assumptions about your situation and provide a recommendation based on those assumptions. Feel free to correct me if my assumptions are incorrect.

    I take it you are successfully extracting and classifying your strings. I don't know how you are doing this, so I will assume you are taking each line of a file as a string then using a regular expression to classify it, as follows:

    use strict; use warnings; my %strings; foreach my $line (<DATA>) { $line =~ m/(\d+)/; my $class = $1; # Save the line in an array for this class of line # Keep references to the arrays in a hash keyed by # the class. Use push() to add each line to the array. push(@{$strings{$class}}, $line); } # Now iterate over the classes and print all the lines # for each class. foreach my $class (sort { $a <=> $b } keys %strings) { print "strings for class $class:\n"; foreach my $string (@{$strings{$class}}) { print "\t$string"; } } __DATA__ BLABLABLA 2 BLABLA BLABLABLA 2 BLABLA BLABLABLA 2 BLABLA BLABLALBA 3 BLABLA BLABLABLA 3 BLABLA

    This produces the following output:

    strings for class 2: BLABLABLA 2 BLABLA BLABLABLA 2 BLABLA BLABLABLA 2 BLABLA strings for class 3: BLABLALBA 3 BLABLA BLABLABLA 3 BLABLA

    I have not used arrays with names like @array1, @array2, etc. Instead I have used a "hash of arrays" where the keys to the hash are the numbers by which the strings are classified and the values are references to the arrays of strings of each class.

    If this isn't relevant to your problem or does not solve it, perhaps you can explain why.

Re: to group array items or not to group array items
by ww (Archbishop) on Aug 10, 2009 at 00:26 UTC
    Following up on ssandv's "state your problem more clearly" -- more "precisely" would also be a "good thing"Reg SM. Re code tags, see Markup in the Monastery. And does "it doesn't work" mean you're having trouble writing a regex that matches what you want or something else? (If so, what?)

    Then you may want to explain whether "I want the BLABLA's with the integer '2'" means

    • those single instances preceded by a 2
      or
    • those single instances followed by a 2
      or
    • those multiple instances after a 2
      or
    • etc...?
Re: to group array items or not to group array items
by GrandFather (Saint) on Aug 10, 2009 at 00:28 UTC

    As ssandv said, show us your code (and you data)!

    That aside, possibly you ought be using a hash of arrays (HOA) keyed by the prefix string rather than what sounds like an array of arrays (AOA, or more often: LOL (list of list)).


    True laziness is hard work