Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

regular expressions

by pugsly62 (Novice)
on Feb 01, 2005 at 18:02 UTC ( [id://426985]=perlquestion: print w/replies, xml ) Need Help??

pugsly62 has asked for the wisdom of the Perl Monks concerning the following question:

Hi! is there a regular expression I can use instead of my line of code 'if ( $county eq 26005 )' ?

Also, for lines that match '$county eq 26005' I need to count/total the corresponding '$yr_tot' field on the same line. How do you total a $scalar column in perl? there doesn't appear to be a function similar to sql's 'count'.

Thanks!
Scott

use strict; use warnings; my $file = 'scott.d3'; my $FhIn; open $FhIn, '<', $file or die "Could not open $file: $!"; while( my $line = <$FhIn> ) { my ($county, $year, $yr_tot) = split ' ', $line; if ( $county eq 26005 ) { print "$county : $yr_tot\n"; } } close $FhIn or die $!;

20050201 Janitored by Corion: Added formatting

Replies are listed 'Best First'.
Re: regular expressions
by friedo (Prior) on Feb 01, 2005 at 18:12 UTC
    I don't understand why you want to use a regular expression instead of a simple equality operator. Because $county is a number, however, you should be using == instead of eq.

    To add up all the totals, simply declare a variable outside your loop and add each one.

    my $total = 0; while( my $line = <$FhIn> ) { my ($county, $year, $yr_tot) = split ' ', $line; if ( $county == 26005 ) { print "$county : $yr_tot\n"; $total += $yr_tot; } }
Re: regular expressions
by si_lence (Deacon) on Feb 01, 2005 at 18:14 UTC
    hi
    Use a total counter outside your loop and print it out at
    the end.
    Something along the lines of:
    use strict; use warnings; my $total; while(<DATA>) { my ($county, $year, $yr_tot) = split; $total += $yr_tot if $county =~ /26005/; } print "Total for county 26005: $total\n"; __END__ 26005 2004 1 26005 2004 2 26005 2004 3 26004 2004 1 26003 2004 1

    si_lence
    update: typo
Re: regular expressions
by Tanktalus (Canon) on Feb 01, 2005 at 18:15 UTC

    The question isn't entirely clear ... but here's my inferrence.

    First, use "==" rather than "eq" when comparing numbers. Of course, you may actually be comparing strings - but then put the string in quotes. For example, if "26005.0000" is the same as "26005", then use ==. If they're different, use eq, but put "26005" in quotes (single or double, doesn't matter).

    (also, please don't put all those spaces at the end of the lines .. they're just distracting ;->)

    use strict; use warnings; my $file = 'scott.d3'; my $FhIn; open $FhIn, '<', $file or die "Could not open $file: $!"; my $total; while( my $line = <$FhIn> ) { my ($county, $year, $yr_tot) = split ' ', $line; if ( $county == 26005 ) { # - or - # if ( $county eq '26005' ) { print "$county : $yr_tot\n"; $total += $yr_tot; } } close $FhIn or die $!; print "Total yr_tot's: $total\n";
Re: regular expressions
by holli (Abbot) on Feb 01, 2005 at 18:16 UTC
    No need to use a regex if your split works fine. besides that it is hard to create a regex without knowing what your data looks like. For the summing: simply create a variable and use "+"
    use strict; use warnings; my $file = 'scott.d3'; my $FhIn; my $all_yr_tot = 0; open $FhIn, '<', $file or die "Could not open $file: $!"; while( my $line = <$FhIn> ) { my ($county, $year, $yr_tot) = split ' ', $line; if ( $county eq 26005 ) { print "$county : $yr_tot\n"; $all_yr_tot += $yr_tot; } } close $FhIn or die $!;

    holli, regexed monk
Re: regular expressions
by blazar (Canon) on Feb 01, 2005 at 19:36 UTC
    Hi! is there a regular expression I can use instead of my line of code 'if ( $county eq 26005 )' ?
    1. Chances are you may want if ( $county == 26005 ) instead.
    2. To answer your question: yes! But unless you have a very good reason to do so... don't!!
    Also, for lines that match '$county eq 26005' I need to count/total the corresponding '$yr_tot' field on the same line. How do you total a $scalar column in perl? there doesn't appear to be a function similar to sql's 'count'.
    There doesn't appear to be a function similar to a gasoline pump as well.

    To answer your question: $total+=$yr_tot.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://426985]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-23 12:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found