hashes question

Discussion in 'Perl Misc' started by ed, Sep 20, 2006.

  1. ed

    ed Guest

    I'm having a bit of trouble sending hash elements to sub routines and
    back.

    Any help appreciated!

    sub entry {
    my $l = shift;
    my %h = %_;

    while( ( my $k, my $v ) = each ( %h ) ) {
    print( "K: $k V: $v\n" );
    }
    return(%h);
    }

    $h{'stuff'} = "hello";
    %h = entry( "1", \%h );

    When run %h becomes empty in entry.

    --
    Regards, Ed :: http://www.s5h.net
    proud unix hacker
    Mr. T cannot be pitied. Mr. T is most often envied, admired or
    feared. Once, Mr. T was even ignored. That fool has since been
    nothing but pitied.
    ed, Sep 20, 2006
    #1
    1. Advertising

  2. ed wrote:
    > I'm having a bit of trouble sending hash elements to sub routines and
    > back.
    >
    > Any help appreciated!
    >
    > sub entry {
    > my $l = shift;
    > my %h = %_;
    >
    > while( ( my $k, my $v ) = each ( %h ) ) {
    > print( "K: $k V: $v\n" );
    > }
    > return(%h);
    > }
    >
    > $h{'stuff'} = "hello";
    > %h = entry( "1", \%h );
    >
    > When run %h becomes empty in entry.


    You are calling entry() with a hash reference so you have to dereference it
    inside the sub:

    sub entry {
    my $l = shift;
    my $h = shift;

    while( my ( $k, $v ) = each ( %$h ) ) {
    print( "K: $k V: $v\n" );
    }
    return %h;
    }




    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Sep 20, 2006
    #2
    1. Advertising

  3. ed

    David Squire Guest

    ed wrote:
    > I'm having a bit of trouble sending hash elements to sub routines and
    > back.
    >
    > Any help appreciated!
    >
    > sub entry {
    > my $l = shift;
    > my %h = %_;


    Hmmm. Is there such a variable s %_? It doesn't appear in perldoc perlvar.

    >
    > while( ( my $k, my $v ) = each ( %h ) ) {
    > print( "K: $k V: $v\n" );
    > }
    > return(%h);
    > }
    >
    > $h{'stuff'} = "hello";
    > %h = entry( "1", \%h );


    here you pass a *reference* to the hash %h. If you pass a reference
    (which is a sensible thing to do for complex datastructures), you need
    to treat it as a reference in the subroutine. Also, there is no need to
    assign the subroutine return value to the hash, since if you pass a
    reference, the subroutine will use the same hash (though yours does not
    modify it, so you don't need to return anything.

    For example:

    ----

    #!/usr/bin/perl

    use strict;
    use warnings;

    sub entry {
    my $l = shift;
    my $h_ref = shift;

    while( ( my $k, my $v ) = each ( %$h_ref ) ) {
    print( "K: $k V: $v\n" );
    }
    # Let's modify the hash while we're here
    $$h_ref{'more stuff'} = 'nonsense';
    }

    my %h;
    $h{'stuff'} = "hello";
    entry( "1", \%h );

    foreach my $key (keys %h) {
    print "$key: $h{$key}\n";
    }

    ----

    Output:

    K: stuff V: hello
    stuff: hello
    more stuff: nonsense

    ----


    DS
    David Squire, Sep 20, 2006
    #3
  4. David Squire <> wrote:
    > ed wrote:


    >> my %h = %_;

    >
    > Hmmm. Is there such a variable s %_?



    Yes there is. (but it doesn't do anything for the OP.)

    There is a $_ variable, so then there is also @_ and %_ (and
    a few more) variables.


    eg: Since there is a $@ variable, this works fine, even with strictures:

    -----------------
    #!/usr/bin/perl
    use warnings;
    use strict;

    @@ = qw/foo bar/;
    print "$_\n" for @@;

    %@ = qw/foo FOO bar BAR/;
    print "$_ => $@{$_}\n" for keys %@;
    -----------------


    > It doesn't appear in perldoc perlvar.



    Then it doesn't do anything special.

    (but that does not mean that you cannot use it.)


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 21, 2006
    #4
  5. ed

    Ch Lamprecht Guest

    Tad McClellan wrote:
    > David Squire <> wrote:
    >
    >>ed wrote:

    >
    >
    >>> my %h = %_;

    >>
    >>Hmmm. Is there such a variable s %_?

    >
    >
    >
    > Yes there is. (but it doesn't do anything for the OP.)
    >
    > There is a $_ variable, so then there is also @_ and %_ (and
    > a few more) variables.
    >
    >
    > eg: Since there is a $@ variable, this works fine, even with strictures:

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^


    perlvar:

    Perl identifiers that begin with digits, control characters, or punctuation
    characters are exempt from the effects of the package declaration and are always
    forced to be in package main; they are also exempt from strict 'vars' errors.


    >
    > -----------------
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    >
    > @@ = qw/foo bar/;
    > print "$_\n" for @@;
    >
    > %@ = qw/foo FOO bar BAR/;
    > print "$_ => $@{$_}\n" for keys %@;
    > -----------------
    >
    >


    so this will work with any variable name starting with a control character:

    use warnings;
    use strict;

    @? = qw/foo bar/;
    print "$_\n" for @?;

    %? = qw/foo FOO bar BAR/;
    print "$_ => $?{$_}\n" for keys (%?);



    Christoph

    --

    perl -e "print scalar reverse q//"
    Ch Lamprecht, Sep 21, 2006
    #5
  6. ed

    Dr.Ruud Guest

    Ch Lamprecht schreef:

    > so this will work with any variable name starting with a
    > control character:
    >
    > use warnings;
    > use strict;
    >
    > @? = qw/foo bar/;
    > print "$_\n" for @?;
    >
    > %? = qw/foo FOO bar BAR/;
    > print "$_ => $?{$_}\n" for keys (%?);


    I see no "variable name starting with a
    control character" in your example.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Sep 21, 2006
    #6
  7. ed

    Ch Lamprecht Guest

    Dr.Ruud wrote:
    > Ch Lamprecht schreef:
    >
    >
    >>so this will work with any variable name starting with a
    >>control character:
    >>
    >>use warnings;
    >>use strict;
    >>
    >>@? = qw/foo bar/;
    >>print "$_\n" for @?;
    >>
    >>%? = qw/foo FOO bar BAR/;
    >>print "$_ => $?{$_}\n" for keys (%?);

    >
    >
    > I see no "variable name starting with a
    > control character" in your example.
    >


    Of course you are right.

    s/control/punctuation/


    --

    perl -e "print scalar reverse q//"
    Ch Lamprecht, Sep 21, 2006
    #7
  8. ed

    ed Guest

    On Wed, 20 Sep 2006 22:31:53 GMT
    "John W. Krahn" <> wrote:

    > ed wrote:
    > > I'm having a bit of trouble sending hash elements to sub routines
    > > and back.
    > >
    > > Any help appreciated!
    > >
    > > sub entry {
    > > my $l = shift;
    > > my %h = %_;
    > >
    > > while( ( my $k, my $v ) = each ( %h ) ) {
    > > print( "K: $k V: $v\n" );
    > > }
    > > return(%h);
    > > }
    > >
    > > $h{'stuff'} = "hello";
    > > %h = entry( "1", \%h );
    > >
    > > When run %h becomes empty in entry.

    >
    > You are calling entry() with a hash reference so you have to
    > dereference it inside the sub:
    >
    > sub entry {
    > my $l = shift;
    > my $h = shift;
    >
    > while( my ( $k, $v ) = each ( %$h ) ) {
    > print( "K: $k V: $v\n" );
    > }
    > return %h;
    > }



    Thank you all very much, Abigail, David and Tad. Problem solved. This
    was a very difficult problem for me, I would never have solved it alone.


    --
    Regards, Ed :: http://www.linuxwarez.co.uk
    proud bash person
    When Chuck Norris roundhouse kicks people, they do not die of blunt
    trauma or tissue damage. They simple lose their will to live.
    ed, Sep 21, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    560
    Ben Holness
    Oct 8, 2003
  2. Steven Arnold

    using hashes as keys in hashes

    Steven Arnold, Nov 23, 2005, in forum: Ruby
    Replies:
    3
    Views:
    160
    Mauricio Fernández
    Nov 23, 2005
  3. kazaam
    Replies:
    12
    Views:
    271
    Matthias Wächter
    Sep 13, 2007
  4. djacober

    Question about hashes of hashes

    djacober, Jul 24, 2005, in forum: Perl Misc
    Replies:
    10
    Views:
    182
    Ian Wilson
    Jul 25, 2005
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    210
Loading...

Share This Page