Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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"; }

In reply to 2d arrays and 'Can't use string as an ARRAY ref' by ronlewis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-25 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found