Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: how to assign hash element value to scalar?

by jugdish114 (Initiate)
on Nov 14, 2006 at 20:14 UTC ( [id://584062]=note: print w/replies, xml ) Need Help??


in reply to Re: how to assign hash element value to scalar?
in thread how to assign hash element value to scalar?

thanks, yes this is part of a subroutine, here's the beginning of it:
sub enter_order { my ($userid) = $_[0]; my ($password) = $_[1]; my ($projectid) = $_[2]; my ($ordertypeid) = $_[3]; my ($orderdateid) = $_[4]; my ($accountid) = $_[5]; my ($segcodeid) = $_[6]; my ($marketid) = $_[7]; my ($marketday) = $_[8]; my ($marketmonth) = $_[9]; my ($marketyear) = $_[10]; my ($putcall) = $_[11]; my ($strikeprice) = $_[12]; my ($bs) = $_[13]; my ($quantity) = $_[14]; my ($orderprice) = $_[15]; my ($entered_by) = $_[16];

yes, when i add 'use strict' it is not very pretty, some of the code outside the subroutine give errors.

and thanks for pointing out the error with the "==" rather than "eq" i think i write in too many languages to keep them straight.

see ya,

Replies are listed 'Best First'.
Re^3: how to assign hash element value to scalar?
by liverpole (Monsignor) on Nov 14, 2006 at 20:28 UTC
        when i add 'use strict' it is not very pretty, some of the code outside the subroutine give errors.

    You don't really believe that by not adding 'use strict' it means the errors magically go away, do you? :-)


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Ok, Ok, Liverpole wins, it was his gently embarassing remarks that got me to clean up the error when 'use strict' was added back in the code. Once I worked through all the global and local referencing, the code behaved pretty well. So my syntax was correct, it seems that Perl was getting confused somewhere because of lack of variable definition.

      Looks good, thanks again
      fc
Re^3: how to assign hash element value to scalar?
by Hofmator (Curate) on Nov 15, 2006 at 07:42 UTC
    Well, as an aside ... if you pass that many parameters to a subroutine, have you considered using a hash and using name => value pairs? That would greatly reduce the risk of errors and makes all these variable declarations unnecessary ...

    I was thinking of something like the following ... suitably adjusted to your concrete requirements of course (are all parameters necessary, should they get a default value if not given, ...)

    # call the sub my_sub(userid => 'foo', password => 'bar'); sub my_sub { my %default_values = (userid => 'user', password => 'passwd'); my %param = (%default_values, @_); print $param{userid}, $/; print $param{password}, $/; # ... } # or with all required arguments sub my_sub { my @required = qw/userid password/; my %param = @_; for my $req (@required) { die "Subroutine my_sub called without required parameter: $req" unless exists $param{$req}; } print $param{userid}, $/; print $param{password}, $/; # ... }

    -- Hofmator

    Code written by Hofmator and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re^3: how to assign hash element value to scalar?
by johngg (Canon) on Nov 14, 2006 at 23:01 UTC
    It may be that you want to explicitly show which element of the subroutine's arguments is assigned to which variable but if not, you could save some typing by doing

    my ($userid, $password, $projectid, ... , $entered_by) = @_;

    I hope this is of use.

    Cheers,

    JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://584062]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 07:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found