| Author |
Message |
TheOrangeRemix *nix forums beginner
Joined: 28 Jun 2006
Posts: 2
|
Posted: Wed Jun 28, 2006 4:16 pm Post subject:
How to pass an array and scalar as arguments to a subroutine/
|
|
|
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;
}
-------------------------------------------------------------------------------
What ends up happening is that the subroutine returns the new formatted
array and the size in the first array and returns a blank scalar. What
has happened here and how do I fix it? Thanks. |
|
| Back to top |
|
 |
Brian Wakem *nix forums Guru
Joined: 26 Feb 2005
Posts: 320
|
Posted: Wed Jun 28, 2006 4:29 pm Post subject:
Re: How to pass an array and scalar as arguments to a subroutine/
|
|
|
TheOrangeRemix wrote:
| Quote: | 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
|
Switch them around:
unhash($a,@Array_A);
| Quote: | 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.
--
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png |
|
| Back to top |
|
 |
tuser *nix forums beginner
Joined: 15 Feb 2006
Posts: 17
|
Posted: Wed Jun 28, 2006 4:49 pm Post subject:
Re: How to pass an array and scalar as arguments to a subroutine/
|
|
|
TheOrangeRemix wrote:
| Quote: | sub round {
my($number) = shift;
return int($number + .5);
|
that would be better written as:
return sprintf("%.0f", $number);
see PerlFAQ 4 - "Does Perl have a round() function?" |
|
| Back to top |
|
 |
Tim Hammerquist *nix forums Guru Wannabe
Joined: 25 Mar 2005
Posts: 159
|
Posted: Wed Jun 28, 2006 8:44 pm Post subject:
Re: How to pass an array and scalar as arguments to a subroutine/
|
|
|
TheOrangeRemix <fgchan@gmail.com> wrote:
| Quote: | 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;
}
-------------------------------------------------------------------------------
What ends up happening is that the subroutine returns the new formatted
array and the size in the first array and returns a blank scalar. What
has happened here and how do I fix it? Thanks.
|
### untested ###
sub round {
return sprintf("%.0f", shift);
}
sub unhash {
my @fieldarray = @_;
print scalar(@fieldarray), "\n";
foreach my $rawfield ( @fieldarray ) {
if ($rawfield =~ m/########### (.*?)#/) {
$field = $1;
}
}
print @fieldarray;
}
Also, have a look at perlsub, "Prototypes".
HTH,
Tim Hammerquist |
|
| Back to top |
|
 |
Mumia W. *nix forums Guru Wannabe
Joined: 08 May 2006
Posts: 153
|
Posted: Wed Jun 28, 2006 10:52 pm Post subject:
Re: How to pass an array and scalar as arguments to a subroutine/
|
|
|
TheOrangeRemix wrote:
| Quote: | 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" |
|
| Back to top |
|
 |
anno4000@zrz.tu-berlin.de *nix forums beginner
Joined: 28 Jun 2006
Posts: 22
|
Posted: Thu Jun 29, 2006 8:25 am Post subject:
Re: How to pass an array and scalar as arguments to a subroutine/
|
|
|
Mumia W. <mumia.w.18.spam+nospam.usenet@earthlink.net> wrote in comp.lang.perl.misc:
| Quote: | 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 |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|