use of "delete" for hash keys

E

ela

For the following codes, the 2nd and 3rd lines print out a lot of IDs and
keys and the corresponding hash values. However, after executing the 4th
line, none is left for the 5th line to print. The 1st line should make
%absent contain more hash values than to delete by the 4th line. So I doubt
that I misunderstand something in usage, could anybody point the problem
out?


my %absent = map {$_ => 1} keys %Priseq;
foreach $key (keys %absent) {print $key, '=', $absent{$key}, "\n";
<STDIN>;}
foreach $key (@ids) {print $key, "\n"; <STDIN>;}
foreach $key (@ids) {delete $absent{$key};}
foreach $key (keys %absent) {print $key, '=', $absent{$key}, "\n";
<STDIN>;}
 
S

sln

For the following codes, the 2nd and 3rd lines print out a lot of IDs and
keys and the corresponding hash values. However, after executing the 4th
line, none is left for the 5th line to print. The 1st line should make
%absent contain more hash values than to delete by the 4th line. So I doubt
that I misunderstand something in usage, could anybody point the problem
out?


my %absent = map {$_ => 1} keys %Priseq;
foreach $key (keys %absent) {print $key, '=', $absent{$key}, "\n";
<STDIN>;}
foreach $key (@ids) {print $key, "\n"; <STDIN>;}
foreach $key (@ids) {delete $absent{$key};}
foreach $key (keys %absent) {print $key, '=', $absent{$key}, "\n";
<STDIN>;}

I don't understand. If you take the 4th word from the end of the
3rd line from the top of the last key and put it in the right spot,
it should work.

-sln
 
E

ela

I guess I do not create the data structure correctly because previously you
taught me by this example script:

my @ids = qw/2 1 4/;
my %countries = qw(
3 Japan
1 USA
4 England
2 China
5 Australia
);

my %absent = map {$_ => 1} keys %countries;
foreach $key (sort(keys %absent)) {print $key, '=', $absent{$key}, "\n";}
print "\n";
foreach $key (keys %absent) {print $key, '=', $absent{$key}, "\n";}
delete $absent{$_} for @ids;

print "missing countries:\n";
print "$_\n" for @countries{keys %absent};


The above does work, when I adopted the concept to my data, which is like:
3 Japan
China
5
Australia

by the following codes:

$fline =<FFP>; #FFP is the file pointer for the above file
%Priseq;
while ($fline ne "") {
chomp $fline;
if ($fline =~ />/) {
@fcells = split />/, $fline;
$fline =<FFP>;
$Priseq{ $fcells[1] } = $fline;
}
$fline =<FFP>;
}

and then another tab-delimited file contains data like:

1 North America
3 Asia
4 Europe

is read by the following codes:

$line =<TFP>; #TFP is the file pointer for the above file
while ($line ne "") {
if ($line eq "\n") {
$line = <TFP>;
next;
}
@cells = split /\t/, $line;
$pattern = $cells[0]; #print "$pattern"; <STDIN>;

if ($mode eq "p") { # $mode is the variable to make this program
flexible, so I can choose to print present and absent values
print OFP ">$pattern\n$Priseq{ $pattern }";
} else {
push @ids, $pattern;#print "$pattern"; <STDIN>;
}
$line = <TFP>;
}

Then the followings are the codes that I posted previously.

if ($mode ne "p") {
my %absent = map {$_ => 1} keys %Priseq;
delete $absent{$_} for @ids;
print ">$key\n$_\n" for @Priseq{keys %absent};
}


What data structures should I use then? Is that I shouldn't use "@" for
Priseq? But I was taught previously to print the content, I have to use
"@"......
 
E

ela

Tad McClellan said:
Since you seem to be remarkably dedicated to not doing those things,
I conclude that you are not interested in getting answers to your
Perl questions.

So I will honor your desire, and not provide you with any more answers.

You are on your own. Into the killfile with you.


*plonk*


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.

Sorry for creating inconvenience but unfortunately I am unable to
differentiate:

- Do not re-type Perl code
- Provide enough information
- Do not provide too much information
Do not just post your entire program for debugging. Most especially
do not post someone *else's* entire program.

