Re: accessing arrays in a different sub-routine

From: A. Sinan Unur (1usa_at_llenroc.ude.invalid)
Date: 03/29/05


Date: Mon, 28 Mar 2005 23:05:53 GMT


"erik" <ewitkop90@hotmail.com> wrote in news:1112049496.433969.94040
@g14g2000cwa.googlegroups.com:

> I just read that, thanks.

Please quote an appropriate amount of context when replying.

> That sheds some light. So basically with my
> example above, and due to it's dynamic nature, the only way I can get
> this working is a sub-function within a subfunction. I really didn't
> want to do that, but I take it I have no choice.

You lost me here.

Here is a simple example of what you can do:

#! /usr/bin/perl

my $files = read_files_in_current_dir();

$files or die "No files in current directory\n";

my_print($files);

sub read_files_in_current_dir {
    opendir my $dir, '.'
        or die "Cannot open current directory: $!";
    [ grep { -f } readdir $dir ];
}

sub my_print {
    print "$_\n" for sort @{ shift() };
}

__END__
    
The first subroutine you call generates the array you want, and returns
a reference to it. Then, you pass that reference to other subroutines
that work on the array.

No need for nested subroutines.

> BTW, what was wrong with the name that I gave my array
> @bandwidth_array? You lost me on that one.

For me, the '@' in front of the name already signifies that the variable
is an array, why replicate that information?

Sinan



Relevant Pages

  • Re: VB-101: Passing Arrays ByVal vs ByRef
    ... changed if an array is passed by Val. ... ' new reference ... As each function creates a new array object, ... 'secondArray' and 'secondArrayCopy'. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Need help with textboxes
    ... The JavaScript 1.5 Reference already states: ... All forms and their children are stored in an array ... use dot notation or object literals. ... No. Bracket property accessors allow their argument to be any string value. ...
    (comp.lang.javascript)
  • Re: Garbage Collection Issues in long-standing services
    ... the pinned array should get unpinned or ... The receive case is done quite well, I do create a 4K buffer and reuse it ... and remove the reference to my wrapper socket class, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to give selective access to the methods in a class?
    ... different memory locations at different times. ... The reference still leads to the ... So array resizing ... program pushes certain objects into the circular buffer, ...
    (comp.lang.java.programmer)
  • Re: Perlish map() function
    ... the reference material, the Array constructor was introduced in JavaScript ... evaluation would result in a ReferenceError. ... identifier shows that it is true. ...
    (comp.lang.javascript)