Problems with Arithmetic Operators in a Perl hash

Problems with Arithmetic Operators in a Perl hash

am 01.11.2007 16:40:32 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 Arithmetic Operators in a Perl hash

am 01.11.2007 16:58:47 von glex_no-spam

coolchick wrote:
> Hi All,

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

Re: Problems with Arithmetic Operators in a Perl hash

am 01.11.2007 17:46:01 von Peter Makholm

coolchick writes:

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

Re: Problems with Arithmetic Operators in a Perl hash

am 01.11.2007 18:50:54 von krahnj

coolchick wrote:
>
> 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 () {

while ( my $line = ) {


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

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


> }
> }



John
--
use Perl;
program
fulfillment

Re: Problems with Arithmetic Operators in a Perl hash

am 05.11.2007 13:14:18 von Dave Weaver

On Thu, 01 Nov 2007 08:40:32 -0700, 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

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

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 = <$f> )

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

Re: Problems with Arithmetic Operators in a Perl hash

am 05.11.2007 14:01:09 von coolchick

Thanks a bunch for all your help.