from insufficient information. Sometimes when I remove the author name and
then a guy comes out and tells me "I shouldn't do that" and then sometimes
"too much useless information". In my own codes, I DO include use strict and
use warnings and to facilitate reading I just posted the exercpt instead of
dumping the whole codes and the big data files. I also adhere to spending
time on thinking out a meaningful "Subject" . Another problem that I
frequently encounter is that I just don't know what keywords I should use in
order to Google/study documentation (In this example, hash array reference
doesn't work at all). Occassionally some kind guys will give me some
reference such as perlref and then I learn a little bit more about some
complex data structures in perl. I sincerely apologize for making such
inconvenience and would very appreciate if I could better understand what is
right & what is wrong in posting questions. Wasting other's time indeed also
wastes my own time and therefore I never deliberately mean that.
 
E

ela

ela said:
I guess I do not create the data structure correctly because previously you
taught me by this example script:

my @ids = qw/2 1 4/;
my %countries = qw(
3 Japan
1 USA
4 England
2 China
5 Australia
);

my %absent = map {$_ => 1} keys %countries;
foreach $key (sort(keys %absent)) {print $key, '=', $absent{$key}, "\n";}
print "\n";
foreach $key (keys %absent) {print $key, '=', $absent{$key}, "\n";} delete
$absent{$_} for @ids;

print "missing countries:\n"; print "$_\n" for @countries{keys %absent};


The above does work, when I adopted the concept to my data, which is like:
3 Japan
China
5
Australia

by the following codes:

$fline =<FFP>; #FFP is the file pointer for the above file
%Priseq;
while ($fline ne "") {
chomp $fline;
if ($fline =~ />/) {
@fcells = split />/, $fline;
$fline =<FFP>;
$Priseq{ $fcells[1] } = $fline;
}
$fline =<FFP>;
}

and then another tab-delimited file contains data like:

1 North America
3 Asia
4 Europe

is read by the following codes:

$line =<TFP>; #TFP is the file pointer for the above file
while ($line ne "") {
if ($line eq "\n") {
$line = <TFP>;
next;
}
@cells = split /\t/, $line;
$pattern = $cells[0]; #print "$pattern"; <STDIN>;

if ($mode eq "p") { # $mode is the variable to make this program
flexible, so I can choose to print present and absent values
print OFP ">$pattern\n$Priseq{ $pattern }";
} else {
push @ids, $pattern;#print "$pattern"; <STDIN>;
}
$line = <TFP>;
}

Then the followings are the codes that I posted previously.

if ($mode ne "p") {
my %absent = map {$_ => 1} keys %Priseq;
delete $absent{$_} for @ids;
print ">$key\n$_\n" for @Priseq{keys %absent};
}


What data structures should I use then? Is that I shouldn't use "@" for
Priseq? But I was taught previously to print the content, I have to use
"@"......

The solution is as follows, why I include the commented lines is to show
that I did try debugging myself in order to understand the complex data
structures used in Perl.

my %absent = map {$_ => 1} keys %Priseq;
#foreach $key (sort(keys %absent)) {print $key, '=', $absent{$key},
"\n"; <STDIN>;}
delete $absent{$_} for @ids;
# foreach $key (keys %absent) {print $key, '=', $absent{$key}, "\n";
<STDIN>;}
#foreach $key (@ids) {delete $absent{$key};}
#foreach $key (@ids) {print $key, "\n"; <STDIN>;}
#foreach $key (keys %absent) {print $key, '=', $absent{$key}, "\n";
<STDIN>;}

foreach $key (keys %absent) { print OFP ">$key\n$Priseq{$key}"; }
#print ">$key\n$_\n" for @Priseq{keys %absent};

# if exists key
 
E

ela

Tad McClellan said:
The Posting Guidelines say that you should not quote .sigs,
yet here you are, quoting sigs.
Ok, now finally I understand what a .signature is... sorry for not knowing
what it is before...
Nobody asked you to dump the whole code or big data files!

You were asked to post a short and complete program that illustrates
the problem you are having.

(Note how I made a short and complete program for you to run, you
should be able to do that for us too.
)

I will strike a good balance from learning this example then.
If your code reads a file into an array, and that part is working OK,
then just post code that initializes the array to the same values it
would have gotten if you read them from a file.

Oh, I see.
That is because there ARE NO REFERENCES in the code in this thread.

I can't even remember what your question was anymore...

Do "references" mean previous messages? Sorry to look silly, but it's better
to clarify than being misunderstood as misbehaviour...
 
J

Jim Gibson

This ^^^^^^^^^ word is "reference". It is a Perl construct that means a
value that allows access to a hash (anonymous or otherwise).
Do "references" mean previous messages?

No. You used the word (see above) in the context of Perl references,
which mean indirect access to Perl values.
 

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,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top