Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Passing a hash to a sub

Expand Messages
  • Jeff Eggen
    ... [snip] ... Well, it looks like you re passing your subroutine a reference to a hash. So, you can t get a hash out of @_, as there s only a reference in
    Message 1 of 2 , Sep 4, 2002
    • 0 Attachment
      >>> michael.s.eberhart@... 09/04/02 12:16pm >>>
      >I have the following code, and it fails. How do you pass a hash to a
      >sub so that you can access its keys and values?
      [snip]
      >create_file(\%rec);

      >sub create_file
      >{
      > my ($c, $o);
      > my %new_rec = shift;

      Well, it looks like you're passing your subroutine a reference to a hash. So, you can't get a hash out of @_, as there's only a reference in there.

      Try:

      my $new_rec = shift;

      And then later on just change your calls to %new_rec accordingly:

      foreach my $c (keys %{$new_rec})

      Stuff like that should work.

      Hope this helps,

      Jeff Eggen
      IT Programmer Analyst
      Saskatchewan Government Insurance
      Ph (306) 751-1795
      email jeggen@...
    • Nathan.Jeffrey@dhs.vic.gov.au
      The problem is in this bit (edited for brevity)... create_file( %rec); sub create_file { my ($c, $o); my %new_rec = shift; foreach $c (keys %new_rec) {} }
      Message 2 of 2 , Sep 4, 2002
      • 0 Attachment
        The problem is in this bit (edited for brevity)...

        create_file(\%rec);

        sub create_file
        {

        my ($c, $o);
        my %new_rec = shift;

        foreach $c (keys %new_rec) {}
        }

        You're passing a reference (\%rec) to a sub. References are scalar. So,
        in order to read it, you have to dereference it. Like so.

        sub create_file {
        my $rec_ref = shift;
        foreach $c (keys %$rec_ref) {}
        }

        Hope this helps!

        N






        "b2174ml" <michael.s.eberhart@...>
        05/09/2002 04:16 AM
        Please respond to perl-beginner


        To: perl-beginner@yahoogroups.com
        cc:
        Subject: [PBML] Passing a hash to a sub


        I have the following code, and it fails. How do you pass a hash to a
        sub so that you can access its keys and values?

        -------------------------------------------------------------------
        use strict;
        use DBI;

        my (%rec);
        my ($dbh, $st, $records, $cust, $ord, $not, $state);

        $dbh = DBI->connect ("dbi:Oracle:$ORACLE_SID", "$ORACLE_USERID");

        $query = "SELECT customer, order_num, notifier, state FROM orders";

        $sth = $dbh->prepare("$query");
        $sth->execute();
        $records = $sth->fetchall_arrayref();


        foreach my $r (@$records) {
        my($cust, $ord, $not, $state) = @$r;
        $rec{$cust}{$ord}{notifier} = $not;
        $rec{$cust}{$ord}{state} = $state;
        }

        create_file(\%rec);

        sub create_file
        {

        my ($c, $o);
        my %new_rec = shift;

        foreach $c (keys %new_rec) {
        foreach $o (keys %{$new_rec{$c}}) {
        print "$c|$o|$new_rec{$c}{$o}{notifier}\n";
        }
        }
        }

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

        Why does the sub fail?
        I'd appreciate any help.

        Thanks
        Mike




        Unsubscribing info is here:
        http://help.yahoo.com/help/us/groups/groups-32.html

        Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/







        _________________________________________________________________________________

        This email contains confidential information intended only for the person named above and may be subject to legal privilege. If you are not the intended recipient, any disclosure, copying or use of this information is prohibited. The Department provides no guarantee that this communication is free of virus or that it has not been intercepted or interfered with. If you have received this email in error or have any other concerns regarding its transmission, please notify Postmaster@...
        _________________________________________________________________________________
      Your message has been successfully submitted and would be delivered to recipients shortly.