Ok, let's hope i can shed some light on this situation.

What's going wrong is that you are using a reference to an array slice...
not sure why that's a big problem but it is apparently.
in your case, there are 2 solutions:

1) do something like the following if you insist on using references

    @foo = (1,2,3,4,5,6,7,8,9);
    @baz = @foo[1..5];
    bar(\@baz);

    sub bar {
        my $aref = shift;
        for (@$aref) {print}
    }

    #which will nicely print '23456'

2) Seeing your passing an array slice to the sub, there is no real need for
references, so you could do something like this:
      @foo = (1,2,3,4,5,6,7,8,9);
       bar(@foo[1..5]);

    sub bar {
         my @a = @_;
         for (@a) {print}
    }

     #which will nicely print '23456'

I hope this will solve your problem for now...

Regards,

Jos Boumans

Paul wrote:

> --- 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