Problems with Arithmetic Operators in a Perl 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;
}
}
}
 
J

J. Gleixner

coolchick said:

Yeah, we heard you the first time you posted this same question
15-minutes ago.
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

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

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

Why not have it die with why it failed?

die "Could not open $FILE: $!";

You should be using the three argument open.

open( my $file, '<', $FILE ) or die "Could not open $FILE: $!";

perldoc -f open
foreach $line (<FILE>) {

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

To learn more about regular expressions, see:

perldoc perlretut
 
P

Peter Makholm

coolchick said:
%operators = ('+',0,'-',0,'=',0,'*',0,'/',0);
while (($key,$value) = each(%operators)){
if ($line =~ /\$key/) {

I'm not quite sure of the exact rules, but in regular expressions a $
can be both an sigil for a scalar variable or the special character
matching the end of the string. In the above I would guess that perl
will match the literal string '$key'.

What you need is to interpolate the variable $key but with any special
characters escaped. This is done by using the \Q escape:

if ($line =~ /\Q$key/) {
$operators{$key}=$value+1;
}
}

//Makholm
 
J

John W. Krahn

coolchick said:
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

use warnings;
use strict;

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

my %operators = map { $_ => 0 } qw( + - = * / );

$FILE="operator.txt";

my $FILE = 'operator.txt';

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

open FILE, '<', $FILE or die "Could not open '$FILE' $!";

foreach $line (<FILE>) {

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

for my $key ( keys %operators ) {
$operators{ $key }++ if $line =~ /\Q$key/;
}




John
 
D

Dave Weaver

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

You should use the warnings & strict pragmas to allow perl to help you out.

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

Now you have enabled strictures, you need to declare your variables using 'my'.

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

Use the 3-argument form of open, and include "$!" in the error message so
you know *why* the open failed.

open my $f, '<', $FILE or die "Couldn't open '$FILE' : $!";
foreach $line (<FILE>) {

When iterating over the lines of a file, use "while" instead of "for" - "for"
will read all lines into memory at once; "while" will read them in one at a
time.

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

Looping over the keys of %operators for each line of the file isn't
terribly efficient. It might be better to construct a pattern that
matches any of the operators you're looking for:

if ( $line =~ m{([-+=*/])} ) {
++ $operators{ $1 };
}

Note that this will only find the first operator on the line.
To find all operators:

while ( $line =~ m{([-+=*/])}g ) {
++ $operators{ $1 };
}

So, putting it all together:

#!/usr/bin/perl
use strict;
use warnings;

my %operators;

my $FILE = 'operator.txt';

open my $f, '<', $FILE or die "Can't open '$FILE' : $!";

while ( my $line = <$f> ) {

while ( $line =~ m{([-+=*/])}g ) {
++ $operators{ $1 };
}
}
close $f;

use Data::Dumper;
print Dumper \%operators;
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top