For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > May 2007 > How to print hash keys and values









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to print hash keys and values
moonhk

2007-05-16, 9:58 pm

How to print hash keys and values
Also, I want to know how to get the keys and values in coding.

Expect result:
moon 6
sun 1



#!/usr/bin/perl -w
# 2007/05/16
#

sub printme (%)
{
my ($a, $b) = @_;
print $a ,"\n";
}

%unique = (); # clear hash
$unique{"sun"}++;
$unique{"moon"}++;
$unique{"moon"}+=5;
$, = " "; # Prints a ?? in between elements
print %unique;
print "\n";

print "List of keys\n";
print "============\n";
foreach ( %unique) {
printme($_);

}

Paul Lalli

2007-05-17, 7:58 am

On May 16, 9:46 pm, moonhk <moon_ils...@yahoo.com.hk> wrote:
> How to print hash keys and values
> Also, I want to know how to get the keys and values in coding.


perldoc -f keys
perldoc -f values
perldoc -f each

Paul Lalli

nobull67@gmail.com

2007-05-17, 7:58 am

On May 17, 2:46 am, moonhk <moon_ils...@yahoo.com.hk> wrote:
> How to print hash keys and values
> Also, I want to know how to get the keys and values in coding.


That is a SAQ! keys() and values().

> Expect result:
> moon 6
> sun 1
>
> #!/usr/bin/perl -w
> # 2007/05/16
> #


You forgot:

use strict;
use warnings;

>
> sub printme (%)


What do you think that prototype is doing? Almost certainly not what
you think! I suggest you remove it.

> {
> my ($a, $b) = @_;


The variables $a and $b have special meaning in Perl so it is
suggested that you avoid using them as general purpose variables.

> print $a ,"\n";


You never used $b.
> }
>
> %unique = (); # clear hash


Comments should describe _why_ you do something. They should not
simply restate what the code says.

Anyhow, if you need to explicitly clear a variable this is usually a
sign that you've failed to declare the variable in with the right
scope. In this case I'm fairly sure that this is the case.

my %unique;

> $unique{"sun"}++;
> $unique{"moon"}++;
> $unique{"moon"}+=5;
> $, = " "; # Prints a ?? in between elements
> print %unique;


I suggest that you should only ever alter $, in a verly limited scope
(if at all).

{
local $, = " ";
print %unique;
}

> print "\n";
>
> print "List of keys\n";
> print "============\n";
> foreach ( %unique) {


That lists both the keys and the values interleaved.

To get just the keys use the keys() function.


moonhk

2007-05-17, 9:59 pm

On 5=A4=EB17=A4=E9, =A4U=A4=C88=AE=C938=A4=C0, "nobul...@gmail.com" <nobul.=
..=2E@gmail.com> wrote:
> On May 17, 2:46 am, moonhk <moon_ils...@yahoo.com.hk> wrote:
>
>
> That is a SAQ! keys() and values().
>
>
>
> You forgot:
>
> use strict;
> use warnings;
>
>
>
>
> What do you think that prototype is doing? Almost certainly not what
> you think! I suggest you remove it.
>
>
> The variables $a and $b have special meaning in Perl so it is
> suggested that you avoid using them as general purpose variables.
>
>
> You never used $b.
>
>
>
> Comments should describe _why_ you do something. They should not
> simply restate what the code says.
>
> Anyhow, if you need to explicitly clear a variable this is usually a
> sign that you've failed to declare the variable in with the right
> scope. In this case I'm fairly sure that this is the case.
>
> my %unique;
>
>
> I suggest that you should only ever alter $, in a verly limited scope
> (if at all).
>
> {
> local $, =3D " ";
> print %unique;
>
> }
>
>
> That lists both the keys and the values interleaved.
>
> To get just the keys use the keys() function.


Just update my perl script. the result is ok

moon 6 sun 1
List of keys
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
moon =3D> value is 6
sun =3D> value is 1


#!/usr/bin/perl -w
# 2007/05/16
#
use strict;
use warnings;

my %unique =3D (); # clear hash
my $key;
$unique{"sun"}++;
$unique{"moon"}++;
$unique{"moon"}+=3D5;
local $, =3D " "; # Prints a ?? in between elements
print %unique;
print "\n";

print "List of keys\n";
print "=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\n";
foreach $key (keys %unique) {
print "$key =3D> value is $unique{$key}\n";
}

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com