Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > hash as argument

Reply
Thread Tools

hash as argument

 
 
Aaron DeLoach
Guest
Posts: n/a
 
      07-08-2004
I am trying to call a sub with two arguments. One being a hash generated
from a sub, the other being a string. I've tried other variations but can
only get the receiving sub to recognize either the hash or the string, but
never both.

e.g, &do_something(&create_hash, "This is the string.");

sub do_something
{
my (%hash, $string) = @_;

process...
}

sub create_hash
{
my %h;
# construct the hash ...
return %h;
}

Can anyone get me in the right direction?

Regards,
Aaron


 
Reply With Quote
 
 
 
 
Matija Papec
Guest
Posts: n/a
 
      07-08-2004
X-Ftn-To: Aaron DeLoach

"Aaron DeLoach" <(E-Mail Removed)> wrote:
>I am trying to call a sub with two arguments. One being a hash generated
>from a sub, the other being a string. I've tried other variations but can
>only get the receiving sub to recognize either the hash or the string, but
>never both.
>
>e.g, &do_something(&create_hash, "This is the string.");
>
>sub do_something
>{
> my (%hash, $string) = @_;
>
> process...
>}
>
>sub create_hash
>{
> my %h;
> # construct the hash ...
> return %h;
>}
>
>Can anyone get me in the right direction?


Try,
my ($string, %hash) = @_;

this is for the same reason you can't
my (@arr, $string) = @_;

as @arr gets all arguments from @_



--
Matija
 
Reply With Quote
 
 
 
 
Sherm Pendley
Guest
Posts: n/a
 
      07-08-2004
Aaron DeLoach wrote:

> I am trying to call a sub with two arguments. One being a hash generated
> from a sub, the other being a string. I've tried other variations but can
> only get the receiving sub to recognize either the hash or the string, but
> never both.
>
> e.g, &do_something(&create_hash, "This is the string.");


You are using Perl 5.x, right? Not Perl 4? Unless you're using Perl 4, or
you know precisely what the & does and why you want to do that, don't use
it. And don't use double-quotes on strings unless it's necessary.

do_something(create_hashref(), 'This is a string');

> sub do_something
> {
> my (%hash, $string) = @_;
>
> process...
> }


sub do_something {
my ($hashref, $string) = @_;

# Use a hash reference
my @keys = keys(%$hashref);
}

> sub create_hash
> {
> my %h;
> # construct the hash ...
> return %h;
> }


sub create_hashref {
my $h = {};

# Store something via the hashref
$h->{'foo'} = 'bar';
$h->{'baz'} = 'buzz';

return $h;
}

> Can anyone get me in the right direction?


See also:

perldoc perldsc
perldoc perllol

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
Aaron DeLoach
Guest
Posts: n/a
 
      07-08-2004
That's one method I didn't try... but it worked.

Thanks

"Matija Papec" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> X-Ftn-To: Aaron DeLoach
>
> "Aaron DeLoach" <(E-Mail Removed)> wrote:
> >I am trying to call a sub with two arguments. One being a hash generated
> >from a sub, the other being a string. I've tried other variations but can
> >only get the receiving sub to recognize either the hash or the string,

but
> >never both.
> >
> >e.g, &do_something(&create_hash, "This is the string.");
> >
> >sub do_something
> >{
> > my (%hash, $string) = @_;
> >
> > process...
> >}
> >
> >sub create_hash
> >{
> > my %h;
> > # construct the hash ...
> > return %h;
> >}
> >
> >Can anyone get me in the right direction?

>
> Try,
> my ($string, %hash) = @_;
>
> this is for the same reason you can't
> my (@arr, $string) = @_;
>
> as @arr gets all arguments from @_
>
>
>
> --
> Matija



 
Reply With Quote
 
Aaron DeLoach
Guest
Posts: n/a
 
      07-08-2004

"Sherm Pendley" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Aaron DeLoach wrote:
>
> > I am trying to call a sub with two arguments. One being a hash generated
> > from a sub, the other being a string. I've tried other variations but

