How to pass an array and scalar as arguments to a subroutine/

T

TheOrangeRemix

I'm writing a Perl script to take in an array and a scalar as two
arguments and pass them to a subroutine.
Each element of the array has contents ########### word ########### and
the scalar being passed is the size of the array.
The subroutine is supposed to strip the '#" from the contents of each
element and just report the "word" in one array, and report the size of
the array in a scalar.

Here is the code:

-------------------------------------------------------------------------------

unhash(@Array_A,$a); # array and size

sub round {
my($number) = shift;
return int($number + .5);
}

sub unhash
{
my(@fieldarray) = @_;
my($sizearray) = shift;


print "$sizearray\n";

for ($i = 0; $i < $sizearray; $i++)
{
if ($fieldarray[$i] =~ m/########### (.*?)#/)

{
$fieldarray[$i] = $1;

}



}
print @fieldarray;
}
 
B

Brian Wakem

TheOrangeRemix said:
I'm writing a Perl script to take in an array and a scalar as two
arguments and pass them to a subroutine.
Each element of the array has contents ########### word ########### and
the scalar being passed is the size of the array.
The subroutine is supposed to strip the '#" from the contents of each
element and just report the "word" in one array, and report the size of
the array in a scalar.

Here is the code:


Switch them around:

unhash($a,@Array_A);

sub round {
my($number) = shift;
return int($number + .5);
}

sub unhash
{
my(@fieldarray) = @_;
my($sizearray) = shift;


my ($sizearray,@fieldarray) = @_;



BUT:
1) Don't use $a as a variable name.
2) You don't need to know the size of the array, just use a foreach loop
on the array.
3) Even if you did need to know the size of the array, find out when you
need to inside the sub.
4) It is much better to pass a reference to an array than the actual
array as this avoids the problem you have had and uses less memory.
 
M

Mumia W.

TheOrangeRemix said:
I'm writing a Perl script to take in an array and a scalar as two
arguments and pass them to a subroutine.
Each element of the array has contents ########### word ########### and
the scalar being passed is the size of the array.
The subroutine is supposed to strip the '#" from the contents of each
element and just report the "word" in one array, and report the size of
the array in a scalar.
[...]

sub unhash
{
my(@fieldarray) = @_;
my($sizearray) = shift;


print "$sizearray\n";

for ($i = 0; $i < $sizearray; $i++)
{ [...]

Hmm.
....array size provided as a separate parameter...
....using 'for' to iterate over an array...

Why do I detect a C programmer? :)

Perl stores the sizes of arrays internally and has more powerful looping
constructs for arrays than the 'for' statement:

sub unhash_array {
map m/(\w+)/ && $1, @_;
}

Learn Perl. It'll pay off ;)


And like Brian Wakem said, it's more efficient to use a reference. See
"perldoc perlref"
 
A

anno4000

Mumia W. said:
sub unhash_array {
map m/(\w+)/ && $1, @_;
}

A capturing regex in list context returns the list of captures (a single
one in your case) on success and an empty list on failure. No need to
refer to $1.

sub unhash_array { map m/(\w+)/, @_ }

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top