traversing array slices

Discussion in 'Perl Misc' started by TonyShirt, Feb 27, 2004.

  1. TonyShirt

    TonyShirt Guest

    #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?
     
    TonyShirt, Feb 27, 2004
    #1
    1. Advertising

  2. TonyShirt

    gnari Guest

    "TonyShirt" <> wrote in message
    news:...
    > #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
     
    gnari, Feb 27, 2004
    #2
    1. Advertising

  3. TonyShirt <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Feb 27, 2004
    #3
  4. TonyShirt

    Keith Keller Guest

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

    On 2004-02-27, TonyShirt <> 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

    - --
    -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-----
     
    Keith Keller, Feb 28, 2004
    #4
  5. (TonyShirt) wrote in message news:<>...
    > #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";
    }
    }
     
    valued customer, Feb 28, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. proudfeet

    array slices

    proudfeet, Oct 1, 2003, in forum: VHDL
    Replies:
    1
    Views:
    637
    Egbert Molenkamp
    Oct 1, 2003
  2. Alex Polite

    No Array slices! What is this?

    Alex Polite, Jun 6, 2004, in forum: Java
    Replies:
    3
    Views:
    540
    John C. Bollinger
    Jun 7, 2004
  3. Replies:
    8
    Views:
    24,558
    Thomas G. Marshall
    May 30, 2005
  4. Patrick Tyler

    string/array slices

    Patrick Tyler, Mar 30, 2011, in forum: Ruby
    Replies:
    12
    Views:
    282
    Adam Prescott
    Mar 31, 2011
  5. drKreso
    Replies:
    5
    Views:
    621
    drKreso
    Jan 5, 2012
Loading...

Share This Page