overginign two arrays to a package!

Discussion in 'Perl Misc' started by RBCS, Mar 9, 2006.

  1. RBCS

    RBCS Guest

    Hello,

    Why are in the package Testing all the Vars from arr1 and arr2 in the
    @array1 array and the @array2 empty?

    Roman

    ___________________________

    my (@tasks);
    my (@args);

    my $ResultofTesting = Testing->info($par1, $par2, @arr1, @arr2);


    ackage Testing;

    sub info{
    my( undef, $param1, $param2, @array1, @array2 ) = @_;
    RBCS, Mar 9, 2006
    #1
    1. Advertising

  2. RBCS

    Paul Lalli Guest

    RBCS wrote:
    > Subject: Re: overginign two arrays to a package!


    I have *no* idea what that first word is supposed to be. Please
    consider at least using a spell checker.

    > Why are in the package Testing all the Vars from arr1 and arr2 in the
    > @array1 array and the @array2 empty?
    >
    > my (@tasks);
    > my (@args);
    >
    > my $ResultofTesting = Testing->info($par1, $par2, @arr1, @arr2);
    >
    >
    > package Testing;
    >
    > sub info{
    > my( undef, $param1, $param2, @array1, @array2 ) = @_;


    Because that's how subroutines work in Perl. More generally, that's
    how lists and arrays work in Perl. Arrays "flatten" when making a list
    assignment. If you want to keep track of where one array ended and
    another began, you have to use references.

    my $ResultOfTesting = Testing->info($par1, $par2, \@arr1, @arr2);

    sub info {
    my $class = shift; #don't throw this away...
    my ($param1, $param2, $ref1, $ref2) = @_;
    my @array1 = @{$ref1};
    my @array2 = @{$ref2};
    # . . .
    }

    Please read:
    perldoc perlsub
    perldoc perlreftut

    Paul Lalli
    Paul Lalli, Mar 9, 2006
    #2
    1. Advertising

  3. RBCS

    RBCS Guest

    Hello Paul

    Thanks

    I Got it...!

    Roman

    On Thu, 09 Mar 2006 12:47:04 -0800, Paul Lalli wrote:

    > RBCS wrote:
    >> Subject: Re: overginign two arrays to a package!

    >
    > I have *no* idea what that first word is supposed to be. Please consider
    > at least using a spell checker.
    >
    >> Why are in the package Testing all the Vars from arr1 and arr2 in the
    >> @array1 array and the @array2 empty?
    >>
    >> my (@tasks);
    >> my (@args);
    >>
    >> my $ResultofTesting = Testing->info($par1, $par2, @arr1, @arr2);
    >>
    >>
    >> package Testing;
    >>
    >> sub info{
    >> my( undef, $param1, $param2, @array1, @array2 ) = @_;

    >
    > Because that's how subroutines work in Perl. More generally, that's how
    > lists and arrays work in Perl. Arrays "flatten" when making a list
    > assignment. If you want to keep track of where one array ended and
    > another began, you have to use references.
    >
    > my $ResultOfTesting = Testing->info($par1, $par2, \@arr1, @arr2);
    >
    > sub info {
    > my $class = shift; #don't throw this away... my ($param1, $param2,
    > $ref1, $ref2) = @_; my @array1 = @{$ref1};
    > my @array2 = @{$ref2};
    > # . . .
    > }
    > }
    > Please read:
    > perldoc perlsub
    > perldoc perlreftut
    >
    > Paul Lalli
    RBCS, Mar 9, 2006
    #3
  4. Paul Lalli <> wrote:


    > my $ResultOfTesting = Testing->info($par1, $par2, \@arr1, @arr2);

    ^^^

    You missed a backslash:

    my $ResultOfTesting = Testing->info($par1, $par2, \@arr1, \@arr2);


    > sub info {
    > my $class = shift; #don't throw this away...
    > my ($param1, $param2, $ref1, $ref2) = @_;
    > my @array1 = @{$ref1};
    > my @array2 = @{$ref2};
    > # . . .
    > }



    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 9, 2006
    #4
  5. RBCS

    Anno Siegel Guest

    Tad McClellan <> wrote in comp.lang.perl.misc:
    > Paul Lalli <> wrote:
    >
    >
    > > my $ResultOfTesting = Testing->info($par1, $par2, \@arr1, @arr2);

    > ^^^
    >
    > You missed a backslash:
    >
    > my $ResultOfTesting = Testing->info($par1, $par2, \@arr1, \@arr2);


    Not necessarily. Change

    > > sub info {
    > > my $class = shift; #don't throw this away...
    > > my ($param1, $param2, $ref1, $ref2) = @_;


    to

    sub info {
    my( $class, $param1, $param2, $ref1, @array) = @_;

    to make it work.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Mar 10, 2006
    #5
  6. Anno Siegel wrote:
    > Tad McClellan <> wrote in comp.lang.perl.misc:
    > > Paul Lalli <> wrote:
    > >
    > > > my $ResultOfTesting = Testing->info($par1, $par2, \@arr1, @arr2);

    > > ^^^
    > >
    > > You missed a backslash:
    > >
    > > my $ResultOfTesting = Testing->info($par1, $par2, \@arr1, \@arr2);

    >
    > Not necessarily.


    TMTOWTDI! However some are preferable to others.

    > Change
    >
    > > > sub info {
    > > > my $class = shift; #don't throw this away...
    > > > my ($param1, $param2, $ref1, $ref2) = @_;

    >
    > to
    >
    > sub info {
    > my( $class, $param1, $param2, $ref1, @array) = @_;
    >
    > to make it work.


    Yes that works. But when you come along next week and want to add
    another argument to info() you'll curse yourself.

    Also as a stylistic convention I'm certainly in favour of shifting a
    method's class/self argument off @_ as a separate operation from
    unpacking the remainder of the argument list.

    > Anno
    > --
    > If you want to post a followup via groups.google.com, don't use
    > the broken "Reply" link at the bottom of the article. Click on
    > "show options" at the top of the article, then click on the
    > "Reply" at the bottom of the article headers.
    Brian McCauley, Mar 12, 2006
    #6
  7. Paul Lalli wrote:

    > Please consider at least using a spell checker.


    See also:

    http://www.geocities.com/SouthBeach/1915/chequer.html

    > sub info {
    > my $class = shift; #don't throw this away...
    > my ($param1, $param2, $ref1, $ref2) = @_;
    > my @array1 = @{$ref1};
    > my @array2 = @{$ref2};


    I think you should point out that copying the arrays like this could be
    expensive and usually not necessary. It is often simpler to work with
    $ref1 and $ref2 dierctly.
    Brian McCauley, Mar 12, 2006
    #7
    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. Alf P. Steinbach
    Replies:
    0
    Views:
    422
    Alf P. Steinbach
    Aug 18, 2003
  2. Philipp
    Replies:
    21
    Views:
    1,102
    Philipp
    Jan 20, 2009
  3. Kev Jackson
    Replies:
    2
    Views:
    100
  4. Joshua Muheim
    Replies:
    8
    Views:
    182
    Dejan Dimic
    Nov 23, 2007
  5. Allen Walker

    Merging two arrays -> array of arrays

    Allen Walker, May 21, 2010, in forum: Ruby
    Replies:
    6
    Views:
    152
    Jesús Gabriel y Galán
    May 21, 2010
Loading...

Share This Page