HASH

Discussion in 'Perl Misc' started by Winston, Dec 6, 2006.

  1. Winston

    Winston Guest

    I need to *MAKE* some subrutines for search inside a hash.
    I wonder what is the best for this.

    For example, I have a hash with names and ages. And I need to make some
    searches using regular expressions...

    sub a_option {
    %hash = @_;
    print "Letter? ";
    chomp($letter = <STDIN>);
    @names = keys(%hash);
    $found=0;
    foreach $name (@names) {
    if ($name =~ /^$letter/) {
    print "Name: $name\t\t Age:$hash{$name}";
    $encontrado++;
    }
    }
    }

    And the same for b_option, c_option, etc...
    Is this ok? Or is a little bit ridiculous?

    Is correct to do: %hash=@_ ?

    These are my first steps in PERL. Is this the correct group?
    Don't reply with a 1-line-perl-code that make all I need!
    Thanks!
     
    Winston, Dec 6, 2006
    #1
    1. Advertising

  2. Winston

    John Bokma Guest

    Winston <> wrote:

    > I need to *MAKE* some subrutines for search inside a hash.
    > I wonder what is the best for this.
    >
    > For example, I have a hash with names and ages. And I need to make some
    > searches using regular expressions...
    >
    > sub a_option {
    > %hash = @_;
    > print "Letter? ";
    > chomp($letter = <STDIN>);
    > @names = keys(%hash);
    > $found=0;
    > foreach $name (@names) {
    > if ($name =~ /^$letter/) {
    > print "Name: $name\t\t Age:$hash{$name}";
    > $encontrado++;
    > }
    > }
    > }
    >
    > And the same for b_option, c_option, etc...
    > Is this ok? Or is a little bit ridiculous?
    >
    > Is correct to do: %hash=@_ ?
    >
    > These are my first steps in PERL.


    The language is called Perl, not PERL (Nor is it really an acronym).

    > Is this the correct group?


    Yes, if you take the time to read the posting guidelines.


    > Don't reply with a 1-line-perl-code that make all I need!
    > Thanks!


    Ok, put at the very start of your program:

    use strict;
    use warnings;

    Furthermore: name your subs right. I doubt if a_option is clear to you in
    a week or two.

    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
     
    John Bokma, Dec 6, 2006
    #2
    1. Advertising

  3. Winston

    gf Guest

    Winston wrote:
    > I need to *MAKE* some subrutines for search inside a hash.
    > I wonder what is the best for this.
    >
    > For example, I have a hash with names and ages. And I need to make some
    > searches using regular expressions...
    >
    > sub a_option {
    > %hash = @_;
    > print "Letter? ";
    > chomp($letter = <STDIN>);
    > @names = keys(%hash);
    > $found=0;
    > foreach $name (@names) {
    > if ($name =~ /^$letter/) {
    > print "Name: $name\t\t Age:$hash{$name}";
    > $encontrado++;
    > }
    > }
    > }
    >
    > And the same for b_option, c_option, etc...
    > Is this ok? Or is a little bit ridiculous?


    Not ridiculous, just going about it the hard way.

    >
    > Is correct to do: %hash=@_ ?


    I wouldn't because it won't scale well... actually, it'll scale very
    badly.

    OK Grasshopper, grab your Camel book and prepare to do some lookin' up.
    I won't write it for you, but I will point in the directions I'd go.

    Pass the hash as a reference for speed...

    ....a_option(\%hash_to_scan);
    ....
    sub a_option {

    # make sure you got a hash-ref and it contained something. This is
    called defensive programming and is good for speed and robustness.
    my $href = shift or return;
    my %hash = %{$href};
    scalar(keys(%hash)) or return;
    ....

    # You don't wanna go this way...
    # @names = keys(%hash);
    # $found=0;

    # prebuild your regexp for speed. Speed is good.
    my $pattern = qr/^$letter/;
    # filter all the keys that match...
    foreach my $name (grep {$pattern} keys %hash) {
    ....
    }
    >
    > These are my first steps in PERL. Is this the correct group?
    > Don't reply with a 1-line-perl-code that make all I need!
    > Thanks!
     
    gf, Dec 6, 2006
    #3
  4. gf wrote:
    > Winston wrote:
    >
    >>Is correct to do: %hash=@_ ?

    >
    >
    > I wouldn't because it won't scale well... actually, it'll scale very
    > badly.
    >
    > OK Grasshopper, grab your Camel book and prepare to do some lookin' up.
    > I won't write it for you, but I will point in the directions I'd go.
    >
    > Pass the hash as a reference for speed...


    You want speed...

    >
    > ...a_option(\%hash_to_scan);
    > ...
    > sub a_option {
    >
    > # make sure you got a hash-ref and it contained something. This is
    > called defensive programming and is good for speed and robustness.
    > my $href = shift or return;
    > my %hash = %{$href};


    and then immediately dereference. Not much different than passing the hash.
     
    Brian Helterline, Dec 6, 2006
    #4
  5. Winston

    Dr.Ruud Guest

    Brian Helterline schreef:
    > gf:


    >> Pass the hash as a reference for speed...

    >
    > You want speed... [...]
    >
    >> my %hash = %{$href};

    >
    > and then immediately dereference. Not much different than
    > passing the hash.


    AFAIK, passing the hash itself, actually passes a flattened copy. So
    there is a big difference.

    $ perl -wle '%x=(A=>1,B=>2,C=>3);sub x{%_=@_;$_{'A'}=-9};x(%x);print %x'
    A1C3B2

    $ perl -wle '%x=(A=>1,B=>2,C=>3);sub x{$_[0]->{'A'}=-9};x(\%x);print %x'
    A-9C3B2

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Dec 7, 2006
    #5
  6. Winston

    Ben Morrow Guest

    Quoth "Dr.Ruud" <>:
    > Brian Helterline schreef:
    > > gf:

    >
    > >> Pass the hash as a reference for speed...

    > >
    > > You want speed... [...]
    > >
    > >> my %hash = %{$href};

    > >
    > > and then immediately dereference. Not much different than
    > > passing the hash.

    >
    > AFAIK, passing the hash itself, actually passes a flattened copy.


    The point is that %$href also returns a flattened copy, so 'gf' was
    copying the hash in any case.

    Ben

    --
    We do not stop playing because we grow old;
    we grow old because we stop playing.
     
    Ben Morrow, Dec 7, 2006
    #6
  7. Winston

    gf Guest

    Brian Helterline wrote:

    > and then immediately dereference. Not much different than passing the hash.


    Unless the hash is really big, then you've got Perl shoving all the
    contents of the hash into the sub as a humongeous array instead of a
    single hash-ref value. Do that in a loop with a lot of data, and your
    program slows down.

    To keep the speed up sometimes I'll keep the hash-ref and just access
    the keys using the -> operator, but I find it to be visually noisy, so
    when speed isn't an issue I'll dereference into a local hash and go
    from there. At least it's an informed decision, one I make again and
    again... change, back out the change... benchmark... change again....
     
    gf, Dec 7, 2006
    #7
  8. Brian Helterline <> wrote:
    > gf wrote:
    >> Pass the hash as a reference for speed...

    > You want speed...


    >> ...a_option(\%hash_to_scan);
    >> ...
    >> sub a_option {
    >>
    >> # make sure you got a hash-ref and it contained something. This is
    >> called defensive programming and is good for speed and robustness.
    >> my $href = shift or return;
    >> my %hash = %{$href};


    > and then immediately dereference. Not much different than passing the
    > hash.


    It seems completely different to me. The reference is a constant
    penalty to dereference. Otherwise, it's a penalty to copy the data. So
    as the contents of the hash increase in size, the cost to copy
    increases.

    If the hash is sizeable (megabytes), the you're saving time and memory.
    If the hash will always be tiny, then you're probably not saving
    anything.

    --
    Darren Dunham
    Senior Technical Consultant TAOS http://www.taos.com/
    Got some Dr Pepper? San Francisco, CA bay area
    < This line left intentionally blank to confuse you. >
     
    Darren Dunham, Dec 8, 2006
    #8
  9. Winston

    Dr.Ruud Guest

    Ben Morrow schreef:
    > Dr.Ruud:
    >> Brian Helterline:
    >>> gf:


    >>>> Pass the hash as a reference for speed...
    >>>
    >>> You want speed... [...]
    >>>
    >>>> my %hash = %{$href};
    >>>
    >>> and then immediately dereference. Not much different than
    >>> passing the hash.

    >>
    >> AFAIK, passing the hash itself, actually passes a flattened copy.

    >
    > The point is that %$href also returns a flattened copy, so 'gf' was
    > copying the hash in any case.


    Ah yes, I concentrated on the "dereferencing" and missed that gf built a
    hash with it.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Dec 8, 2006
    #9
    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. Red Orchid
    Replies:
    3
    Views:
    1,050
  2. Pieter Claassen
    Replies:
    1
    Views:
    1,120
    CBFalconer
    Aug 4, 2004
  3. Bo Peng
    Replies:
    4
    Views:
    792
  4. rp
    Replies:
    1
    Views:
    540
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    627
    David A. Black
    Jul 2, 2008
Loading...

Share This Page