Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Problems with Airthmetic Operators in a hash

Reply
Thread Tools

Problems with Airthmetic Operators in a hash

 
 
coolchick
Guest
Posts: n/a
 
      11-01-2007
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;
}
}
}

 
Reply With Quote
 
 
 
 
Narthring
Guest
Posts: n/a
 
      11-01-2007
On Nov 1, 10:25 am, coolchick <(E-Mail Removed)> 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 (<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

 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      11-01-2007
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}++.

--
Affijn, Ruud

"Gewoon is een tijger."

 
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
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Problems with Arithmetic Operators in a Perl hash coolchick Perl Misc 5 11-05-2007 01:01 PM
power of a large int and a modular airthmetic problems. Zero C Programming 20 09-08-2003 12:22 AM
Overloaded Operators Problems -Steve- C++ 2 07-28-2003 03:25 AM



Advertisments