niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » Perl
How to pass an array and scalar as arguments to a subroutine/
Post new topic   Reply to topic Page 1 of 1 [6 Posts] View previous topic :: View next topic
Author Message
TheOrangeRemix
*nix forums beginner


Joined: 28 Jun 2006
Posts: 2

PostPosted: Wed Jun 28, 2006 4:16 pm    Post subject: How to pass an array and scalar as arguments to a subroutine/ Reply with 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.
Back to top
Brian Wakem
*nix forums Guru


Joined: 26 Feb 2005
Posts: 320

PostPosted: Wed Jun 28, 2006 4:29 pm    Post subject: Re: How to pass an array and scalar as arguments to a subroutine/ Reply with quote

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

PostPosted: Wed Jun 28, 2006 4:49 pm    Post subject: Re: How to pass an array and scalar as arguments to a subroutine/ Reply with quote

TheOrangeRemix wrote:
Quote:
sub round {
my($number) = shift;
return int($number + .5);

that would be better written as:
return sprintf("%.0f", $number);

Quote:
}

see PerlFAQ 4 - "Does Perl have a round() function?"
Back to top
Tim Hammerquist
*nix forums Guru Wannabe


Joined: 25 Mar 2005
Posts: 159

PostPosted: Wed Jun 28, 2006 8:44 pm    Post subject: Re: How to pass an array and scalar as arguments to a subroutine/ Reply with quote

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

PostPosted: Wed Jun 28, 2006 10:52 pm    Post subject: Re: How to pass an array and scalar as arguments to a subroutine/ Reply with quote

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

PostPosted: Thu Jun 29, 2006 8:25 am    Post subject: Re: How to pass an array and scalar as arguments to a subroutine/ Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [6 Posts] View previous topic :: View next topic
The time now is Tue Dec 02, 2008 12:30 pm | All times are GMT
navigation Forum index » Programming » Perl
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts postfix smtp authentication using mysql stored user/pass rtresidd Postfix 0 Fri Oct 03, 2008 5:58 am
No new posts Trouble Declaring 3D Array in Header File free2klim C++ 1 Fri Jul 21, 2006 4:07 am
No new posts determine pointer to point to array or single item during... yancheng.cheok@gmail.com C++ 5 Fri Jul 21, 2006 1:17 am
No new posts FAQ 4.41 How can I remove duplicate elements from a list ... PerlFAQ Server Perl 0 Fri Jul 21, 2006 1:03 am
No new posts subroutine weberw@adelphia.net Perl 6 Thu Jul 20, 2006 9:36 pm

Internet Advertising | Loans | Mortgages | Debt Management | MPAA
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 15.8254s ][ Queries: 16 (15.7152s) ][ GZIP on - Debug on ]