traversing array slices

T

TonyShirt

#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?
 
G

gnari

TonyShirt said:
#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
 
T

Tad McClellan

TonyShirt said:
#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
}
}
 
K

Keith Keller

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

#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

- --
(e-mail address 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+QGhVcNCxZ5ID8RArVoAJ9u6SzvstxCLb0LH9rzkcVtMksD2QCfeci1
uqXXDXatZSfYdraLbNaQdJ0=
=8FyU
-----END PGP SIGNATURE-----
 
V

valued customer

#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";
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top