Problems with Airthmetic Operators in a hash

Problems with Airthmetic Operators in a hash

am 01.11.2007 16:25:03 von coolchick

Hi All,

I am trying to go through a file and grab all the arithmetic operators
using a perl hash.
It is not working for me. What am I doing wrong? I think my issue is
with the key value that I can't escape. HELP!

#!/usr/bin/perl

%operators = ('+',0,'-',0,'=',0,'*',0,'/',0);

$FILE="operator.txt";
open(FILE) or die("Could not open $FILE.");

foreach $line () {

while (($key,$value) = each(%operators)){
if ($line =~ /\$key/) {
$operators{$key}=$value+1;
}
}
}

Re: Problems with Airthmetic Operators in a hash

am 01.11.2007 19:16:23 von Narthring

On Nov 1, 10:25 am, coolchick wrote:
> Hi All,
>
> I am trying to go through a file and grab all the arithmetic operators
> using a perl hash.
> It is not working for me. What am I doing wrong? I think my issue is
> with the key value that I can't escape. HELP!
>
> #!/usr/bin/perl
>
> %operators = ('+',0,'-',0,'=',0,'*',0,'/',0);
>
> $FILE="operator.txt";
> open(FILE) or die("Could not open $FILE.");
>
> foreach $line () {
>
> while (($key,$value) = each(%operators)){
> if ($line =~ /\$key/) {
> $operators{$key}=$value+1;
> }
> }
>
> }


Change /\$key/ to /\Q$key\E/

\Q quotes pattern metacharacters until \E

Re: Problems with Airthmetic Operators in a hash

am 01.11.2007 23:11:36 von rvtol+news

coolchick schreef:

> I am trying to go through a file and grab all the arithmetic operators
> using a perl hash.
> It is not working for me. What am I doing wrong? I think my issue is
> with the key value that I can't escape. HELP!
>
> #!/usr/bin/perl
>
> %operators = ('+',0,'-',0,'=',0,'*',0,'/',0);
>
> $FILE="operator.txt";
> open(FILE) or die("Could not open $FILE.");
>
> foreach $line () {
>
> while (($key,$value) = each(%operators)){
> if ($line =~ /\$key/) {
> $operators{$key}=$value+1;
> }
> }
> }

perldoc -f quotemeta
perldoc -f index
perldoc -f keys

You are aware that you are only counting maximally one of each operator
per line?

You don't need the each(), becausee you can do $operators{$key}++.

--
Affijn, Ruud

"Gewoon is een tijger."