Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > traversing array slices

Reply
Thread Tools

traversing array slices

 
 
TonyShirt
Guest
Posts: n/a
 
      02-27-2004
#I'm Trying to traverse an array slice that has been passed to a sub
#I'm not sure what perl is doing when I set the reference to @_


$Slice[1]=["a","b","c"];
$Slice[2]=["d","e","f"];

printslice(\@Slice);


sub printslice{
@tempslice = @_;

foreach my $x (@tempslice){
foreach ($w,$y,$z){
print $w."\n";
}
}
}

#I would like the output to be the first value in the slice i.e.:

#a
#d

#Could any one help?
 
Reply With Quote
 
 
 
 
gnari
Guest
Posts: n/a
 
      02-27-2004
"TonyShirt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> #I'm Trying to traverse an array slice that has been passed to a sub
> #I'm not sure what perl is doing when I set the reference to @_
>
>
> $Slice[1]=["a","b","c"];
> $Slice[2]=["d","e","f"];
>
> printslice(\@Slice);


this call your sub with one argument, an arrayref
judging from your following code, you probably meant to:
printslice(@Slice);

> sub printslice{
> @tempslice = @_;


my @tempslice = @_;

> foreach my $x (@tempslice){


here $x is an array ref

> foreach ($w,$y,$z){
> print $w."\n";
> }


amazing. begone!
you want to print the first element of the array $x refers to
print "$x->[0]\n";

> }
> }
>


gnari



 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      02-27-2004
TonyShirt <(E-Mail Removed)> wrote:

> #I'm Trying to traverse an array slice that has been passed to a sub



No you're not. You are trying to traverse an LoL that is passed
via a reference to an array.


> #I'm not sure what perl is doing when I set the reference to @_



You do not set any reference to @_ ...


> $Slice[1]=["a","b","c"];
> $Slice[2]=["d","e","f"];
>
> printslice(\@Slice);



The arguments are not a slice. The argument is an array reference.

printslice( @Slice[1,2] );

_That_ would be passing an array slice.


> sub printslice{
> @tempslice = @_;
>
> foreach my $x (@tempslice){
> foreach ($w,$y,$z){
> print $w."\n";



I am at a complete loss as to what you think that inner foreach
is supposed to be doing. You are printing a variable that has
never been given any value...


> }
> }
> }
>
> #I would like the output to be the first value in the slice i.e.:



There are NO "slices" anywhere in the code you posted!


> #a
> #d
>
> #Could any one help?



We could if we knew what you wanted.

Here is my guess:

---------------------
sub printslice{
my($array_ref) = @_;

foreach my $inner_array (@$array_ref){
next unless defined $inner_array; # $Slice[0] is undef
print "$inner_array->[0]\n"; # print the first element
}
}
---------------------


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Keith Keller
Guest
Posts: n/a
 
      02-28-2004
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 2004-02-27, TonyShirt <(E-Mail Removed)> wrote:
> #I'm Trying to traverse an array slice that has been passed to a sub
> #I'm not sure what perl is doing when I set the reference to @_


use strict;
use warnings;

(plus the comments you've already gotten)

- --keith

- --
http://www.velocityreviews.com/forums/(E-Mail Removed)-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQFAP+QGhVcNCxZ5ID8RArVoAJ9u6SzvstxCLb0LH9rzkc VtMksD2QCfeci1
uqXXDXatZSfYdraLbNaQdJ0=
=8FyU
-----END PGP SIGNATURE-----
 
Reply With Quote
 
valued customer
Guest
Posts: n/a
 
      02-28-2004
(E-Mail Removed) (TonyShirt) wrote in message news:<(E-Mail Removed). com>...
> #I'm Trying to traverse an array slice that has been passed to a sub
> #I'm not sure what perl is doing when I set the reference to @_
>
>
> $Slice[1]=["a","b","c"];
> $Slice[2]=["d","e","f"];

Assuming you *really* meant to start with zero-indexed
array element ... $Slice[0] ...
>
> printslice(\@Slice);
>
>
> sub printslice{
> @tempslice = @_;
>
> foreach my $x (@tempslice){
> foreach ($w,$y,$z){
> print $w."\n";
> }
> }
> }
>
> #I would like the output to be the first value in the slice i.e.:
>
> #a
> #d
>
> #Could any one help?


You might be mixing things up, what you are calling a 'slice'
is really a pair of anonymous array references inside an array
called 'Slice'. If you are used to programming Python, then that
is probably causing the mixup.

The following example might be more what you intended.

### INITIALIZE
my @Slice = ();
$Slice[0]=["a","b","c"];
$Slice[1]=["d","e","f"];

### call the subroutine passing a single argument (array ref)
&printslice(\@Slice);

sub printslice
{
### grab the single argument (array ref)
my $tempslice = shift;

### deref and loop through the array
### and assign the nested ref to $x
foreach my $x (@{$tempslice})
{
### deref $x and get the zero-th element
print $x->[0]."\n";
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Idiomatic way to get array of possible slices from string drKreso Ruby 5 01-05-2012 10:50 AM
string/array slices Patrick Tyler Ruby 12 03-31-2011 07:17 AM
Array Slices in Java kvnsmnsn@hotmail.com Java 8 05-30-2005 02:30 AM
No Array slices! What is this? Alex Polite Java 3 06-07-2004 01:52 PM
array slices proudfeet VHDL 1 10-01-2003 08:55 AM



Advertisments