develooper Front page | perl.beginners | Postings from January 2002

RE: Reference problem

Thread Previous
From:
ss004b3324
Date:
January 6, 2002 12:17
Subject:
RE: Reference problem
Message ID:
CIEFLCAPAEKPDDFKAOGMKECODAAA.meecho@blueyonder.co.uk
> Robert Thompson wrote:
> #!/usr/bin/perl
>
> @one_list = qw(1 2 3 4);
> @two_list = qw(5 6 7 8);
> @three_list = qw(9 10 11 12);
> @four_list = qw(13 14 15 16);
>
> foreach my $array (qw(one two three four)) {
>   foreach my $num ( @{$array}_list ) {   <== Break here
>     print $array . "_" . $num . "\n";
>   }
> }
>
>
> 	The error I am getting is that the "_list" is not seen as
> part of the variable name in the foreach loop.

Here's one solution, though I am sure there will be better solutions
offered.

#!/usr/bin/perl

@one = qw(1 2 3 4);
@two = qw(5 6 7 8);
@three = qw(9 10 11 12);
@four = qw(13 14 15 16);

@names = ( *one, *two, *three, *four );

foreach my $num(@names){
  foreach(@$num){
    $ref =~ s/(^\*[a-z]+\:+)//g;
    print "$num" . "_" . $_ . "\n";
  }
}

If you wish to keep the original array names one_list etc., then:
#!/usr/bin/perl

@one_list = qw(1 2 3 4);
@two_list = qw(5 6 7 8);
@three_list = qw(9 10 11 12);
@four_list = qw(13 14 15 16);

@names = ( *one_list, *two_list, *three_list, *four_list );

foreach my $num(@names){
  foreach(@$num){
    $num =~ s/(^\*[a-z]+\:+)//g;
    $num =~ s/\_list//g;
    print "$num" . "_" . $_ . "\n";
  }
}

Shaun

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.313 / Virus Database: 174 - Release Date: 02/01/2002


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About