Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

Convert field values from currency to numbers

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);
$ref=&readlistings(); 
@listings=@$ref;
 
shift @listings; # get rid of header
foreach my $field (@FIELDS){
  $field=~s/[\-\:]//sg;
  $field=~s/^\s*//sg;
  $field=~s/\s*$//sg;
}
undef($ref);

Open in new window

0
taagangel
Asked:
taagangel
  • 5
  • 4
1 Solution
 
ozoCommented:
What is in @FIELDS before, and what do you want in @FIELDS after?

And what about @listings?
0
 
taagangelAuthor Commented:
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.
res2.txt
0
 
ozoCommented:
Where are the field values from which you want to remove the $

are you looking for something like
   s/\t\$/\t/g for @listings;
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
taagangelAuthor Commented:
The field in particular is $listings{'Price'}
The values for that field used to be simply:
40,000
now it's
$40,000
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
0
 
ozoCommented:
Is this what you are trying to do?
 s/(\$[^\t]*)/join'',split'[$,]',$1/e for @listings;
0
 
taagangelAuthor Commented:
Yes, I think so - though my problem is I don't know where in the code to put that.... :-(
0
 
ozoCommented:
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 $)
0
 
taagangelAuthor Commented:
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; 
  close(FILE);
  return \@lines;
}

Open in new window

0
 
taagangelAuthor Commented:
Thanks again!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now