Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to declare/assign a array ref on fly while "use strict" 3

Status
Not open for further replies.

cyan01

Programmer
Mar 13, 2002
143
US
Hi, experts,

I have a sample code listed below:

Code:
#! /usr/bin/perl
use strict;

print "$0..testing...\n";
my @list = ("a", "b", "c");
print "@list\n";
for(my $i = 0; $i <= $#list ; $i++)
{
  for(my $j = 0; $j < 10; $j++)
  {
    if($i == 0)
    {
      push @{$list[$i]}, $j;
    }
    elsif($i == 1)
    {
      push @{$list[$i]}, $j*2;
    }
    else
    {
      push @{$list[$i]}, $j*$j;
    }
  }
}

foreach my $x (@list)
{
  print "\$x = $x, \@{$x} = @{$x}\n";
}

The output is just as expected:

Code:
% ./buildArrayRef.pl
./buildArrayRef.pl..testing...
a b c
$x = a, @{a} = 0 1 2 3 4 5 6 7 8 9
$x = b, @{b} = 0 2 4 6 8 10 12 14 16 18
$x = c, @{c} = 0 1 4 9 16 25 36 49 64 81

However, if I uncomment the line "use strict", it would not run:

Code:
% ./buildArrayRef.pl
./buildArrayRef.pl..testing...
a b c
Can't use string ("a") as an ARRAY ref while "strict refs" in use at ./buildArrayRef.pl line 13.

Becuase I have to use "use strict", how can I modify the above code to make it work?

Many thanks!

p.s. I am using linux (ubuntu).
 
I think the only way to avoid the error is to use "no strict 'refs' ;" after use strict
Code:
#! /usr/bin/perl
use strict;
[blue]no strict 'refs';[/blue]

print "$0..testing...\n";
my @list = ("a", "b", "c");
print "@list\n";
for(my $i = 0; $i <= $#list ; $i++)
{
  for(my $j = 0; $j < 10; $j++)
  {
    if($i == 0)
    {
      push @{$list[$i]}, $j;
    }
    elsif($i == 1)
    {
.......
....


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
You start off by setting the values in the list to 'a', 'b', and 'c'. They print out OK.

Then you want to convert the strings in the array into array references, which strict won't allow. You'll notice that in your second print, the values 'a', 'b', 'c' have been destroyed and don't appear.

Start off with the @list array holding array references, then you won't offend strict's sensibilities, or clobber the values.
Code:
use strict;
use warnings;

my @list = (["a"], ["b"], ["c"]);
print "@list\n";
for(my $i = 0; $i < @list ; $i++)
{
  for(my $j = 0; $j < 10; $j++)
  {
    if($i == 0)
    {
      push @{$list[$i]}, $j;
    }
    elsif($i == 1)
    {
      push @{$list[$i]}, $j*2;
    }
    else
    {
      push @{$list[$i]}, $j*$j;
    }
  }
}

foreach my $x (@list)
{
  print '$x = ' ,$x, ', @{$x} = ', "@{$x}\n";
}

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
there is actually no need for the "a b c" at all. There is also no need to have @list defined as anything to begin with but maybe that's how the script gets the data. To me the whole thing would be better written as a hash of arrays because you can then use the elements of @list as the hash keys and the values generated as anonymous arrays and not offend "strict" in anyway.

Code:
my @list = qw(a b c);
print "@list\n";
my %HoA = ();
for my $i (0 .. $#list) {
  for my $j (0 .. 10) {
    if($i == 0){
      push @{$HoA{$list[$i]}}, $j;
    }
    elsif($i == 1) {
      push @{$HoA{$list[$i]}}, $j*2;
    }
    else {
      push @{$HoA{$list[$i]}}, $j*$j;
    }
  }
}

foreach my $keys (keys %HoA)
{
  print "$keys = @{$HoA{$keys}}$/";
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top