[ic] format array value

Mark Johnson markj@redhat.com
Wed, 14 Mar 2001 09:44:03 -0500


Mathew Jones wrote:
> 
> The results of an sql query are returned to @$resultsetfinal. What i
> would like to do is remove any price formating from the 3rd value within
> the array for each record e.g  s/\$//;
> and write that new format back into the value of the 3rd array element,
> so that when I use @$resultsetfinal again  to sort by price there is no
> unneccesary formating there.
> Your help is appreciated...
> 

I would look for a way to remove it from the query first, assuming your
SQL engine handles substrings in some way *and* your formatting is
consistent (which, frankly, is essential to good db management).
Something like:

SELECT substr(column, 2, length ( column ) - 1 ) as sort_price....

The syntax will heavily depend on your particular db.

However, if this isn't an option, or your data are inconsistent, you can
do it in Perl:

foreach (@$resultsetfinal) {
   $_->[2] =~ s/\$//g;
}

Perl is also pretty magical, and it wouldn't surprise me a bit to find
that it would perform a numeric sort by simply ignoring your non-numeric
$ signs, etc.

-- 
Mark Johnson
Senior Developer - Professional Services
Red Hat, Inc.
E-Business Solutions
markj@redhat.com
703-456-2912