> Hi,
> 
> I am trying to initialize a dynamically-named array, i.e. with the
> following test code (if I type "script.pl char" at the command prompt) I
> get the message "array char contains 1 2 3" but I get a warning if 'use
> strict' is on. Can anyone show me how it should be done (I would like to
> use 'use strict'!)?
> 
> #!/usr/bin/perl -w
> #use strict;
> my ($string);
> 
> while (<$ARGV[0]>) {
>   chomp($string=$_);
>   @$string = (1,2,3);  #Won't work if 'use strict' pragma is used
> }
> 
> print "array $string contains @$string\n";
> 
> 

Well that is kind of the point, you don't :-). That is specifically what
the 'refs' part of 'strict' prevents (symbolic references), for good reason.

perldoc strict

In newer Perls you can shut off that portion of the stricture lexically,
or a better idea is to store a reference to an anonymous array to a
hash, then use the hash key as its name.

my %hash;
$hash{'array_name'} = [ 'one','two','three' ];

print $hash{'array_name'}->[2];

perldoc perlreftut
perldoc perlref

http://danconia.org
 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to