special characters in a hash variable

M

Michael Rasmussen

Any suggestions?
Use single quotes when assigning. Use keys as string constants or
single quotes:

#!/usr/bin/perl
use strict;
use warnings;

my @names = ('(e-mail address removed)','Hello! World?','Who loves $?');

my (%hash,$key);

foreach (@names) {
$hash{$_}++;
}

foreach $key(keys(%hash)) {
print "$key: $hash{$key}\n";
}




--
Hilsen/Regards
Michael Rasmussen
http://pgp.mit.edu:11371/pks/lookup?op=get&search=0xE3E80917
A computer is like air conditioning: it becomes useless when you open
windows.
 
W

Willem

ela wrote:
) This works when the exact names are known in advance. How about retrieving
) lines from a file, e.g.
)
) while (<FilePointer>) {
) $variable = $_;
) $hash{$variable}++;
) }

Have you tried it ?


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
W

Willem

ela wrote:
) Thanks for correcting this mistake. The pseudocodes should be:
)
) while ($line = <FilePointer>) {
) chomp $line;
) $variable = $line;
) $hash{"$variable"}{freq}++;
) if (<some criteria fulfilled>) {
) $hash{"$variable"}{type} = "A";
) } else {
) $hash{"$variable"}{type} = "B";
) }
) }

What does it do ? What do you want it to do ? Where does it go wrong ?


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
W

Willem

ela wrote:
) )> ela wrote:
)> ) Thanks for correcting this mistake. The pseudocodes should be:
)> )
)> ) while ($line = <FilePointer>) {
)> ) chomp $line;
)> ) $variable = $line;
)> ) $hash{"$variable"}{freq}++;
)> ) if (<some criteria fulfilled>) {
)> ) $hash{"$variable"}{type} = "A";
)> ) } else {
)> ) $hash{"$variable"}{type} = "B";
)> ) }
)> ) }
)>
)> What does it do ? What do you want it to do ? Where does it go wrong ?
)
) I make use of hash variable to take statistics of the frequency of different
) names stored in a file. However, I find some of the names contain special
) characters, for
) example,
)
) Normal
) I have 30% {abc} shares.
) (e-mail address removed)
) Hello! World?
) Who loves $?
)
) So except that "Normal", the others have abnormal counts, e.g.
) I expect there should be a key (e-mail address removed), but then I obtain two keys,
) one is
) (e-mail address removed),
) and the other is
) abc@yahoo

The code you posted above doesn't do that.
The mistake must be in some code you didn't post.
Please post a complete program that can be run and shows the problem.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
J

Jürgen Exner

ela said:
Thanks for correcting this mistake. The pseudocodes should be:

while ($line = <FilePointer>) {
chomp $line;
$variable = $line;
$hash{"$variable"}{freq}++;
if (<some criteria fulfilled>) {
$hash{"$variable"}{type} = "A";
} else {
$hash{"$variable"}{type} = "B";

Why are you stringifying the hash key? Please see "perldoc -q quoting"
"What's wrong with always quoting "$vars"?"

jue
 
J

Jürgen Exner

ela said:
Marvellous! You pointed out the problem correctly.
Now I have to use special characters as a separator in the hash name
instead, e.g.

What is a "hash name"? Do you mean the name of the hash, i.e. the
variable name like 'foobar' in '%foobar'? Or are you talking about the
keys in the hash? Either way, neither of them has a seperator.

Furthermore your code doesn't even pass the perl syntax checker.

Not to mention that you should always, yes always, add the obligatory
"use strict; use warnings;" !!! If you had done so then perl would have
given your a very crucial hint.
$cell1="It's a good day!";
$cell2="What?";

hash{"$cell1\%$cells2"}{freq}++;

Global symbol "$cells2" requires explicit package name at [...]
syntax error at [...], near "}{"
to result in:

hash{It's a good day!%What?}{freq}++;

Global symbol "%What" requires explicit package name at [...]
syntax error at [...], near "?}"

Now think about why the compiler may be giving you this specific error
(about the missing symbol, not the syntax error). Why would the compiler
try to interpret %What as a variable name? Think about it!
foreach key (sort (keys(%hash))) {
@tmp = split /\%/, $key
print "$tmp[0]\t$tmp[1]\n";
}

Well, it failed so what's the problem then?

"It failed" is the worst possible error description, along with "it
doesn't work".
I am sure you posted the lasted and most complete code sample you got.
And because you didn't say I must guess that you are stuck right there
with whatever perl is telling you about this piece of code.

Well, the error messages are pretty clear:
syntax error at t.pl line 4, near "}{"
syntax error at t.pl line 6, near "?}"
Missing $ on loop variable at t.pl line 8.
In each case you are missing the '$' sign (the sigil) in front of the
variable name '$hash' resp. '$key'.

Furthermore you have to enclose the key
It's a good day!%What?
in quotes(*) because it is not a single bareword.

And there is the statement separator (i.e. the ';') missing at the end
of
@tmp = split /\%/, $key

HTH

jue
 
R

Rainer Weikusat

[...]
while ($line = <FilePointer>) {
chomp $line;
$variable = $line;
$hash{"$variable"}{freq}++;
if (<some criteria fulfilled>) {
$hash{"$variable"}{type} = "A";
} else {
$hash{"$variable"}{type} = "B";
}
}

There is no point in interpolating these variables into strings before
using them as hash keys. Whatever their value happens to be, it will
be stringified if it doesn't already have a string value in order to
turn it into something usable as hash key.
 
W

Willem

ela wrote:
)
) )
)> Why are you stringifying the hash key? Please see "perldoc -q quoting"
)> "What's wrong with always quoting "$vars"?"
)>
)> jue
)
) Thanks a lot. Removing the quotes solves the problem.

