[32196] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3461 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Jul 31 11:14:21 2011

Date: Sun, 31 Jul 2011 08:14:12 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 31 Jul 2011     Volume: 11 Number: 3461

Today's topics:
        special characters in a hash variable <ela@yantai.org>
    Re: special characters in a hash variable <mir@miras.org>
    Re: special characters in a hash variable <ela@yantai.org>
    Re: special characters in a hash variable <willem@toad.stack.nl>
    Re: special characters in a hash variable <ela@yantai.org>
    Re: special characters in a hash variable <willem@toad.stack.nl>
    Re: special characters in a hash variable <ela@yantai.org>
    Re: special characters in a hash variable <willem@toad.stack.nl>
    Re: special characters in a hash variable <ela@yantai.org>
    Re: special characters in a hash variable <jurgenex@hotmail.com>
    Re: special characters in a hash variable <jurgenex@hotmail.com>
    Re: special characters in a hash variable <rweikusat@mssgmbh.com>
    Re: special characters in a hash variable <ela@yantai.org>
    Re: special characters in a hash variable <ela@yantai.org>
    Re: special characters in a hash variable <willem@toad.stack.nl>
    Re: special characters in a hash variable <tadmc@seesig.invalid>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sun, 31 Jul 2011 17:26:33 -0700
From: "ela" <ela@yantai.org>
Subject: special characters in a hash variable
Message-Id: <j133fq$dap$1@ijustice.itsc.cuhk.edu.hk>

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.
abc@yahoo.com
Hello! World?
Who loves $?

that makes my variable, for example,

$hash{"$name"}++;

goes crazy.

Any suggestions? 




------------------------------

Date: Sun, 31 Jul 2011 11:22:31 +0200
From: Michael Rasmussen <mir@miras.org>
Subject: Re: special characters in a hash variable
Message-Id: <20110731112231.0828eb8d@sleipner.datanom.net>

On Sun, 31 Jul 2011 17:26:33 -0700
"ela" <ela@yantai.org> 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 = ('abc@yahoo.com','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.



------------------------------

Date: Sun, 31 Jul 2011 18:39:04 -0700
From: "ela" <ela@yantai.org>
Subject: Re: special characters in a hash variable
Message-Id: <j137np$eq1$1@ijustice.itsc.cuhk.edu.hk>


"Michael Rasmussen" <mir@miras.org> wrote in message 
news:20110731112231.0828eb8d@sleipner.datanom.net...

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




------------------------------

Date: Sun, 31 Jul 2011 09:45:03 +0000 (UTC)
From: Willem <willem@toad.stack.nl>
Subject: Re: special characters in a hash variable
Message-Id: <slrnj3a90v.2t9u.willem@toad.stack.nl>

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


------------------------------

Date: Sun, 31 Jul 2011 19:27:08 -0700
From: "ela" <ela@yantai.org>
Subject: Re: special characters in a hash variable
Message-Id: <j13aht$fpe$1@ijustice.itsc.cuhk.edu.hk>


"Willem" <willem@toad.stack.nl> wrote in message 
news:slrnj3a90v.2t9u.willem@toad.stack.nl...
> 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";
    }
 }




------------------------------

Date: Sun, 31 Jul 2011 10:35:43 +0000 (UTC)
From: Willem <willem@toad.stack.nl>
Subject: Re: special characters in a hash variable
Message-Id: <slrnj3abvv.60a.willem@toad.stack.nl>

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


------------------------------

Date: Sun, 31 Jul 2011 19:41:32 -0700
From: "ela" <ela@yantai.org>
Subject: Re: special characters in a hash variable
Message-Id: <j13bct$g1r$1@ijustice.itsc.cuhk.edu.hk>


"Willem" <willem@toad.stack.nl> wrote in message 
news:slrnj3abvv.60a.willem@toad.stack.nl...
> 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.
abc@yahoo.com
Hello! World?
Who loves $?

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





------------------------------

Date: Sun, 31 Jul 2011 11:15:04 +0000 (UTC)
From: Willem <willem@toad.stack.nl>
Subject: Re: special characters in a hash variable
Message-Id: <slrnj3ae9o.i9t.willem@toad.stack.nl>

