[Phoenix-pm] 3D Datastructure

Scott Walters scott at illogics.org
Fri Sep 30 13:26:17 PDT 2005


Good. I still see you using global variables instead of passing references.

  sub something {
    my $arrref = shift;
    foreach my $element (@$arrref) { ... }
    print $arrref->[3];

  my @array = ( 1 .. 20 );
  foreach my $element (@array) { ... }
  print $array[3];

The \ creates a reference to an array, hash, etc. (People usually say "takes
a reference".) 

This little example shows how to work with a reference when you have one.
Rather than doing a foreach over @array, you're doing it over @$arrayref.
(Rather than pushing onto @array, you push onto @arrayref, etc.) Rather
than accessing individual elements with $array[n], they're accessed like
$arrref->[n]. Hashes are exactly the same except you subscript them
like $hashref->{key} and use them as a hash with hash built-ins like %$hashref.
For example, you can do keys %$hashref.

By the way, Perl6::Contexts was meant to get rid of the nastier parts of
syntax in these examples. I *hate* having to write %$foo, foo(\%one, \@two, 
\%three), etc. 

Oh -- Data::Alias. You can have the advantages of reference (data sharing between
the caller and callee, speed, passing multidim datastructures, etc) with the
syntax of plain old hashes and arrays that way too:

  use Data::Alias;

  sub something {
    alias my @arr = shift;
    foreach my $element (@arr) { ... }
    print $arr[3];

  my @array = ( 1 .. 20 );
  foreach my $element (@array) { ... }
  print $array[3];

This so rocks. 

Working with references as function arguments is a good next step after 
multidim data structures. But good work. I hope it helps to have someone pushing
you =P

Also by the way, the Data Structures in Perl 6 Now has a lot to say about all 
of this ;)



On  0, "Jonathan K. Smith" <jksmith at lexsolutio.com> wrote:
>    Hey made my first 3d data structure and it worked!! Woot Woot!!
>    Anyways thought I'd append it and see what you'll thought of my first
>    attempt into references.  Things I should change to improve these
>    types of scripts.
>    Jonathan Smith
>    Encore Lex Solutio
>    [1]www.lexsolutio.com
>    1-888-389-1658
> References
>    1. http://www.lexsolutio.com/

> _______________________________________________
> Phoenix-pm mailing list
> Phoenix-pm at pm.org
> http://mail.pm.org/mailman/listinfo/phoenix-pm

More information about the Phoenix-pm mailing list