I've looked all over SoPW for some information on how to implement array soft references, but no luck. I'm having trouble making my script more flexible with array naming.
As you can see, I'm defining the array names at the top (I know what they will be called when they are parsed in), but when it comes to populating them, I'd like the flexibility. I'm using "no strict 'refs';" and eval. I'd rather use "use strict" if possible and write clean code, but then it turns into a very rigid set of 'if' statements that requires much more editing.
Perhaps someone can point me to a better solution...
Thanks, smknjoe
The code should simply dump out:#!/usr/bin/perl #use strict; no strict 'refs'; use warnings; my @table_min_1; my @table_min_2; my @table_max_1; my @table_max_2; my @fields; my $array_name; my $item; while (my $lines = <DATA>) { chomp($lines); if ($lines =~ /Array\s+(\w+)\s+(\d+)/) { # array ref $array_name = "\$table_" . $1 . "_" . $2; } else { @fields = split(/\s+/,$lines); my $value = $fields[1]; #print "debug: $array_name \n"; eval "push @{$array_name}, $value"; } } foreach $item (@table_min_1) { print "$item\n"; } foreach $item (@table_min_2) { print "$item\n"; } foreach $item (@table_max_1) { print "$item\n"; } foreach $item (@table_max_2) { print "$item\n"; } __DATA__ Array max 1 useless_text_field a useless_text_field b useless_text_field c Array max 2 useless_text_field 1 useless_text_field 2 useless_text_field 3 Array min 1 useless_text_field d useless_text_field e useless_text_field f Array min 2 useless_text_field 4 useless_text_field 5 useless_text_field 6
a b c 1 2 3 d e f 4 5 6
In reply to Soft Array Refs by smknjoe
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |