newbie question. how to assign an array to a hash table?

Discussion in 'Perl Misc' started by Guest1, Sep 3, 2003.

  1. Guest1

    Guest1 Guest

    Hi,

    I have something like:

    $value = 'A' 'C' 'G';

    and I would like store this value on a hash table.

    my @value = split(' ',$value);
    print "=>\t@value[1]\n"; # print: => C;
    ($splitted_hap{$key}) = @value;

    while trying to print the values:

    print "\n";

    while ( (my $key, my @value) = each(%splitted_hap) ) {
    print "$key\t$value[0]\n";
    print "$key\t$value[1]\n";
    }


    I get:


    first A
    first
    second C
    second
    third A
    third

    why the values are not been stored on value[1]

    what I am doing wrong?


    Thanks for your help
    Pedro
    Guest1, Sep 3, 2003
    #1
    1. Advertising

  2. Guest1

    Anno Siegel Guest

    Guest1 <> wrote in comp.lang.perl.misc:
    >
    > Hi,
    >
    > I have something like:
    >
    > $value = 'A' 'C' 'G';


    This doesn't compile. It can't be your real code.

    > and I would like store this value on a hash table.
    >
    > my @value = split(' ',$value);
    > print "=>\t@value[1]\n"; # print: => C;
    > ($splitted_hap{$key}) = @value;
    >
    > while trying to print the values:
    >
    > print "\n";
    >
    > while ( (my $key, my @value) = each(%splitted_hap) ) {
    > print "$key\t$value[0]\n";
    > print "$key\t$value[1]\n";
    > }
    >
    >
    > I get:
    >
    >
    > first A


    No you don't get that, not with the code you posted. Even ignoring
    the invalid syntax in the first line (and who knows what else), the
    string "first" appears nowhere in your program. Please show your
    real code.

    [...]

    Anno
    Anno Siegel, Sep 3, 2003
    #2
    1. Advertising

  3. Guest1 <> wrote:

    > I have something like:



    "something like" is not good enough for us to be able to help you.

    We need _real Perl code_ in order to determine what is wrong with the code.


    > $value = 'A' 'C' 'G';



    That is not Perl code.

    There are many modules for processing DNA sequences, have you
    seen if any of those wheels will roll your cart?


    > and I would like store this value on a hash table.
    >
    > my @value = split(' ',$value);
    > print "=>\t@value[1]\n"; # print: => C;

    ^
    ^

    You should always enable warnings when developing Perl code!


    > ($splitted_hap{$key}) = @value;



    This will store only the 'A' in the hash.

    Don't you want 'C' and 'G' in there somewhere too?

    You will need to understand references if you want to do that.


    > while trying to print the values:
    >
    > print "\n";
    >
    > while ( (my $key, my @value) = each(%splitted_hap) ) {



    The hash values are not arrays, @value can never have more
    than one element in it...


    > print "$key\t$value[0]\n";
    > print "$key\t$value[1]\n";



    .... so $value[1] will never be defined.


    > }
    >
    >
    > I get:
    >
    >
    > first A
    > first



    Your program cannot possibly make that output.

    Many people will not volunteer to help you if you lie to them...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 3, 2003
    #3
  4. Guest1

    Guest1 Guest

    thanks guys for your help.

    The fixed code is:

    my %hap =(
    "first" => 'a c g t',
    "second" => 'c g ? t',
    "third" => 'a g t a',
    );

    foreach my $key (keys %hap) {
    my @values = split(' ',$hap{$key});
    $splitted_hap{$key} = \@values;
    }

    now works fine!

    I didn't know how to pass the splitted String into the array.

    Thanks
    Pedro


    On Wed, 3 Sep 2003, Tad McClellan wrote:

    > Guest1 <> wrote:
    >
    > > I have something like:

    >
    >
    > "something like" is not good enough for us to be able to help you.
    >
    > We need _real Perl code_ in order to determine what is wrong with the code.
    >
    >
    > > $value = 'A' 'C' 'G';

    >
    >
    > That is not Perl code.
    >
    > There are many modules for processing DNA sequences, have you
    > seen if any of those wheels will roll your cart?
    >
    >
    > > and I would like store this value on a hash table.
    > >
    > > my @value = split(' ',$value);
    > > print "=>\t@value[1]\n"; # print: => C;

    > ^
    > ^
    >
    > You should always enable warnings when developing Perl code!
    >
    >
    > > ($splitted_hap{$key}) = @value;

    >
    >
    > This will store only the 'A' in the hash.
    >
    > Don't you want 'C' and 'G' in there somewhere too?
    >
    > You will need to understand references if you want to do that.
    >
    >
    > > while trying to print the values:
    > >
    > > print "\n";
    > >
    > > while ( (my $key, my @value) = each(%splitted_hap) ) {

    >
    >
    > The hash values are not arrays, @value can never have more
    > than one element in it...
    >
    >
    > > print "$key\t$value[0]\n";
    > > print "$key\t$value[1]\n";

    >
    >
    > ... so $value[1] will never be defined.
    >
    >
    > > }
    > >
    > >
    > > I get:
    > >
    > >
    > > first A
    > > first

    >
    >
    > Your program cannot possibly make that output.
    >
    > Many people will not volunteer to help you if you lie to them...
    >
    >
    >
    Guest1, Sep 3, 2003
    #4
  5. Guest1 <> wrote:


    > my @values = split(' ',$hap{$key});
    > $splitted_hap{$key} = \@values;



    You do not need the @values temporary array.

    If you use an anonymous array you can replace those 2 lines with this:

    $splitted_hap{$key} = [ split(' ',$hap{$key}) ];




    [ snip TOFU. Please quote your followups properly ]

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 3, 2003
    #5
    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. rp
    Replies:
    1
    Views:
    478
    red floyd
    Nov 10, 2011
  2. Anthony Martinez
    Replies:
    4
    Views:
    255
    Robert Klemme
    Jun 11, 2007
  3. Michal Suchanek
    Replies:
    6
    Views:
    212
    Nobuyoshi Nakada
    Jun 13, 2007
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    581
    David A. Black
    Jul 2, 2008
  5. Replies:
    6
    Views:
    94
Loading...

Share This Page