That's very unusual, because the quoted vars were not causing any
actual problems in this case. Hash keys get stringified anyways.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
E

ela

I make use of hash variable to take statistics of the frequency of different
names. However, I find the names can contain special characters, for
example,

I have 30% {abc} shares.
(e-mail address removed)
Hello! World?
Who loves $?

that makes my variable, for example,

$hash{"$name"}++;

goes crazy.

Any suggestions?
 
E

ela

Use single quotes when assigning. Use keys as string constants or
single quotes:

my @names = ('(e-mail address removed)','Hello! World?','Who loves $?');

This works when the exact names are known in advance. How about retrieving
lines from a file, e.g.

while (<FilePointer>) {
$variable = $_;
$hash{$variable}++;
}
 
E

ela

Willem said:
ela wrote:
) This works when the exact names are known in advance. How about
retrieving
) lines from a file, e.g.
)
) while (<FilePointer>) {
) $variable = $_;
) $hash{$variable}++;
) }

Have you tried it ?

Thanks for correcting this mistake. The pseudocodes should be:

while ($line = <FilePointer>) {
chomp $line;
$variable = $line;
$hash{"$variable"}{freq}++;
if (<some criteria fulfilled>) {
$hash{"$variable"}{type} = "A";
} else {
$hash{"$variable"}{type} = "B";
}
}
 
E

ela

Willem said:
ela wrote:
) Thanks for correcting this mistake. The pseudocodes should be:
)
) while ($line = <FilePointer>) {
) chomp $line;
) $variable = $line;
) $hash{"$variable"}{freq}++;
) if (<some criteria fulfilled>) {
) $hash{"$variable"}{type} = "A";
) } else {
) $hash{"$variable"}{type} = "B";
) }
) }

What does it do ? What do you want it to do ? Where does it go wrong ?

I make use of hash variable to take statistics of the frequency of different
names stored in a file. However, I find some of the names contain special
characters, for
example,

Normal
I have 30% {abc} shares.
(e-mail address removed)
Hello! World?
Who loves $?

So except that "Normal", the others have abnormal counts, e.g.
I expect there should be a key (e-mail address removed), but then I obtain two keys,
one is
(e-mail address removed),
and the other is
abc@yahoo
 
E

ela

Willem said:
ela wrote:
The code you posted above doesn't do that.
The mistake must be in some code you didn't post.
Please post a complete program that can be run and shows the problem.

Marvellous! You pointed out the problem correctly.
Now I have to use special characters as a separator in the hash name
instead, e.g.

$cell1="It's a good day!";
$cell2="What?";

hash{"$cell1\%$cells2"}{freq}++;

to result in:

hash{It's a good day!%What?}{freq}++;

foreach key (sort (keys(%hash))) {
@tmp = split /\%/, $key
print "$tmp[0]\t$tmp[1]\n";
}

Well, it failed so what's the problem then?
 
E

ela

Why are you stringifying the hash key? Please see "perldoc -q quoting"
"What's wrong with always quoting "$vars"?"

jue

Thanks a lot. Removing the quotes solves the problem.
 
E

ela

Jürgen Exner said:
ela said:
Marvellous! You pointed out the problem correctly.
Now I have to use special characters as a separator in the hash name
instead, e.g.

What is a "hash name"? Do you mean the name of the hash, i.e. the
variable name like 'foobar' in '%foobar'? Or are you talking about the
keys in the hash? Either way, neither of them has a seperator.

Furthermore your code doesn't even pass the perl syntax checker.

Not to mention that you should always, yes always, add the obligatory
"use strict; use warnings;" !!! If you had done so then perl would have
given your a very crucial hint.
$cell1="It's a good day!";
$cell2="What?";

hash{"$cell1\%$cells2"}{freq}++;

Global symbol "$cells2" requires explicit package name at [...]
syntax error at [...], near "}{"
to result in:

hash{It's a good day!%What?}{freq}++;

Global symbol "%What" requires explicit package name at [...]
syntax error at [...], near "?}"

Now think about why the compiler may be giving you this specific error
(about the missing symbol, not the syntax error). Why would the compiler
try to interpret %What as a variable name? Think about it!
foreach key (sort (keys(%hash))) {
@tmp = split /\%/, $key
print "$tmp[0]\t$tmp[1]\n";
}

Well, it failed so what's the problem then?

"It failed" is the worst possible error description, along with "it
doesn't work".
I am sure you posted the lasted and most complete code sample you got.
And because you didn't say I must guess that you are stuck right there
with whatever perl is telling you about this piece of code.

Well, the error messages are pretty clear:
syntax error at t.pl line 4, near "}{"
syntax error at t.pl line 6, near "?}"
Missing $ on loop variable at t.pl line 8.
In each case you are missing the '$' sign (the sigil) in front of the
variable name '$hash' resp. '$key'.

Furthermore you have to enclose the key
It's a good day!%What?
in quotes(*) because it is not a single bareword.

And there is the statement separator (i.e. the ';') missing at the end
of
@tmp = split /\%/, $key

HTH

jue

Sorry for making this misunderstanding. In the real codes, I do add use
strict and use warnings. Here I haven't copied all the codes directly but
post pseudocodes. I will learn this lesson again and to prevent lousy
pseudocodes.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top