Professional Web Applications Themes

Delete array element from inside function - PERL Miscellaneous

Hey, all: I've got a function that would like to be able to delete an array element from an array in the caller's scope, given only the array element (not the array itself, or a reference to it). The idea is to have something that duplicates the functionality of delete(), but which tacks a bit of other processing onto it. The existing code is quite simple, and looks like this: ########## sub delete_seg { return undef unless is_segment($_[0]); $_[0]->clear; delete($_[0]); } ######### In the calling script (which has imported the function delete_seg()): ######### delete_seg($word[3]); ######### Unfortunately, this does not work. ...

  1. #1

    Default Delete array element from inside function

    Hey, all:

    I've got a function that would like to be able to delete an array element
    from an array in the caller's scope, given only the array element (not the
    array itself, or a reference to it). The idea is to have something that
    duplicates the functionality of delete(), but which tacks a bit of
    other processing onto it. The existing code is quite simple, and looks
    like this:

    ##########

    sub delete_seg {
    return undef unless is_segment($_[0]);
    $_[0]->clear;
    delete($_[0]);
    }

    #########

    In the calling script (which has imported the function delete_seg()):

    #########

    delete_seg($word[3]);

    #########

    Unfortunately, this does not work. $word[3] remains firmly in place with a
    defined value. What can I do to make this work?


    Jesse S. Bangs [email]jaspaxu.washington.edu[/email]
    [url]http://students.washington.edu/jaspax/[/url]
    [url]http://students.washington.edu/jaspax/blog[/url]
    JS Bangs Guest

  2. #2

    Default Re: Delete array element from inside function

    JS Bangs <jaspaxu.washington.edu> writes:
    > Hey, all:
    >
    > I've got a function that would like to be able to delete an array element
    > from an array in the caller's scope, given only the array element (not the
    > array itself, or a reference to it).
    I don't think this is possible, since $word[3] is a scalar and
    contains no reference to word as a whole. If you don't need it to be
    a general solution, and for some reason you're opposed to passing an
    array ref, I suppose you could do something like:

    $word[3] = {val => $word[3],
    parent => \word};
    delete_seg($word[3]);

    and then:

    sub delete_seg {
    my parent = {$_[0]->{parent}};
    ...
    }

    but I don't see why that would be useful.

    Again, I don't think there's a general solution (but would be happy to
    be proved wrong). You could also probably use tie() for a specific
    solution, but again I think it's unlikely to be a better solution than
    just passing the array ref.

    ap

    --
    ----------------------------------------------------------------------
    Andrew J Perrin - [url]http://www.unc.edu/~aperrin[/url]
    Assistant Professor of Sociology, U of North Carolina, Chapel Hill
    [email]clistsperrin.socsci.unc.edu[/email] * andrew_perrin (at) unc.edu
    Andrew Perrin Guest

  3. #3

    Default Re: Delete array element from inside function

    JS Bangs <jaspaxu.washington.edu> writes:
    > I've got a function that would like to be able to delete an array element
    > from an array in the caller's scope, given only the array element (not the
    > array itself, or a reference to it). The idea is to have something that
    > duplicates the functionality of delete(), but which tacks a bit of
    > other processing onto it.
    Short answer is you can't.

    Slightly longer answer is defined(prototype("CORE::delete")) is false
    meaning that the inbuilt delete() cannot be simulated by a subroutine.

    Slightly longer answer still is that the scalars that are elements of
    an array have an existance independant of the array. When I delete()
    the second element of an array it has no actual effect on the the
    scalar that holds the second element, instead if just disconnects if
    from the array.

    my array = (1,2,3);
    my $r = \$array[1];
    delete $array[2];
    print $$r;

    Similarly if I use the for() or subroutine call mechansims to make $_
    or and element of _ into an alias for an array element it becomes an
    alias to the scalar that contains the value of the array element - the
    alias is in no way associated with the array.
    > The existing code is quite simple, and looks
    > like this:
    >
    > ##########
    >
    > sub delete_seg {
    > return undef unless is_segment($_[0]);
    > $_[0]->clear;
    > delete($_[0]);
    > }
    >
    > #########
    >
    > In the calling script (which has imported the function delete_seg()):
    >
    > #########
    >
    > delete_seg($word[3]);
    >
    > #########
    >
    > Unfortunately, this does not work. $word[3] remains firmly in place with a
    > defined value. What can I do to make this work?
    You cannot, you need to avoid wanting to do it.

    Without more background I can't be sure, but at a guess I'd say you
    appear to be trying to work arround the misconception that Perl
    doesn't have destructors.

    If this is the case forget about delete_seg() function totally and
    rename the 'clear' method to 'DESTROY'.

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\
    . l___\\
    # ll l\\
    ###LL LL\\
    Brian McCauley Guest

  4. #4

    Default Re: Delete array element from inside function

    JS Bangs wrote:
    >
    > I've got a function that would like to be able to delete an array element
    > from an array in the caller's scope, given only the array element (not the
    > array itself, or a reference to it). The idea is to have something that
    > duplicates the functionality of delete(), but which tacks a bit of
    > other processing onto it. The existing code is quite simple, and looks
    > like this:
    >
    > ##########
    >
    > sub delete_seg {
    > return undef unless is_segment($_[0]);
    > $_[0]->clear;
    > delete($_[0]);
    > }
    >
    > #########
    >
    > In the calling script (which has imported the function delete_seg()):
    >
    > #########
    >
    > delete_seg($word[3]);
    >
    > #########
    >
    > Unfortunately, this does not work. $word[3] remains firmly in place with a
    > defined value. What can I do to make this work?
    It looks like you need to use splice:

    splice word, 3, 1;



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  5. #5

    Default Re: Delete array element from inside function

    Brian McCauley sikyal:
    > JS Bangs <jaspaxu.washington.edu> writes:
    >
    > > I've got a function that would like to be able to delete an array element
    > > from an array in the caller's scope, given only the array element (not the
    > > array itself, or a reference to it). The idea is to have something that
    > > duplicates the functionality of delete(), but which tacks a bit of
    > > other processing onto it.
    >
    > Short answer is you can't.
    >
    > Slightly longer answer is defined(prototype("CORE::delete")) is false
    > meaning that the inbuilt delete() cannot be simulated by a subroutine.
    Which also means that delete() is not goto-able, as a simple test
    revealed. Well, drat. That was my last hope.
    > > Unfortunately, this does not work. $word[3] remains firmly in place with a
    > > defined value. What can I do to make this work?
    >
    > You cannot, you need to avoid wanting to do it.
    It sure looks like I do.
    >
    > Without more background I can't be sure, but at a guess I'd say you
    > appear to be trying to work arround the misconception that Perl
    > doesn't have destructors.
    Nah, I'm not worried about object destruction. The kink here is that I
    often want to "delete" a segment object, but "deletion" can take the form
    of actually delete()-ing it, or in clearing all of its values, depending
    on context. I was hoping to have a function that would do both and so
    avoid me having to think about the difference.



    Jesse S. Bangs [email]jaspaxu.washington.edu[/email]
    [url]http://students.washington.edu/jaspax/[/url]
    [url]http://students.washington.edu/jaspax/blog[/url]

    Jesus asked them, "Who do you say that I am?"

    And they answered, "You are the eschatological manifestation of the ground
    of our being, the kerygma in which we find the ultimate meaning of our
    interpersonal relationship."

    And Jesus said, "What?"
    JS Bangs Guest

  6. #6

    Default Re: Delete array element from inside function

    Andrew Perrin (CLists) wrote:
    > JS Bangs <jaspaxu.washington.edu> writes:
    >
    >> Hey, all:
    >>
    >> I've got a function that would like to be able to delete an array
    >> element from an array in the caller's scope, given only the array
    >> element (not the array itself, or a reference to it).
    >
    > I don't think this is possible, since $word[3] is a scalar and
    > contains no reference to word as a whole. If you don't need it to be
    > a general solution, and for some reason you're opposed to passing an
    > array ref, I suppose you could do something like:
    >
    > $word[3] = {val => $word[3],
    > parent => \word};
    > delete_seg($word[3]);
    >
    > and then:
    >
    > sub delete_seg {
    > my parent = {$_[0]->{parent}};
    > ...
    > }
    >
    > but I don't see why that would be useful.
    >
    > Again, I don't think there's a general solution (but would be happy to
    > be proved wrong). You could also probably use tie() for a specific
    > solution, but again I think it's unlikely to be a better solution than
    > just passing the array ref.
    Very creative though! (If a tad overkill for the task at hand.) As you
    mentioned, a array ref would be a bit cleaner (and more general.)

    --
    Islaw


    Islaw Guest

  7. #7

    Default Re: Delete array element from inside function

    Brian McCauley wrote:
    >
    [Line numbers added]
    > 1 my array = (1,2,3);
    > 2 my $r = \$array[1];
    > 3 delete $array[2];
    > 4 print $$r;
    I think you meant
    delete $array[1];
    line 3 :)

    --
    Islaw


    Islaw Guest

Similar Threads

  1. how to check array element's values if this element isempty
    By jiecoldfusion in forum Macromedia ColdFusion
    Replies: 2
    Last Post: February 25th, 10:00 PM
  2. Is there a function to see if something is an element of an array?
    By Wiggins D'Anconia in forum PERL Beginners
    Replies: 0
    Last Post: September 30th, 05:05 PM
  3. Replies: 1
    Last Post: September 30th, 05:05 PM
  4. [PHP] Array element from function return
    By John W. Holmes in forum PHP Development
    Replies: 1
    Last Post: August 24th, 03:41 PM
  5. Array element from function return
    By Wouter Van Vliet in forum PHP Development
    Replies: 0
    Last Post: August 24th, 03:24 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139