How to initialize a referenced array?

Discussion in 'Perl Misc' started by feltra, Sep 25, 2010.

  1. feltra

    feltra Guest

    Hi,

    Am using arrays with only references in a sub-routine. While I got
    the hang of how to access an element of the array using the '->'
    operator, I do not know how to intialize this array. I.e. I want to
    be able to do something like

    @myarr=(); $#myarr = -1;

    inside the subroutine, but myarr is only a reference to an array not
    the actual array...

    Hope the above problem description is clear.

    If anyone knows how to do this, kindly help by posting the answer or
    tell me where to look...

    Thanks & Best Regards,
    -feltra
     
    feltra, Sep 25, 2010
    #1
    1. Advertising

  2. feltra

    feltra Guest

    Thanks for the quick reply, Ben.. It works like a charm!

    Basically I am copying one array to another, and within the sub, I
    wouldn't know which one to init (ie. which is the receiving array),
    except thru references...

    It's possible that there is a better method than copying, but at least
    this solution will solve my problem for now..

    Thanks a lot & Best Regards,
    -feltra


    On Sep 25, 9:42 pm, Ben Morrow <> wrote:
    > Quoth feltra <>:
    >
    > > Am using arrays with only references in a sub-routine.  While I got
    > > the hang of how to access an element of the array using the '->'
    > > operator,  I do not know how to intialize this array.   I.e. I wantto
    > > be able to do something like

    >
    > > @myarr=();   $#myarr = -1;

    >
    > Assuming $aref holds an array reference, that would be
    >
    >     @$aref = ();    $#$aref = -1;
    >
    > but I don't see why you think you need to do this. What are you actually
    > trying to do?
    >
    > Ben
     
    feltra, Sep 25, 2010
    #2
    1. Advertising

  3. [Please do not top-post, that is considered rude]
    [Trying to correct]
    feltra <> wrote:
    >On Sep 25, 9:42 pm, Ben Morrow <> wrote:
    >> Quoth feltra <>:
    >>
    >> > Am using arrays with only references in a sub-routine.  While I got
    >> > the hang of how to access an element of the array using the '->'
    >> > operator,  I do not know how to intialize this array.   I.e. I want to
    >> > be able to do something like

    >>
    >> > @myarr=();   $#myarr = -1;

    >>
    >> Assuming $aref holds an array reference, that would be
    >>     @$aref = ();    $#$aref = -1;

    >
    >Basically I am copying one array to another, and within the sub, I


    Ok, so just do
    @target = @source;

    >wouldn't know which one to init (ie. which is the receiving array),
    >except thru references...


    ???
    Are you trying to say that in order to pass an array to a sub (instead
    of its elements) you have to pass a reference?

    >It's possible that there is a better method than copying, but at least
    >this solution will solve my problem for now..


    Still there is no need to initialize the array.
    As simple
    @$target = @$source
    should do.

    jue
     
    Jürgen Exner, Sep 25, 2010
    #3
    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. Brandon
    Replies:
    5
    Views:
    5,994
    Mike Treseler
    Sep 29, 2005
  2. Replies:
    10
    Views:
    692
  3. .rhavin
    Replies:
    7
    Views:
    129
    Heinrich Mislik
    Oct 8, 2004
  4. Henry Law
    Replies:
    8
    Views:
    306
    Eric J. Roode
    Nov 14, 2004
  5. Replies:
    3
    Views:
    106
    Tad J McClellan
    Nov 11, 2008
Loading...

Share This Page