Skip to content

Instantly share code, notes, and snippets.

@ashaw
Created May 26, 2010 20:31
Show Gist options
  • Save ashaw/415003 to your computer and use it in GitHub Desktop.
Save ashaw/415003 to your computer and use it in GitHub Desktop.
#State,attribute
#California,sunshine
#California,beaches
#Texas,oil
#Texas,rock_festivals
s = TableFu.new(csv)
s.faceted_by("State").each do |state|
puts state.row
end
#or even better
s.faceted_by("State").each do |state|
puts state.row['attribute']
end
@thejefflarson
Copy link

Well, if you're faceting then you actually are looping through tf instances so, let me push a fix.

@thejefflarson
Copy link

also, what do you want state.row to output? It'll just send and array of tablefu::datum instances.

@ashaw
Copy link
Author

ashaw commented May 26, 2010

state.row should output an array of all the rows sorted by the "State" column. You can get that effect now by doing

s = s.faceted_by "State"
s.each do |state|
 rows = state.table
 rows.each do |row|
   puts row
 end
end

though it's a little clunky and hard to actually get at the data for just one common column. My second example in the gist should get you

sunshine
beaches
oil
rock_festivals

@thejefflarson
Copy link

Yeah it's always gunna be a bit clunky for the one common column. but you can now do row[key] for all rows. and then you can accomplish this with:

s.faceted_by("State").map { |state|
   state.rows.map { |row| row['attribute'] }
}.flatten
# ['sunshine', 'beaches', 'oil', 'rock festivals']

Of course rather than all these hoops, you can also just grab the original table and loop through the rows[key]

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