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

Re: [PBML] search and replace

Expand Messages
  • RSS
    Hai Chris, Try with this part of code....This is straight forward and it works, but I m not sure whether its the best way to do it sub total { # Determines
    Message 1 of 2 , Jul 2, 2002
    • 0 Attachment
      Hai Chris,

      Try with this part of code....This is straight forward and it works, but
      I'm not sure whether its the best way to do it

      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];
      # MODIFIED CODE STARTS
      $formula = $amts;
      $formula =~ s/ /+/g;
      # MODIFIED CODE ENDS
      };
      print qq(
      Formula: $formula
      );
      $gettotal-> finish;
      }





      Chris LaFrance
      <lafrance@bcn. To: perl-beginner@yahoogroups.com
      net> cc: chris@...
      Subject: [PBML] search and replace
      2002/07/03
      08:57
      Please respond
      to
      perl-beginner







      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 was using s/// to search and replace but I am not getting any output at
      all right now. I've tried both of the following to do this with no luck.

      $formula=~s/ /+/ ,$amts;
      $formula=~s/ /\+/ ,$amts;

      Any direction on this would be appreciated. Thanks.

      --
      Chris LaFrance
      chris@...


      Unsubscribing info is here:
      http://help.yahoo.com/help/us/groups/groups-32.html

      Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    • 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 2 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.