Hello all,

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

#!/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
The code should simply dump out:
a b c 1 2 3 d e f 4 5 6

In reply to Soft Array Refs by smknjoe

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.