can
> > only get the receiving sub to recognize either the hash or the string,

but
> > never both.
> >
> > e.g, &do_something(&create_hash, "This is the string.");

>
> You are using Perl 5.x, right? Not Perl 4? Unless you're using Perl 4, or
> you know precisely what the & does and why you want to do that, don't use
> it. And don't use double-quotes on strings unless it's necessary.


Perl 5.x

I use the '&' mainly to distinguish sub calls. Why not?

I used the double-quotes in the example.

Thanks for your help!

>
> do_something(create_hashref(), 'This is a string');
>
> > sub do_something
> > {
> > my (%hash, $string) = @_;
> >
> > process...
> > }

>
> sub do_something {
> my ($hashref, $string) = @_;
>
> # Use a hash reference
> my @keys = keys(%$hashref);
> }
>
> > sub create_hash
> > {
> > my %h;
> > # construct the hash ...
> > return %h;
> > }

>
> sub create_hashref {
> my $h = {};
>
> # Store something via the hashref
> $h->{'foo'} = 'bar';
> $h->{'baz'} = 'buzz';
>
> return $h;
> }
>
> > Can anyone get me in the right direction?

>
> See also:
>
> perldoc perldsc
> perldoc perllol
>
> sherm--
>
> --
> Cocoa programming in Perl: http://camelbones.sourceforge.net
> Hire me! My resume: http://www.dot-app.org



 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      07-08-2004
Aaron DeLoach wrote:

> I use the '&' mainly to distinguish sub calls. Why not?


It's deprecated for normal use in Perl 5. It disables prototype checking,
and passes the current @_ to the called sub instead of creating a lexically
scoped @_ within the sub.

So unless you're using a Very Old Perl, or you have a specific reason for
needing one of the above effects, don't use & to call subs.

A better way to make sub calls more visually distinctive is to always use
the ()s, even when you're not passing any arguments. That helps them stand
out visually, but doesn't have the unwanted side-effects of using &.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      07-09-2004
Sherm Pendley wrote:
> And don't use double-quotes on strings unless it's necessary.


Has Larry Wall made a statement on this?

Using double quotes in a situation where single quotes could also
be used is not a programming error. It is a matter of style.
-Joe
 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      07-09-2004
Joe Smith wrote:

> Has Larry Wall made a statement on this?


I think his statement that "there's more than one way to do it" is relevant.
Quite a shame so many folks have forgotten it.

> It is a matter of style.


I agree, but there are a few regulars here who view it as a matter of
religion. Minimizing the use of double-quotes, at least when posting code
here, helps reduce the incessant whining about it.

I'd fight the good fight if it mattered, but in this case there's nothing to
be gained by arguing the point.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
Andrew Hamm
Guest
Posts: n/a
 
      07-09-2004
Sherm Pendley wrote:
> Joe Smith wrote:
>
>> Has Larry Wall made a statement on this?

>
> I think his statement that "there's more than one way to do it" is
> relevant. Quite a shame so many folks have forgotten it.
>
>> It is a matter of style.

>
> I agree, but there are a few regulars here who view it as a matter of
> religion. Minimizing the use of double-quotes, at least when posting
> code here, helps reduce the incessant whining about it.
>


I'm not changing - I hate it when I spend 15 minutes looking for a bug only
to finally discover I've tried to add $thingy inside single quotes. Too he11
with that...


 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      07-09-2004
Andrew Hamm wrote:

> I'm not changing


So now someone wants to debate whether it's a subject worth arguing about.
Oy! You just can't win with this group...

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Function pointers, variable argument functions calling other variable-argument functions (sort of) S?ren Gammelmark C Programming 1 01-07-2005 09:41 PM
How to pass variable argument list to another function w/ variable argument list? Ben Kial C Programming 1 11-15-2004 01:51 AM
defaulting argument to previous argument Bhushit Joshipura C++ 5 12-30-2003 03:21 PM



Advertisments