Skip to content

Instantly share code, notes, and snippets.

@deepfryed
Created May 19, 2010 04:16
Show Gist options
  • Save deepfryed/405945 to your computer and use it in GitHub Desktop.
Save deepfryed/405945 to your computer and use it in GitHub Desktop.
// compile this as
// g++ -o read read.cpp -lpcre++
#include <iostream>
#include <fstream>
#include <pcre++.h>
using namespace std;
using namespace pcrepp;
int main(int argc, char *argv[]) {
Pcre regex("[,\\r\\n]+");
string line;
vector<string> cols;
ifstream file;
if (argc == 2) file.open(argv[1]);
if (file.is_open()) {
getline(file, line);
while (!file.eof()) {
getline(file, line);
cols = regex.split(line);
cout << cols[0] << " is " << atof(cols[1].c_str())*atof(cols[2].c_str()) << "\n";
}
file.close();
}
else {
cout << "ERROR: Please pass the filename on the command line.\n";
exit(1);
}
}
@shanna
Copy link

shanna commented May 19, 2010

In case you wanted one:

#!/usr/bin/env ruby

begin
  # You could also require csv but it seems like overkill for the sample.
  File.open(ARGV.first.to_s, 'r') do |fh|
    fh.readline # Drop headers.
    fh.each do |line|
      cols = line.split /,|\n/
      puts '%s is %.02f' % [cols[0], cols[1].to_f * cols[2].to_f]
    end
  end
rescue Errno::ENOENT
  puts 'Please pass the filename on the command line.'
  exit 1
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment