3 arrays into @_

Discussion in 'Perl Misc' started by Rocky Allen, Oct 5, 2005.

  1. Rocky Allen

    Rocky Allen Guest

    hi Y'all,

    I am writing a backup script for an Oracle Database server on Linux. It
    is very long so I only included the code I need help with here. The
    entire script is published here. http://bobotheclown.org/scripts/comlang

    I am trying to turn this code into a subroutine:

    open(FH13, ">$contentsfile") or die "Couldnt get FH13:$!";
    print FH13 @joblog;
    print FH13 "\n";
    print FH13 "breakpointforrecover\n";
    print FH13 "\n";
    print FH13 @intersection;
    print FH13 "\n";
    print FH13 "\n";
    print FH13 @splitnames;
    close FH13;

    My question is this:

    Since I have to pass all of those arrays into the subroutine as @_, how
    can I tell when one ends and the next begins?

    I would like for it to be a subroutine because the only way I currently
    get a logfile i if the backup runs successfully. I want it in any case.


    regards,
    Rocky Allen
    Rocky Allen, Oct 5, 2005
    #1
    1. Advertising

  2. Rocky Allen

    Paul Lalli Guest

    Rocky Allen wrote:
    > hi Y'all,
    >
    > I am writing a backup script for an Oracle Database server on Linux. It
    > is very long so I only included the code I need help with here. The
    > entire script is published here. http://bobotheclown.org/scripts/comlang
    >
    > I am trying to turn this code into a subroutine:
    >
    > open(FH13, ">$contentsfile") or die "Couldnt get FH13:$!";
    > print FH13 @joblog;
    > print FH13 "\n";
    > print FH13 "breakpointforrecover\n";
    > print FH13 "\n";
    > print FH13 @intersection;
    > print FH13 "\n";
    > print FH13 "\n";
    > print FH13 @splitnames;
    > close FH13;
    >
    > My question is this:
    >
    > Since I have to pass all of those arrays into the subroutine as @_, how
    > can I tell when one ends and the next begins?


    Don't pass the arrays - pass references to them:

    sub print_to_file {
    my $contentsfile = shift;
    my ($joblog, $intersection, $splitnames) = @_;
    open my $FH13, '>', $contentsfile or
    die "Could not open $contentsfile: $!\n";
    my $old_fh = select $FH13;
    print @$joblog, "\n";
    print "breakpointforrecover\n\n";
    print @$intersection, "\n\n";
    print @splitnames;
    select $old_fh;
    }

    print_to_file($filename, \@joblog, \@intersection, \@splitnames);

    alternatively, use a prototype to auto-create the references for you:
    sub print_to_file ($\@\@\@) {
    #... all code as before
    }
    print_to_file($filename, @joblog, @intersection, @splitnames);

    For more information:
    perldoc perlsub
    perldoc perlreftut
    perldoc perlref
    perldoc -f select

    Paul Lalli
    Paul Lalli, Oct 5, 2005
    #2
    1. Advertising

  3. Rocky Allen

    Rocky Allen Guest

    On Wed, 05 Oct 2005 09:36:37 -0700, Paul Lalli wrote:

    > Rocky Allen wrote:
    >> hi Y'all,
    >>
    >> I am writing a backup script for an Oracle Database server on Linux. It
    >> is very long so I only included the code I need help with here. The
    >> entire script is published here. http://bobotheclown.org/scripts/comlang
    >>
    >> I am trying to turn this code into a subroutine:
    >>
    >> open(FH13, ">$contentsfile") or die "Couldnt get FH13:$!";
    >> print FH13 @joblog;
    >> print FH13 "\n";
    >> print FH13 "breakpointforrecover\n";
    >> print FH13 "\n";
    >> print FH13 @intersection;
    >> print FH13 "\n";
    >> print FH13 "\n";
    >> print FH13 @splitnames;
    >> close FH13;
    >>
    >> My question is this:
    >>
    >> Since I have to pass all of those arrays into the subroutine as @_, how
    >> can I tell when one ends and the next begins?

    >
    > Don't pass the arrays - pass references to them:
    >
    > sub print_to_file {
    > my $contentsfile = shift;
    > my ($joblog, $intersection, $splitnames) = @_;
    > open my $FH13, '>', $contentsfile or
    > die "Could not open $contentsfile: $!\n";
    > my $old_fh = select $FH13;
    > print @$joblog, "\n";
    > print "breakpointforrecover\n\n";
    > print @$intersection, "\n\n";
    > print @splitnames;
    > select $old_fh;
    > }
    >
    > print_to_file($filename, \@joblog, \@intersection, \@splitnames);
    >
    > alternatively, use a prototype to auto-create the references for you:
    > sub print_to_file ($\@\@\@) {
    > #... all code as before
    > }
    > print_to_file($filename, @joblog, @intersection, @splitnames);
    >
    > For more information:
    > perldoc perlsub
    > perldoc perlreftut
    > perldoc perlref
    > perldoc -f select
    >
    > Paul Lalli


    Thank you. I have been reading about references lately and couldnt
    imagine a situation where I would need one. how funny. Thanks again.

    Rocky
    Rocky Allen, Oct 5, 2005
    #3
  4. Rocky Allen <> wrote:
    > hi Y'all,
    >
    > I am writing a backup script for an Oracle Database server on Linux. It
    > is very long so I only included the code I need help with here. The
    > entire script is published here. http://bobotheclown.org/scripts/comlang
    >
    > I am trying to turn this code into a subroutine:
    >
    > open(FH13, ">$contentsfile") or die "Couldnt get FH13:$!";
    > print FH13 @joblog;
    > print FH13 "\n";
    > print FH13 "breakpointforrecover\n";
    > print FH13 "\n";
    > print FH13 @intersection;
    > print FH13 "\n";
    > print FH13 "\n";
    > print FH13 @splitnames;
    > close FH13;
    >
    > My question is this:
    >
    > Since I have to pass all of those arrays into the subroutine as @_, how
    > can I tell when one ends and the next begins?



    You can't, so instead pass three *references* to arrays:

    myfunc( \@joblog, \@intersection, \@splitnames );

    Then deref them in the subroutine body:

    my($joblog, $intersection, $splitnames) = @_;
    ...
    print FH13 @$joblog;


    See also:

    perldoc perlreftut
    perldoc perlref

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 5, 2005
    #4
  5. Rocky Allen

    Paul Lalli Guest

    Rocky Allen wrote:
    > Thank you. I have been reading about references lately and couldnt
    > imagine a situation where I would need one. how funny. Thanks again.


    .... you've never had a need for using a two-dimensional array or hash?
    Or for using a class object?

    Paul Lalli
    Paul Lalli, Oct 5, 2005
    #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. Alexandra Stehman
    Replies:
    5
    Views:
    30,537
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,231
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    552
  4. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    373
  5. Philipp
    Replies:
    21
    Views:
    1,109
    Philipp
    Jan 20, 2009
Loading...

Share This Page