Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

using hash to compare with string and print the string

by Newbie95 (Novice)
on May 15, 2019 at 14:13 UTC ( #11100018=perlquestion: print w/replies, xml ) Need Help??

Newbie95 has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone. I'm still learning on how to use hash in perl. May I know if i can use the keys inside hash to match with string in another array? The reason being is because I wanted to print the hash values once the hash keys is matched with the array.

Here is example of my code:

#!usr/bin/perl use warnings; use stricts; my @array = b, d, a, c; my %hash = ( "a" => "200", "b" => "100", "c" => "30", "d" => "40",); foreach my $key (keys %hash){ push (@key, $key);} foreach my $value (values %hash){ push (@value, $value);} foreach my $t (0 .. $#array) { if ($array[$t] eq $key[$t]) { print "array element: $array[$t] = $value[$t] inside \%hash\n" +; } else { next } }

The result will be:

array element: a = 200 inside %hash

array element: b = 100 inside %hash

array element: c = 30 inside %hash

array element: d = 40 inside %hash

But my code above doesn't print anything. Is it possible to keep on looping array until it print all the element that is matched with the keys inside hash?

Any suggestions are highly appreciated. Thank you in advanced.

Replies are listed 'Best First'.
Re: using hash to compare with string and print the string
by holli (Monsignor) on May 15, 2019 at 14:32 UTC
    You don't need to create any intermediate arrays or for loops. Just ask the hash wether it contains the key you want
    foreach my $key (@array) { if (exists $hash{$key} ) { print "array element: $key = $hash{$key} inside \%hash\n"; } else { next } }


    holli

    You can lead your users to water, but alas, you cannot drown them.
Re: using hash to compare with string and print the string
by hippo (Canon) on May 15, 2019 at 14:35 UTC
    But my code above doesn't print anything.

    It's worse than that - your code doesn't compile:

    $ perl -cw 11100018.pl Can't locate stricts.pm in @INC (you may need to install the stricts m +odule) (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 +/usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/ +perl5 /usr/share/perl5 .) at 11100018.pl line 3. BEGIN failed--compilation aborted at 11100018.pl line 3.

    Here's something which is what I think you might want instead.

    #!/usr/bin/env perl use strict; use warnings; my @array = qw/b d a c/; my %hash = ( a => 200, b => 100, c => 30, d => 40 ); for my $key (sort @array) { if ($hash{$key}) { print "array element: $key = $hash{$key} inside \%hash\n"; } }
Re: using hash to compare with string and print the string
by thanos1983 (Parson) on May 16, 2019 at 08:59 UTC

    Hello Newbie95,

    Just a minor note here because you said you are doing your first steps on hashes.

    I noticed that you have an array of keys and a hash below. If the hash is not manually defined you can use hash slicing and assign the keys and values in one step to avoid typing everything manually. For example see below:

    my @keys = qw(b d a c); my @values = qw(200 100 30 40); my %hash; @hash{@keys} = @values; print Dumper \%hash;

    Then simply as fellow Monks demonstrate to you, iterate over the keys and if the key exists print.

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @keys = qw(b d a c); my @values = qw(200 100 30 40); my %hash; @hash{@keys} = @values; print Dumper \%hash; foreach my $key (@keys) { if (exists $hash{$key} ) { print "array element: $key = $hash{$key} inside \%hash\n"; } next; } __END__ $ perl test.pl $VAR1 = { 'a' => '30', 'c' => '40', 'b' => '200', 'd' => '100' }; array element: b = 200 inside %hash array element: d = 100 inside %hash array element: a = 30 inside %hash array element: c = 40 inside %hash

    Here is the documentation regarding perldata/Slices. In case you want to read a bit more on how to play with them :).

    BR / Thanos

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      I wanted to comment on style here. Notice that hippo writes
      my @array = qw/b d a c/; my %hash = ( a => 200, b => 100, c => 30, d => 40 );
      while Thanos offers
      my @keys = qw(b d a c); my @values = qw(200 100 30 40); my %hash; @hash{@keys} = @values;
      I didn't know this trick with hash slicing, and I appreciate the pointer. It is very helpful to learn new things.

      Stylistically, the latter is smart, the former is pleasant. If I can invoke Henry Koster's _Harvey_, "'In this world, Elwood, you must be oh so smart or oh so pleasant.' Well, for years I was smart. I recommend pleasant. You may quote me." These two aren't identical, I think. Note $hash{'a'} is different in the two samples (30 and 200). There are times when clever is better (more concise, more elegant, more insightful, golfing) and there are times when obvious is better. If I'm just writing for myself, I lean toward clever. But when I'm trying to get something done, or writing for myself in six months, I'm with Elwood.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11100018]
Approved by holli
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2019-06-26 00:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Is there a future for codeless software?



    Results (108 votes). Check out past polls.

    Notices?