smknjoe has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Soft Array Refs
by stevieb (Canon) on Jun 23, 2015 at 22:45 UTC | |
by smknjoe (Beadle) on Jun 24, 2015 at 02:50 UTC | |
by shawnhcorey (Friar) on Jun 24, 2015 at 12:25 UTC | |
|
Re: Soft Array Refs
by Athanasius (Cardinal) on Jun 24, 2015 at 05:38 UTC | |
by smknjoe (Beadle) on Jun 24, 2015 at 16:15 UTC | |
by AnomalousMonk (Archbishop) on Jun 24, 2015 at 18:33 UTC | |
by smknjoe (Beadle) on Jun 25, 2015 at 15:30 UTC |