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.


In reply to Re: to group array items or not to group array items by ig
in thread to group array items or not to group array items by hellworld

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.