Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] search and replace

Expand Messages
  • Charles K. Clarkson
    ... I don t like subroutines that modify variables defined outside the subroutine. For that reason I re-wrote the sub as well as offering a different method to
    Message 1 of 2 , Jul 4, 2002
    • 0 Attachment
      "Chris LaFrance" <lafrance@...> wrote:

      : I have a variable called $amts which is gathered from a table in a MySQL
      : databse. This variable is a list of numbers (amounts), what I am trying to
      : do is replace the spaces with the + sign to have output like this:
      :
      : original: 200.00 420.00 50.00 60.00
      : new: 200.00+420.00+50.00+60.00
      :
      :
      : here's a chunk of the code:
      :
      : sub total { # Determines total amount spent
      : $gettotal = $db-> prepare ("SELECT amount FROM $table");
      : $gettotal-> execute;
      :
      : while ( (@rows = $gettotal -> fetchrow_array) != undef)
      : {
      : $count++;
      : $amts=$rows[0];
      : $formula=~s/ /+/ ,$amts;
      : };
      :
      : print qq(
      : Formula: $formula
      : );
      : $gettotal-> finish;
      : }

      I don't like subroutines that modify variables defined outside
      the subroutine. For that reason I re-wrote the sub as well as
      offering a different method to retrieve the formula.


      my( $formula, @rows ) = total( $dbh, $table );
      my $count = @rows;
      print qq|
      Formula: $formula
      Count: $count
      |;


      sub total {
      my( $dbh, $table ) = @_;
      my $sth = $dbh->prepare( qq|SELECT amount FROM $table| );
      $sth->execute;

      my $amounts = $sth->fetchall_arrayref;

      # convert AoA to Array
      $_ = $_->[0] foreach @$amounts;

      (my $formula = join '', @$amounts) =~ tr/ /+/;

      $sth->finish;
      return ( $formula, @$amounts );
      }

      This uses the fetchall_arrayref method since we're grabbing
      the whole column anyway. @rows in scalar context is the count,
      so there's no reason for $count anymore. The commented line
      of code converts $amounts from a reference to an array of
      arrays to a reference to an array. I used $sth and $dbh
      because most people are more used to them than $db and
      $gettotal, which is misleading since we never retrieve a total.

      (my $formula = join '', @$amounts) =~ tr/ /+/;

      Is a shortened form of:

      my $formula = join '', @$amounts;
      $formula =~ tr/ /+/;

      We could add a total by changing the commented statement
      or by 'eval'ing $formula.

      my $total;
      foreach ( @$amounts ) {
      # convert AoA to Array
      $_ = $_->[0];
      $total += $_;
      }


      HTH,

      Charles K. Clarkson
      --
      Head Bottle Washer,
      Clarkson Energy Homes, Inc.
      Small commercial and residential rehabilitation specialists.
      254 968-8328

      CJ Web Work - Solutions for Real Estate Investors.
      972 216-4952
    Your message has been successfully submitted and would be delivered to recipients shortly.