Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^5: Can call array but not index of an array in subroutine?!

by fraizerangus (Sexton)
on Apr 03, 2009 at 13:38 UTC ( [id://755267]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Can call array but not index of an array in subroutine?!
in thread Can call array but not index of an array in subroutine?!

Hi everyone! I've just found out the problem seems to be that some of the arrays I am creating are infact negative arrays?! does anyone know why this might be happening or what I can do to reverse it?! best wishes Dan
  • Comment on Re^5: Can call array but not index of an array in subroutine?!

Replies are listed 'Best First'.
Re^6: Can call array but not index of an array in subroutine?!
by Corion (Patriarch) on Apr 03, 2009 at 13:42 UTC

    What do you consider a "negative array"? Is it:

    1. A data structure where the index is arbitrary but the values are always the range 0 to (number of items-1)?
    2. A data structure where the values are arbitrary but the index is always in the range 0 down to -(number of items-1)?
    3. A normal array?

    If you're wondering, you can access Perl arrays with positive and negative values for the index, where positive values count from the front and negative values count from the end of the array.

      Sorry I did'nt really explain what I meant. The values are only acessible by using a negative index system, if I use the following loop, the array appears empty
      for ($i=0; $i >= 1000; $i++) { print STDOUT ("$Bxcoord[$i]"); }
      But if I use this loop I get the required values from the array.
      for ($i=0; $i >= -1000; $i--) { print STDOUT ("$Bxcoord[$i]"); }
      many thanks in advance! regards Dan

        Why are you sure that your loop is ever even entered? You got a condition that is wrong at the start of the loop:

        for ($i=0; $i >= 1000; $i++) {

        This is why it is much much better to use foreach to loop over arrays instead of using the C-style loop. Or, if you want to just look at an array, use Data::Dumper or a simple print statement:

        print "@Bxcoord";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://755267]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-19 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found