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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |