Solved

Convert field values from currency to numbers

Posted on 2009-04-14
9
163 Views
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);

$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
Comment
Question by:taagangel
  • 5
  • 4
9 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
What is in @FIELDS before, and what do you want in @FIELDS after?

And what about @listings?
0
 

Author Comment

by:taagangel
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
 

Author Comment

by:taagangel
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 84

Expert Comment

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

Author Comment

by:taagangel
Comment Utility
Yes, I think so - though my problem is I don't know where in the code to put that.... :-(
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
Comment Utility
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
 

Author Comment

by:taagangel
Comment Utility
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
 

Author Closing Comment

by:taagangel
Comment Utility
Thanks again!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now