in reply to Soft Array Refs

Hello smknjoe,

As stevieb has shown, a hash is a much better option here. But, for the record, you need no strict 'refs' only if you’re not using eval — in which case, you also have to use package global variables (see perlref#Symbolic-references):

use strict; use warnings; our (@table_min_1, @table_min_2, @table_max_1, @table_max_2, $array_na +me); while (<DATA>) { chomp; if (/Array\s+(\w+)\s+(\d+)/) { $array_name = 'table_' . $1 . '_' . $2; } else { no strict 'refs'; my @fields = split /\s+/; push @$array_name, $fields[1]; } } print "\n"; print "$_ " for @table_max_1, @table_max_2, @table_min_1, @table_min_2 +; print "\n"; __DATA__ ...

On the other hand, if you are using eval, you can use strict without qualification, along with my lexicals:

use strict; use warnings; my (@table_min_1, @table_min_2, @table_max_1, @table_max_2, $array_nam +e); while (<DATA>) { chomp; if (/Array\s+(\w+)\s+(\d+)/) { $array_name = '@table_' . $1 . '_' . $2; # ^ } else { my @fields = split /\s+/; eval qq(push $array_name, "$fields[1]"); } } print "\n"; print "$_ " for @table_max_1, @table_max_2, @table_min_1, @table_min_2 +; print "\n"; __DATA__ ...

Note the additional @ in the second example. In both cases, the output is as desired:

15:43 >perl 1283_SoPW.pl a b c 1 2 3 d e f 4 5 6 15:43 >

But, again, don’t do that! :-)

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Soft Array Refs
by smknjoe (Beadle) on Jun 24, 2015 at 16:15 UTC

    Ah so,

    I was somewhat close. I'm still fairly clueless on some of the syntactical subtleties in Perl - single quotes around '@table' for example. I love your printing shortcut at the end - I learn something new everyday. I'm looking to write this code CORRECTLY using hashes instead.

    Thx, Joe
      ... single quotes around '@table' ...

      If double-quotes has been used, double-quotish interpolation of an array named  @table_ would have been attempted and, with strict fully enabled (as it is in Athanasius's code), would have crashed compilation. Single-quotes do not interpolate. (Update: The  @ sigil could also be escaped in double-quotes.)

      c:\@Work\Perl\monks>perl -le "use warnings; use strict; ;; print qq{@table}; " Possible unintended interpolation of @table in string at -e line 1. Global symbol "@table" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors. c:\@Work\Perl\monks>perl -le "use warnings; use strict; ;; my @table = qw(fee fie foe); ;; print qq{double-quote array interpolation: (@table)}; ;; print q{single-quote, no interpolation: (@table)}; ;; print qq{double-quote, sigil escaped: (\@table)}; " double-quote array interpolation: (fee fie foe) single-quote, no interpolation: (@table) double-quote, sigil escaped: (@table)

      Update: Changed code examples to consolidate compiling code examples into single group of statements.


      Give a man a fish:  <%-(-(-(-<

        Correct.

        I noticed this after it failed the first time and I looked closer at Athanasius's code. Gvim's context-sensitive color-coding also clued me in to what was going on.

        As a follow-up, I was very successful in coding this as a nested hash - it took very little time and I was quite pleased at how easy it was. It was far less code than my previous attempt using if-statements and arrays AND no soft references!