Problems with Airthmetic Operators in a hash

C

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 (<FILE>) {

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

Narthring

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 (<FILE>) {

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

}


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

\Q quotes pattern metacharacters until \E
 
D

Dr.Ruud

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 (<FILE>) {

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

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top