delete from array by reference

Discussion in 'Perl Misc' started by Michael Goerz, Apr 23, 2006.

  1. Hi,

    how can I delete from an array by reference (i.e. I already have a
    pointer to the array element) instead of by index. Consider the
    following code:


    #!/usr/bin/perl -w
    use strict;
    use Data::Dumper;

    my $testarray = [
    ["bla", "bla"],
    ["xyz", "xyz"],
    "blabla",
    123 ];

    print("before:\n", Dumper($testarray), "\n");

    my $pointer_to_element = $testarray->[1];

    undef($pointer_to_element); # no effect ...
    undef(@{$pointer_to_element}); # has *some* effect,
    # but only works since I know
    # the element is an array
    print("after undef by reference:\n", Dumper($testarray), "\n");

    undef(@{$testarray}[1]); # ... but I want the same as this
    print("after undef by index:\n", Dumper($testarray), "\n");




    Even better would be to actually delete, not just to undef the element.

    Thanks,
    Michael Goerz
    Michael Goerz, Apr 23, 2006
    #1
    1. Advertising

  2. Michael Goerz <4ward.com> wrote in news:4avs8kFum7haU1@uni-
    berlin.de:

    > how can I delete from an array by reference (i.e. I already have a
    > pointer to the array element)


    There is your first mistake. There are no pointers in Perl. Attempting
    to draw analogies is futile. Search Google Groups for recent discussions
    on this topic in this newsgroup.

    > instead of by index. Consider the following code:
    >
    >
    > #!/usr/bin/perl -w
    > use strict;
    > use Data::Dumper;
    >
    > my $testarray = [
    > ["bla", "bla"],
    > ["xyz", "xyz"],
    > "blabla",
    > 123 ];
    >
    > print("before:\n", Dumper($testarray), "\n");
    >
    > my $pointer_to_element = $testarray->[1];


    $testarray->[1] is not a pointer to anything. It is a reference to the
    anonymous array containing "xyz", "xyz".

    > undef($pointer_to_element); # no effect ...
    > undef(@{$pointer_to_element}); # has *some* effect,
    > # but only works since I know
    > # the element is an array
    > print("after undef by reference:\n", Dumper($testarray), "\n");
    >
    > undef(@{$testarray}[1]); # ... but I want the same as this
    > print("after undef by index:\n", Dumper($testarray), "\n");


    Honestly, I think you have not chosen the right data structure for the
    real problem you are trying to solve.

    You can use grep to filter out the elements you don't want.

    #!/usr/bin/perl

    use strict;
    use warnings;

    my $x = [
    [ qw(abc abc) ],
    [ qw(xyz xyz) ],
    [ qw(edf edf) ],
    ];

    my $e = $x->[1];

    $x = [ grep { $_ != $e } @$x ];

    use Data::Dumper;
    print Dumper $x;

    __END__

    But repeatedly doing this is bound to be inefficient.

    Sinan
    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Apr 23, 2006
    #2
    1. Advertising

  3. Michael Goerz

    Dr.Ruud Guest

    Michael Goerz schreef:

    > how can I delete from an array by reference (i.e. I already have a
    > pointer to the array element) instead of by index.


    Maybe this helps:

    #!/usr/bin/perl
    use strict;
    use warnings;

    my @ary = ( [ 'a', 'a' ]
    , [ 'b', ' b' ]
    , 'c'
    , 123
    );

    my $eref = \$ary[1];

    for (@ary) {
    print $eref, "\t", \$_, "\n";
    }


    > Even better would be to actually delete, not just to undef the
    > element.


    perldoc perlop, look for slice.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Apr 23, 2006
    #3
  4. Michael Goerz

    robic0 Guest

    On Sun, 23 Apr 2006 01:49:50 +0200, "Dr.Ruud" <> wrote:

    >Michael Goerz schreef:
    >
    >> how can I delete from an array by reference (i.e. I already have a
    >> pointer to the array element) instead of by index.

    >

    Oh, so you have a 'poiter' using Perl.....

    >Maybe this helps:
    >
    >#!/usr/bin/perl
    >use strict;
    >use warnings;
    >
    >my @ary = ( [ 'a', 'a' ]
    > , [ 'b', ' b' ]
    > , 'c'
    > , 123
    > );
    >
    >my $eref = \$ary[1];
    >
    >for (@ary) {
    > print $eref, "\t", \$_, "\n";
    >}
    >
    >
    >> Even better would be to actually delete, not just to undef the
    >> element.

    >
    >perldoc perlop, look for slice.


    Where is the perldoc looking for pointer?
    robic0, Apr 24, 2006
    #4
  5. Michael Goerz wrote:
    > Hi,
    >
    > how can I delete from an array by reference (i.e. I already have a
    > pointer to the array element) instead of by index. Consider the
    > following code:
    >
    >
    > #!/usr/bin/perl -w
    > use strict;
    > use Data::Dumper;
    >
    > my $testarray = [
    > ["bla", "bla"],
    > ["xyz", "xyz"],
    > "blabla",
    > 123 ];
    >
    > print("before:\n", Dumper($testarray), "\n");
    >
    > my $pointer_to_element = $testarray->[1];
    >
    > undef($pointer_to_element); # no effect ...
    > undef(@{$pointer_to_element}); # has *some* effect,
    > # but only works since I know
    > # the element is an array
    > print("after undef by reference:\n", Dumper($testarray), "\n");
    >
    > undef(@{$testarray}[1]); # ... but I want the same as this
    > print("after undef by index:\n", Dumper($testarray), "\n");
    >


    > Even better would be to actually delete, not just to undef the element.
    >


    `delete` semantics differ for arrays and hashes. See: perldoc -f delete.
    The `delete` doc needs a tuneup to clarify the difference upfront IMO.

    So, the grep filter shown would be the best way but just to illustrate
    another way utilizing a hash:

    use Tie::IxHash;
    my $hash = Tie::IxHash->new( map { ($_, $_) } @$testarray );
    $hash->Delete( $pointer_to_element );
    $testarray = [ $hash->Values ];

    --
    Charles DeRykus
    Charles DeRykus, Apr 24, 2006
    #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. Sandeep Grover

    delete on delete !

    Sandeep Grover, Jul 12, 2003, in forum: C++
    Replies:
    19
    Views:
    596
    Chris \( Val \)
    Jul 22, 2003
  2. HeroOfSpielburg
    Replies:
    1
    Views:
    367
    Alf P. Steinbach
    Aug 6, 2003
  3. 0to60
    Replies:
    4
    Views:
    301
    Jerry Coffin
    Dec 19, 2003
  4. Mathieu Malaterre

    delete NULL, what about delete[] NULL

    Mathieu Malaterre, Aug 17, 2004, in forum: C++
    Replies:
    2
    Views:
    3,800
    Mathieu Malaterre
    Aug 17, 2004
  5. Replies:
    12
    Views:
    183
    Robert Dober
    Mar 16, 2007
Loading...

Share This Page