Convert field values from currency to numbers

Posted on 2009-04-14
Last Modified: 2013-11-05
I have a text file that is being read by a perl file. There is a field in the text file for Price. It is currently stated as currency (nothing past the decimal point). I want to remove the $ from those field values. Below is the code where it begins to read the file and then remove the field names so that they don't show up in search results.
I'm assuming it should happen before shift but I'm rather ignorant with perl....
my($ref, @listings);
shift @listings; # get rid of header
foreach my $field (@FIELDS){

Open in new window

Question by:taagangel
  • 5
  • 4
LVL 84

Expert Comment

ID: 24142356
What is in @FIELDS before, and what do you want in @FIELDS after?

And what about @listings?

Author Comment

ID: 24142452
ozo... I'm not sure what you mean...
I'll attach the entire cgi file(saved at .txt for uploading here). Basically the fields in the tab-delim text file are defined in the script and are also the first record in the text file. I believe shift was used in order to remove the field names from the array once the text file is read.
LVL 84

Expert Comment

ID: 24142654
Where are the field values from which you want to remove the $

are you looking for something like
   s/\t\$/\t/g for @listings;
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.


Author Comment

ID: 24142728
The field in particular is $listings{'Price'}
The values for that field used to be simply:
now it's
as example - so now the portion of the script that performs a search based on a price range returns 0 results because the $ symbol is in the field value
LVL 84

Expert Comment

ID: 24142934
Is this what you are trying to do?
 s/(\$[^\t]*)/join'',split'[$,]',$1/e for @listings;

Author Comment

ID: 24149511
Yes, I think so - though my problem is I don't know where in the code to put that.... :-(
LVL 84

Accepted Solution

ozo earned 500 total points
ID: 24151017
woe about as soon as you assign @listings?
 s/(\$[^\t]*)/join'',split'[$,]',$1/e for @listings = @{&readlistings()};
(this assumes that COMPANY CODE does not contain a $, and that either Price always contains $ or that nothing else contains $)

Author Comment

ID: 24208424
Hiya - here's the snippet that I ended up using! Thanks so much for your help!
  s/(\$[^\t]*)/join'',split'[$,]',$1/e for @lines;
sub readlistings{
  open(FILE, $file) || &error("Could not open $file: $!");
  my @lines = <FILE>;
  @lines = reverse(@lines); 
  s/(\$[^\t]*)/join'',split'[$,]',$1/e for @lines; 
  return \@lines;

Open in new window


Author Closing Comment

ID: 31570136
Thanks again!

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Syslog text file into database or into .CSV 19 140
File Find regex problem 4 62
Vb script to unzip a files and rename the files 12 106
Coldfusion RegEx 8 63
I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question