ela wrote:
) "Willem" <willem@toad.stack.nl> wrote in message 
) news:slrnj3abvv.60a.willem@toad.stack.nl...
)> 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.
) abc@yahoo.com
) Hello! World?
) Who loves $?
)
) So except that "Normal", the others have abnormal counts, e.g.
) I expect there should be a key abc@yahoo.com, but then I obtain two keys, 
) one is
) abc@yahoo.com,
) 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


------------------------------

Date: Sun, 31 Jul 2011 21:26:32 -0700
From: "ela" <ela@yantai.org>
Subject: Re: special characters in a hash variable
Message-Id: <j13hhp$i7t$1@ijustice.itsc.cuhk.edu.hk>


"Willem" <willem@toad.stack.nl> wrote in message 
news:slrnj3ae9o.i9t.willem@toad.stack.nl...
> 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? 




------------------------------

Date: Sun, 31 Jul 2011 06:22:57 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: special characters in a hash variable
Message-Id: <dela37d5d74e8d13gkkvkq864ju4ieod29@4ax.com>

"ela" <ela@yantai.org> 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


------------------------------

Date: Sun, 31 Jul 2011 06:56:30 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: special characters in a hash variable
Message-Id: <c3ma37pkeoqpnkg6o9jfj6mm2he2lql2k5@4ax.com>

"ela" <ela@yantai.org> wrote:
>"Willem" <willem@toad.stack.nl> wrote in message 
>news:slrnj3ae9o.i9t.willem@toad.stack.nl...
>> 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


------------------------------

Date: Sun, 31 Jul 2011 15:09:28 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: special characters in a hash variable
Message-Id: <87live35k7.fsf@sapphire.mobileactivedefense.com>

"ela" <ela@yantai.org> 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.


------------------------------

Date: Sun, 31 Jul 2011 23:22:43 -0700
From: "ela" <ela@yantai.org>
Subject: Re: special characters in a hash variable
Message-Id: <j13obk$khm$1@ijustice.itsc.cuhk.edu.hk>


"Jürgen Exner" <jurgenex@hotmail.com> wrote in message 
news:dela37d5d74e8d13gkkvkq864ju4ieod29@4ax.com...

> 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. 




------------------------------

Date: Sun, 31 Jul 2011 23:26:14 -0700
From: "ela" <ela@yantai.org>
Subject: Re: special characters in a hash variable
Message-Id: <j13oi6$kmg$1@ijustice.itsc.cuhk.edu.hk>


"Jürgen Exner" <jurgenex@hotmail.com> wrote in message 
news:c3ma37pkeoqpnkg6o9jfj6mm2he2lql2k5@4ax.com...
> "ela" <ela@yantai.org> wrote:
>>"Willem" <willem@toad.stack.nl> wrote in message
>>news:slrnj3ae9o.i9t.willem@toad.stack.nl...
>>> 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

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. 




------------------------------

Date: Sun, 31 Jul 2011 14:43:17 +0000 (UTC)
From: Willem <willem@toad.stack.nl>
Subject: Re: special characters in a hash variable
Message-Id: <slrnj3aqg5.1tfl.willem@toad.stack.nl>

ela wrote:
)
) "J?rgen Exner" <jurgenex@hotmail.com> wrote in message 
) news:dela37d5d74e8d13gkkvkq864ju4ieod29@4ax.com...
)
)> 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


------------------------------

Date: Sun, 31 Jul 2011 09:58:13 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: special characters in a hash variable
Message-Id: <slrnj3aqtu.h3i.tadmc@tadbox.sbcglobal.net>

ela <ela@yantai.org> wrote:

> Here I haven't copied all the codes directly but 
> post pseudocodes.


If you post pseudo code, then you can expect pseudo answers,
and round-and-round you'll go.

Post real code and get real answers.

If you put garbage in, you will get garbage out.


-- 
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.


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 3461
***************************************


home help back first fref pref prev next nref lref last post