[Israel.pm] why does the array behave cyclic?

Shlomi Fish shlomif at iglu.org.il
Sat Jul 1 17:58:40 EEST 2006


Hi Sagiv!

Just a note - I find your code relatively hard to follow. You might consider 
improving your style by using "paragraphs" - short sections of code that are 
separated by newlines. Also you only have a one whitespace indentation. And 
you really should use Test::More or whatever instead of simple prints if you 
want to test your code. Now for some comments:

On Saturday 01 July 2006 17:02, Sagiv Barhoom wrote:
> hi all, I am playing with ref to subroutings and wrote the folowing code:
>
> sub get_list_API_of {
>  my @al= @_;

It is a better idea to pass the list as a reference to an array rather than 
flattened as @_. This is somewhat faster (but probably not very much), but 
also more clean and allows you to add more arguments later on. Alternatively 
you can also try using:

sub get_list_api_of
{
   my $args = shift;
   my $list = $args->{list};
}

Where $args is a hash ref that contains named parameters. This is something 
that is more awkward to do in C.

>  my $index=0;
>  my %LIST;
>  $LIST{prev}= sub{(defined $al[--$index])?$al[$index]:undef};
>  $LIST{head}= sub{$al[0]};
>  $LIST{tail}= sub{$al[$#al]};
>  $LIST{curr}= sub{$al[$index]};
>  $LIST{get_index}= sub {$index};
>
>  return \%LIST;
> } # ---------- end of subroutine get_list_API_of ----------
> #######################################
> # TEST
> #######################################
> my @array=qw{a b c d e f g};
> my $L=get_list_API_of @array;
> $\="\n";
> print "curr: ".$L->{curr}->()." index: ".$L->{get_index}->();
> print "prev: ".$L->{prev}->()." index: ".$L->{get_index}->();
> print "prev: ".$L->{prev}->()." index: ".$L->{get_index}->();
> print "prev: ".$L->{prev}->()." index: ".$L->{get_index}->();
> print "tail: ".$L->{tail}->()." index: ".$L->{get_index}->();
> print "head: ".$L->{head}->()." index: ".$L->{get_index}->();
>
>
> this is the output:
>
>
>
> [sagivba at black ~]$ perl itr1.pl
> curr: a index: 0
> prev: g index: -1
> prev: f index: -2
> prev: e index: -3
> tail: g index: -3
> head: a index: -3
>
>
> can anyone explain why it retuen g at the second line
> (the index is -1 shouldn't the sub return undef )?
>

If you have an array called @myarray, then $myarray[-1] will return the last 
element, and -2 will return the second to last element, etc.:

http://www.shlomifish.org/Vipe/lecture/Perl/Newbies/lecture1/arrays/negative_indexes.html

http://www.perlmeme.org/tutorials/arrays.html

If you don't want it to happen, you need to check for this condition before 
you reference it into the array. This is also the case in other dynamic 
languages:

<<<<<<
shlomi:~$ python
Python 2.4.3 (#2, Apr  7 2006, 21:14:50)
[GCC 4.0.3 (4.0.3-1mdk for Mandriva Linux release 2006.1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [0, 1, 2, 3, 4]
>>> a[-1]
4
>>>
>>>>>>

Regards,

	Shlomi Fish

---------------------------------------------------------------------
Shlomi Fish      shlomif at iglu.org.il
Homepage:        http://www.shlomifish.org/

95% of the programmers consider 95% of the code they did not write, in the
bottom 5%.



More information about the Perl mailing list