Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Problems with Arithmetic Operators in a Perl hash (http://www.velocityreviews.com/forums/t905164-problems-with-arithmetic-operators-in-a-perl-hash.html)

coolchick 11-01-2007 03:40 PM

Problems with Arithmetic Operators in a Perl hash
 
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. Gleixner 11-01-2007 03:58 PM

Re: Problems with Arithmetic Operators in a Perl hash
 
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 (<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

Peter Makholm 11-01-2007 04:46 PM

Re: Problems with Arithmetic Operators in a Perl hash
 
coolchick <naureen.nizam@gmail.com> 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

John W. Krahn 11-01-2007 05:50 PM

Re: Problems with Arithmetic Operators in a Perl hash
 
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 (<FILE>) {


while ( my $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
--
use Perl;
program
fulfillment

Dave Weaver 11-05-2007 12:14 PM

Re: Problems with Arithmetic Operators in a Perl hash
 
On Thu, 01 Nov 2007 08:40:32 -0700, coolchick <naureen.nizam@gmail.com> 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 (<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 = <$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;


coolchick 11-05-2007 01:01 PM

Re: Problems with Arithmetic Operators in a Perl hash
 

Thanks a bunch for all your help.



All times are GMT. The time now is 05:08 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.