Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

2d arrays and 'Can't use string as an ARRAY ref'

by ronlewis (Initiate)
on Apr 02, 2011 at 18:27 UTC ( #897112=perlquestion: print w/ replies, xml ) Need Help??
ronlewis has asked for the wisdom of the Perl Monks concerning the following question:

After tearing my hair out the whole day, it's time to seek some wisdom.

I'm trying to manipulate scalars in a 2-dimensional array.

For example, I might want do a substr() operation on each scalar. I can't do it without seeing strange error messages which I don't understand.

I understand the principle of Perl's 2-dimensional arrays as a list of references to lists. However, these error messages make no sense to me. Could someone nudge me in the right direction?

#! /usr/bin/perl -w use strict; use warnings; # Create a one-dimensional array my @row = ('a sheep', 'an elephant', 'the wolf'); # Use it as the first row in a 2-dimensional array my @twoDimArray = (\@row); # List of articles in the English language my @articleList = ('the', 'a', 'an'); # Remove articles from animals OUTER: foreach my $animal ($twoDimArray[0]) { INNER: foreach my $article (@articleList) { if (index ($animal, $article) == 0) { # Article found at beginning of $animal. Remove it. $animal = substr($animal, (length($article) + 1)); # Only need to remove one article from each animal last INNER; } } } # Print a list of three animals # I expect to see the output # Animal: sheep # Animal: elephant # Animal: wolf # Why do I get only get one animal, and an array reference? # Animal: ARRAY(0x8343218) foreach my $animal ($twoDimArray[0]) { print "Animal: $animal\n"; }

I tried another approach, and got a different strange error message.

#! /usr/bin/perl -w use strict; use warnings; # Create a one-dimensional array my @row = ('a sheep', 'an elephant', 'the wolf'); # Use it as the first row in a 2-dimensional array my @twoDimArray = (\@row); # List of articles in the English language my @articleList = ('the', 'a', 'an'); # This code block produces the error # "Can't use string ("RAY(0x8641210)") as an ARRAY ref while "stric +t refs" in use at ./arraytest.pl line 22." # # Remove articles from animals OUTER: for (my $count = 0; $count < 3;$count++) { INNER: foreach my $article (@articleList) { if (index ($twoDimArray[0][$count], $article) == 0) { # Article found at beginning of $animal. Remove it. $twoDimArray[0] = substr($twoDimArray[0], (length($article +) + 1)); # Only need to remove one article from each animal last INNER; } } } # Print a list of animals foreach my $animal ($twoDimArray[0]) { print "Animal: $animal\n"; }

Comment on 2d arrays and 'Can't use string as an ARRAY ref'
Select or Download Code
Re: 2d arrays and 'Can't use string as an ARRAY ref'
by jwkrahn (Monsignor) on Apr 02, 2011 at 18:49 UTC
    OUTER: foreach my $animal ($twoDimArray[0]) {

    You have to dereference the array in $twoDimArray[0]:

    OUTER: foreach my $animal (@{$twoDimArray[0]}) {
Re: 2d arrays and 'Can't use string as an ARRAY ref'
by Marshall (Prior) on Apr 02, 2011 at 22:51 UTC
    another approach, with a few printing examples, also realize that something like $critters[1][2] is valid also.
    #!/usr/bin/perl -w use strict; use Data::Dumper; #I made the 2D array bigger as you only had one row my @critters = ( ['a sheep', 'an elephant', 'the wolf'], ['a fish', 'an eel', 'the whale'] ); print "original array\n"; print Dumper \@critters; my @articleList = ('the', 'an', 'a'); my $articleregex = join("|", @articleList); print "one way to access the whale and a sheep\n"; print "$critters[1][2], $critters[0][0]\n"; #the whale a sheep foreach my $rowref (@critters) #each row { foreach my $name (@$rowref) #each "column" in that row { $name =~ s/^($articleregex) //; } } print "\nmodified array:\n"; print Dumper \@critters; print "\nPrint one row\n"; #when derefencing a thing with a subscript, you need to #use {} to make clear what is being dereferenced, i.e. the #entire row print "@{$critters[1]}\n"; my $ref2arow = $critters[1]; #using an reference to a row print "$ref2arow->[1]\n"; #print 2nd column (eel) __END__ original array $VAR1 = [ [ 'a sheep', 'an elephant', 'the wolf' ], [ 'a fish', 'an eel', 'the whale' ] ]; one way to access the whale and a sheep the whale, a sheep modified array: $VAR1 = [ [ 'sheep', 'elephant', 'wolf' ], [ 'fish', 'eel', 'whale' ] ]; Print one row fish eel whale eel
    Try adding prints in various places and play with this.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://897112]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-10-20 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (92 votes), past polls