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

G

Guest1

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
 
A

Anno Siegel

Guest1 said:
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
 
T

Tad McClellan

Guest1 said:
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...
 
G

Guest1

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


Guest1 said:
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...
 
T

Tad McClellan

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 ]
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top