Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > 3 arrays into @_

Reply
Thread Tools

3 arrays into @_

 
 
Rocky Allen
Guest
Posts: n/a
 
      10-05-2005
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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      10-05-2005
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

 
Reply With Quote
 
 
 
 
Rocky Allen
Guest
Posts: n/a
 
      10-05-2005
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

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-05-2005
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
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      10-05-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
char arrays and integer arrays... why the difference? Bill Reyn C++ 3 06-22-2004 12:01 PM
Arrays.asList() returning java.util.Arrays$ArrayList Alexandra Stehman Java 5 06-17-2004 06:04 PM
Arrays and Pointers to Arrays kelvSYC C Programming 2 09-26-2003 06:52 AM
initializing arrays of arrays Mantorok Redgormor C Programming 4 09-11-2003 02:08 AM



Advertisments
 



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