--- Gary Stainburn <[EMAIL PROTECTED]> wrote:
> Hi all,

Hi, Gary. =o)

> I have an array that holds the source code to a cobol program.  I
> then have a number of integers that hold the subscript for section
and 
> division changes.
> I then have subs that parse sections of that array. e.g. a sub called
> file_control will parse the file-control section and extract 
> information from the file select statements.

Actually, that sounds like a pretty cool arrangement.

> At the moment my code works because I store the array as a global 
> variable and pass to the sub the start and end array elements.

Ah. A reasonable way to deal with the circumstance, but globals are
commonly considered "bad", lol....
 
> I want to now make my code a bit more flexible and simply pass a 
> reference to the section I require.  The code I've tried may give a 
> better idea of what i mean.  when I run my code I get the following 
> error on the 'foreach (@$lines)' line
> 
> Not an ARRAY reference at ./s line 109.

Hmm....looking below, the cause is not immediately apparent to me.
In fact, what you've done was the first solution that popped into my
mind. (Somebody please explain the problem with that for me, too?)
 
> I could simply pass the array sections to the subroutines but that 
> would be very inefficient - the procedure divisions of some the 
> programs are 5k lines - hence the attempt to reference.

I think this is a good candidate for an object.
Try this:

  my $obj = {};
  $obj->{CODE} = [ @lines ]; 

now you can undef @lines to recoup that memory (though Perl probably
won't give it back.....)

Then you can do neat things like putting the section indexes on the
object also:

  $obj->{FILECTL} = $fc_ndx;

Then all you have to do is pass the object to each function.
  file_control($obj);

and they can grab the pieces they need off it.

 sub func {
     my $obj = shift;
     for my $ndx ($obj->{SECTION1} .. ($obj->{SECTION2}-1)) {
         # code for section
     }
 }
 
try looking at the perldocs for
    perlref
    perlreftut
    perlobj
    perlboot
    perltoot
    perltootc

Paul

(Original code left below for reader's convenience)

> my @lines=( # simulate read cobol file
> ........
> "       file-control.\r\n",
> "           select jobfile\r\n",
> "               assign external das1\r\n",
> "               organization is indexed\r\n",
> "               access dynamic\r\n",
> "               record key is job-jf-key\r\n",
> "               file status is w01-jobf-stat\r\n",
> "               lock mode is automatic with rollback.\r\n",
> .......
> 
> &file_control(\@lines[10..36]); # pass pointer to array elements
> 10-36
> 
> sub file_control() {
>   my ($lines)=@_;
>   foreach (@$lines) {   # process each line in array section
>     next if (/^      [\/\*]/);
> ........


__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/

Reply via email to