Problem with printing hashes using dynamic keys

P

Palaniappan

Hi,
I am a newbie, sorry if my doubt is very basic..

i am having a hash %abc with keys as

$abc{'a0'}, $abc{'a1'}, .... $abc{'a9'}

and i wrote a for loop to print all 10 values..

for($i=0;$i<10;$i++)
{ print "$i - $abc{"a$i"} "; }

i got the problem of using "" in between the string...

how to solve the problem without using extra variable?

-palam
 
A

Anno Siegel

Palaniappan said:
Hi,
I am a newbie, sorry if my doubt is very basic..

i am having a hash %abc with keys as

$abc{'a0'}, $abc{'a1'}, .... $abc{'a9'}

Simple strings in hash braces don't have to be quoted. You could write
that

$abc{a0}, $abc{a1}, .... $abc{a9}
and i wrote a for loop to print all 10 values..

for($i=0;$i<10;$i++)

Better written as

for my $i ( 0 .. 9 )

Use lexical variables per default.
{ print "$i - $abc{"a$i"} "; }

i got the problem of using "" in between the string...

Use concatenation instead. (Perl uses it anyhow, string interpolation
is just concatenation in disguise.)

print "$i - $abc{'a' . $i} ";

Anno
 
B

Brian McCauley

and i wrote a for loop to print all 10 values..

for($i=0;$i<10;$i++)
{ print "$i - $abc{"a$i"} "; }

i got the problem of using "" in between the string...

how to solve the problem without using extra variable?

Use qq() so that you can choose some character other than "" for
delimiting one of your interpolated strings.

Or use backslash to protect the inner "".

Or use explicit concatenation rather than interpolation.

I consider the use of backslash in this context totally
counter-intuative. To understand why it work at all see: Gory details
of parsing quoted constructs.

Note: use of C-style for() in Perl where it is not needed is generally
considered un-Perl-ish.

Note: you should always delcare all varaibles as lexically scoped in
the smallest applicable scope unless you have a reson not to. This is
not perculliar to Perl - it applies in all languages with the concept
of lexically scoped variables exists.

for my $i ( 0 .. 9 ) {
print qq'$i - $abc{"a$i"}\n';
print "$i - $abc{qq{a$i}}\n";
print "$i - $abc{'a'.$i}\n";
print "$i - $abc{\"a$i\"}\n";
}

Note: The fact that you want to do this in the first place may mean
you are using the wrong structure. A single element in the hash
holding an arrayref could well be the more natural reprecentation of
your data.

for my $i ( 0 .. $#{$abc{a}} ) {
print "$i - $abc{a}[$i]\n";
}

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
T

Tad McClellan

Palaniappan said:
I am a newbie,


Please see the Posting Guidelines that are posted here frequently.
It will help you get answers to your Perl questions.

sorry if my doubt is very basic..


There is no need to apologize for basic questions.

(There _is_ a need to apologize for questions answered in the
std docs though, but you didn't do that.)

i am having a hash %abc with keys as

$abc{'a0'}, $abc{'a1'}, .... $abc{'a9'}


Sequentially numbered hash keys may indicate that you've chosen
the wrong data structure. An array is often better in such cases...

and i wrote a for loop to print all 10 values..

for($i=0;$i<10;$i++)


That is an error-prone way of indexing, see below for a Better Way.

Space characters are not a scarse resource, feel free to use as
many of them as you need to make your code easier to read and understand.

for( $i=0; $i<10; $i++ )

{ print "$i - $abc{"a$i"} "; }

i got the problem of using "" in between the string...


Here are 4 ways doing what you want, and one way that suggests
you try to want something else :)

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

my %abc = qw/ a0 zero a1 one a2 two a3 three a4 four a5 five a6 six
a7 seven a8 eight a9 nine /;

foreach my $i ( 0 .. 9 ) {
print "$i - $abc{'a' . $i}\n"; # concatenation
print "$i - ", $abc{"a$i"}, "\n"; # list of args to print()
print qq/$i - $abc{"a$i"}\n/; # alternate delimiters
print "$i - $abc{ qq/a$i/ }\n"; # other alternate delimiters

}
print "-----\n";


foreach my $i ( 'a0' .. 'a9' ) { # range using magic auto-increment
print "$i - $abc{$i}\n";
}
print "-----\n";
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top