Help with multi-dimensional array 
Author Message
 Help with multi-dimensional array

I'm building up an array like this:

What I want to end up with is something like this:
((0,10,""),(11,5,""),(17,6,""))

and I want to access the elements thus:
    my $add = substr($_, $fields[$i][0], $fields[$i][1]);

Am I creating the list incorrectly, or accessing it incorrectly? I get this error:

Not an ARRAY reference at cfields.pl line 28.

Phil Hibbs.



Tue, 28 Sep 2004 12:21:51 GMT  
 Help with multi-dimensional array


--
  Simon Oliver



Tue, 28 Sep 2004 13:04:40 GMT  
 Help with multi-dimensional array

Quote:

> I'm building up an array like this:

> What I want to end up with is something like this:
> ((0,10,""),(11,5,""),(17,6,""))

> and I want to access the elements thus:
>     my $add = substr($_, $fields[$i][0], $fields[$i][1]);

> Am I creating the list incorrectly, or accessing it incorrectly? I get this error:

> Not an ARRAY reference at cfields.pl line 28.

To create the array you might consider using push, and using [] to create the
elements, e.g.


then you can access $fields[$i][0] etc.

Your initial attempt suggests that you aren't quite up to speed on perl's data
structures,

  perlreftut
  perldsc
  perllol

might be good things to check out, in particular

  perldoc perllol

will get you going.

Hope this helps,

Mike

--

http://www.stok.co.uk/~mike/       | GPG PGP Key      1024D/059913DA

http://www.exegenix.com/           |                  75D2 9EC4 C1C0 0599 13DA



Tue, 28 Sep 2004 13:09:28 GMT  
 Help with multi-dimensional array

Quote:
>I'm building up an array like this:


$foo = \($a,$b) is identical to $foo = (\$a,\$b) . $foo ends up holding \$b. use

$foo = [$a,$b] instead,

Quote:
>What I want to end up with is something like this:
>((0,10,""),(11,5,""),(17,6,""))

>and I want to access the elements thus:
>    my $add = substr($_, $fields[$i][0], $fields[$i][1]);

>Am I creating the list incorrectly, or accessing it incorrectly? I get this error:

>Not an ARRAY reference at cfields.pl line 28.

>Phil Hibbs.



Tue, 28 Sep 2004 13:13:18 GMT  
 Help with multi-dimensional array

Quote:

>I'm building up an array like this:


You should use push() to add elements to the end of an array:


Quote:
>What I want to end up with is something like this:
>((0,10,""),(11,5,""),(17,6,""))

In Perl, that is the same as this:

   (0,10,"",11,5,"",17,6,"")

because lists get "flattened".

So you do not have a multi-dimensional array. You (would) have a
one-dimensional array with 9 elements in it.

What you really want would be:

   ([0,10,""],[11,5,""],[17,6,""])

The top-level array has 3 references in it, and each 2nd-level
array has 3 scalars in it.

Quote:
>and I want to access the elements thus:
>    my $add = substr($_, $fields[$i][0], $fields[$i][1]);

>Am I creating the list incorrectly,

Yes.

You cannot take a reference to a list. If you try to, you
get a "list of references" instead of a "reference to a list".

Your code above makes a 1-dimensional array containing 9
references to scalars.

You want to take a reference to an _array_, not to a list.

   perldoc -q array

      "What is the difference between a list and an array?"

   perldoc perlreftut

Quote:
>or accessing it incorrectly?

No, you are accessing it correctly.

Quote:
>I get this error:

>Not an ARRAY reference at cfields.pl line 28.

"$fields[$i]" is a reference to a scalar but you are trying
to de-reference it as if it was a reference to an array.

You could have perhaps figured that out yourself if you had
read about your message in:

   perldoc perldiag

or had perl look it up for you by adding this near the top of your program:

   use diagnostics;

where it says:

----------------------------------------
=item Not an ARRAY reference

(F) Perl was trying to evaluate a reference to an array value, but found
a reference to something else instead.  You can use the ref() function
to find out what kind of ref it really was.  See L<perlref>.
----------------------------------------

Another way to see that you have not built what you thought you
have built, is to use the Data::Dumper module to see what you've got:

   use Data::Dumper;
   # load up the data structure

To build your data structure using references to arrays:


--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Tue, 28 Sep 2004 14:52:25 GMT  
 Help with multi-dimensional array
Thanks for all the replies. Here is my final code (it's another
clipboard manipulator):

use strict;
use warnings;
use Win32::Clipboard;

my $CLIP = Win32::Clipboard();
my $text = $CLIP->GetText;
$text =~ m|\n(.*)$|;       # match last line of $text
my $lastline = $+;         # results of last parenthesised match
chomp $lastline;
my $output = '';
my $offset = 0;
$lastline =~ s|  | |g;
foreach( split / /, $lastline ) {
  print "X".$_."X\n";

  $offset += length($_) + 2;

Quote:
}


  print $i." ".$fields[$i][0]." ".$fields[$i][1]."\n";
  foreach( split /\015\012?|\012\015?/, $text ) {
    my $add = substr($_,$fields[$i][0],$fields[$i][1]);
    print "add = X".$add."X\n";
    if (substr($add,0,1) ne '-') {
      $add =~ m|\s*([A-Z0-9]*)\s*|;
      $fields[$i][2] .= $+.'_' if (length($+) > 0);
      print "sum = X".$fields[$i][2]."X\n";
    }
  }
  chop $fields[$i][2];     # remove trailing _
  $output .= $fields[$i][2]."\n";
Quote:
}

$CLIP->Set($output);
__END__
It takes input like this:
                    PARAMETER   PARAMETER    UPDATED   UPDATED
LOCATION  OWNER       CODE        VALUE       DATE       BY
--------  --------  ----------  -----------  --------  --------
and produces output like this:
LOCATION
OWNER
PARAMETER_CODE
PARAMETER_VALUE
UPDATED_DATE
UPDATED_BY

Criticism is welcome, and this code is free for all to plunder.

Phil Hibbs.



Sat, 02 Oct 2004 14:45:42 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. newbie help: sorting multidimensional arrays

2. help with multidimensional array....

3. help: multi-dimensional hash from flat array

4. multi-dimensional arrays - more help please!

5. multi-dimensional arrays with hashes help please

6. Help understanding Lists of Lists or Multi-dimensional arrays

7. Help: Multi-dimensional arrays & such

8. Help with Multi-dimensional array

9. Need help with multi-dimensional arrays was:tie

10. arrays, multi-dimensional arrays, pointers and such

11. Associative arrays and multi-dimensional arrays.

12. Multi-Dimensional arrays, DBMs, and Assoc. Arrays

 

 
Powered by phpBB® Forum Software