Problem with printing hashes using dynamic keys

Discussion in 'Perl Misc' started by Palaniappan, Oct 27, 2003.

  1. Palaniappan

    Palaniappan Guest

    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
    Palaniappan, Oct 27, 2003
    #1
    1. Advertising

  2. Palaniappan

    Anno Siegel Guest

    Palaniappan <> 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
    Anno Siegel, Oct 27, 2003
    #2
    1. Advertising

  3. (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\\
    Brian McCauley, Oct 27, 2003
    #3
  4. Palaniappan <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 27, 2003
    #4
    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. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    559
    Ben Holness
    Oct 8, 2003
  2. Steven Arnold

    using hashes as keys in hashes

    Steven Arnold, Nov 23, 2005, in forum: Ruby
    Replies:
    3
    Views:
    159
    Mauricio Fernández
    Nov 23, 2005
  3. Neela megha shyam Chivukula

    On Hashes - How the hashes printing works?

    Neela megha shyam Chivukula, May 27, 2009, in forum: Ruby
    Replies:
    4
    Views:
    234
    Markus Schirp
    May 28, 2009
  4. shenry
    Replies:
    14
    Views:
    232
    Rick DeNatale
    Nov 3, 2009
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    208
Loading...

Share This Page