Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Problem with printing hashes using dynamic keys

Reply
Thread Tools

Problem with printing hashes using dynamic keys

 
 
Palaniappan
Guest
Posts: n/a
 
      10-27-2003
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
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      10-27-2003
Palaniappan <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      10-27-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Palaniappan) writes:

> 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\\
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-27-2003
Palaniappan <(E-Mail Removed)> wrote:

> 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";
---------------------------------


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Merging hashes using both symbols and strings as keys shenry Ruby 14 11-03-2009 02:43 PM
On Hashes - How the hashes printing works? Neela megha shyam Chivukula Ruby 4 05-28-2009 10:56 AM
using hashes as keys in hashes Steven Arnold Ruby 3 11-23-2005 03:25 PM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Hashes of Hashes via subs Ben Holness Perl 8 10-08-2003 06:57 AM



Advertisments