Professional Web Applications Themes

How to write a function that clears an array? - PERL Beginners

I'm passing a singularly dimensioned array to a function. I need to clear the array in the function so that when I return to the main program, there are no entries in the array variable I passed to the function. Can someone write a small function that does this? Thanks, Sieg __________________________________ Do you Yahoo!? Yahoo! Tax Center - File online by April 15th http://taxes./filing.html...

  1. #1

    Default How to write a function that clears an array?

    I'm passing a singularly dimensioned array to a
    function. I need to clear the array in the function so
    that when I return to the main program, there are no
    entries in the array variable I passed to the
    function.

    Can someone write a small function that does this?
    Thanks,
    Sieg

    __________________________________
    Do you Yahoo!?
    Yahoo! Tax Center - File online by April 15th
    http://taxes./filing.html
    Richard Guest

  2. #2

    Default Re: How to write a function that clears an array?

    On Apr 10, Richard Heintze said:
     

    You have to pass the array by reference, then.

    my array = (1 .. 4);

    print "(array)\n"; # (1 2 3 4)
    my_func(\array);
    print "(array)\n"; # ()

    sub my_func {
    my $array_ref = shift;
    $array_ref = ();
    }

    --
    Jeff "japhy" Pinyan com http://www.pobox.com/~japhy/
    RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
    CPAN ID: PINYAN [Need a programmer? If you like my work, let me know.]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.

    Jeff Guest

  3. #3

    Default RE: How to write a function that clears an array?

    Jeff 'japhy' Pinyan <org> wrote:
    :
    : On Apr 10, Richard Heintze said:
    :
    : > I'm passing a singularly dimensioned array to
    : > a function. I need to clear the array in the
    : > function so that when I return to the main
    : > program, there are no entries in the array
    : > variable I passed to the function.
    :
    : You have to pass the array by reference, then.
    :
    : my array = (1 .. 4);
    :
    : print "(array)\n"; # (1 2 3 4)
    : my_func(\array);
    : print "(array)\n"; # ()
    :
    : sub my_func {
    : my $array_ref = shift;
    : $array_ref = ();
    : }

    Jeff's example will do what you want, but I'm not
    certain it is a Good Thing. I try to avoid modifying
    function arguments inside a function. There are some
    good reasons, like when the array is very large, but
    in general I would advise avoiding this.

    This seems clearer to me:

    my array = ( 1 .. 4 );

    my_func(\array);

    array = ();

    sub my_func {
    my $array_ref = shift;
    # do stuff
    }


    Actually, it makes me wonder why we are using
    array at all:

    my_func( [ 1 .. 4 ] );

    sub my_func {
    my $array_ref = shift;
    # do stuff
    }



    HTH,

    Charles K. Clarkson
    --
    Mobile Homes Specialist
    254 968-8328

    Get real! This is a discussion group, not a helpdesk.
    You post something, we discuss its implications. If
    the discussion happens to answer a question you've
    asked, that's incidental. - nobull in comp.lang.perl.misc



    Charles Guest

Similar Threads

  1. Replies: 3
    Last Post: March 19th, 07:59 PM
  2. Read and write byte array into PDF file
    By yiyun@adobeforums.com in forum Adobe Acrobat SDK
    Replies: 2
    Last Post: December 1st, 07:40 AM
  3. Form Clears If Emailed Through Browser
    By Frascone_John@adobeforums.com in forum Adobe Acrobat Windows
    Replies: 2
    Last Post: May 24th, 09:08 PM
  4. loop and write array
    By Micah Montoy in forum PHP Development
    Replies: 0
    Last Post: August 3rd, 01:50 PM
  5. Attempting to write a gets like function for tcp
    By David Schwartz in forum UNIX Programming
    Replies: 2
    Last Post: July 9th, 02:44 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