Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > special characters in a hash variable

Reply
Thread Tools

special characters in a hash variable

 
 
Michael Rasmussen
Guest
Posts: n/a
 
      07-31-2011
On Sun, 31 Jul 2011 17:26:33 -0700
"ela" <(E-Mail Removed)> wrote:

>
> 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 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?...rch=0xE3E80917
A computer is like air conditioning: it becomes useless when you open
windows.

 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      07-31-2011
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
 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      07-31-2011
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
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      07-31-2011
ela wrote:
) "Willem" <(E-Mail Removed)> wrote in message
) news:(E-Mail Removed)...
)> 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.
) http://www.velocityreviews.com/forums/(E-Mail 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 Removed), but then I obtain two keys,
) one is
) (E-Mail 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
 
Reply With Quote
 
Jrgen Exner
Guest
Posts: n/a
 
      07-31-2011
"ela" <(E-Mail Removed)> 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";


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

jue
 
Reply With Quote
 
Jrgen Exner
Guest
Posts: n/a
 
      07-31-2011
"ela" <(E-Mail Removed)> wrote:
>"Willem" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>> 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.


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
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      07-31-2011
"ela" <(E-Mail Removed)> writes:

[...]

> 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.
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      07-31-2011
ela wrote:
)
) "J?rgen Exner" <(E-Mail Removed)> wrote in message
) news:(E-Mail Removed)...
)
)> 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
 
Reply With Quote
 
ela
Guest
Posts: n/a
 
      08-01-2011
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 Removed)
Hello! World?
Who loves $?

that makes my variable, for example,

$hash{"$name"}++;

goes crazy.

Any suggestions?


 
Reply With Quote
 
ela
Guest
Posts: n/a
 
      08-01-2011

"Michael Rasmussen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed). ..

> Use single quotes when assigning. Use keys as string constants or
> single quotes:
>
> my @names = ('(E-Mail 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}++;
}


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Counting utf-8 characters -special characters majna Javascript 4 09-19-2007 01:53 PM
Remove only special characters and junk characters from a file rvino Perl 0 08-14-2007 07:23 AM
Re: Meta-Characters, Special Characters xah@xahlee.org Java 2 05-31-2007 09:25 AM
How to convert HTML special characters to the real characters with a Java script Stefan Mueller HTML 3 07-23-2006 10:09 PM



Advertisments