Skip to content

Feature Request: :adverb to capture/return original CSV column-order. #32

@jubilatious1

Description

@jubilatious1

Feature Request: :adverb to capture/return original CSV column-order.

Hi, maybe I'm not "getting" the Text::CSV way of doing things, but one thing that is different between Perl5 and Raku is that Raku guarantees hash randomization.

Therefore, any method capturing "CSV_header" separate from "CSV_datarows" needs to retain column-order otherwise to a new user it appears that Raku hopelessly scrambles CSV columns, with no way of restoring.

Example below using a classic CSV from Microsoft: https://www.microsoft.com/en-us/download/details.aspx?id=45485

admin@MBP ~ % cat Import_User_Sample_en.csv
User Name,First Name,Last Name,Display Name,Job Title,Department,Office Number,Office Phone,Mobile Phone,Fax,Address,City,State or Province,ZIP or Postal Code,Country or Region
chris@contoso.com,Chris,Green,Chris Green,IT Manager,Information Technology,123451,123-555-1211,123-555-6641,123-555-9821,1 Microsoft way,Redmond,Wa,98052,United States
ben@contoso.com,Ben,Andrews,Ben Andrews,IT Manager,Information Technology,123452,123-555-1212,123-555-6642,123-555-9822,1 Microsoft way,Redmond,Wa,98052,United States
david@contoso.com,David,Longmuir,David Longmuir,IT Manager,Information Technology,123453,123-555-1213,123-555-6643,123-555-9823,1 Microsoft way,Redmond,Wa,98052,United States
cynthia@contoso.com,Cynthia,Carey,Cynthia Carey,IT Manager,Information Technology,123454,123-555-1214,123-555-6644,123-555-9824,1 Microsoft way,Redmond,Wa,98052,United States
melissa@contoso.com,Melissa,MacBeth,Melissa MacBeth,IT Manager,Information Technology,123455,123-555-1215,123-555-6645,123-555-9825,1 Microsoft way,Redmond,Wa,98052,United States

Above, column order is User Name, First Name, Last Name, Display Name, Job Title, Department, Office Number, Office Phone, Mobile Phone, Fax, Address, City, State or Province, ZIP or Postal Code, Country or Region


Text:CSV code below generally from the docs; column order is re-arranged to alphabetical: address, city, country or region, department, display name, fax, first name, job title, last name, mobile phone, office number, office phone, state or province, user name, zip or postal code

admin@MBP ~ %  raku -MText::CSV -e 'my $csv = Text::CSV.new;
                                    my $fh  = open "Import_User_Sample_en.csv", :r, :!chomp;  
                                    my @hdr = $csv.header($fh, munge-column-names => "fc").column-names;
                                    my @rows = $csv.getline_hr_all($fh); .say for @rows;'
{address => 1 Microsoft way, city => Redmond, country or region => United States, department => Information Technology, display name => Chris Green, fax => 123-555-9821, first name => Chris, job title => IT Manager, last name => Green, mobile phone => 123-555-6641, office number => 123451, office phone => 123-555-1211, state or province => Wa, user name => chris@contoso.com, zip or postal code => 98052}
{address => 1 Microsoft way, city => Redmond, country or region => United States, department => Information Technology, display name => Ben Andrews, fax => 123-555-9822, first name => Ben, job title => IT Manager, last name => Andrews, mobile phone => 123-555-6642, office number => 123452, office phone => 123-555-1212, state or province => Wa, user name => ben@contoso.com, zip or postal code => 98052}
{address => 1 Microsoft way, city => Redmond, country or region => United States, department => Information Technology, display name => David Longmuir, fax => 123-555-9823, first name => David, job title => IT Manager, last name => Longmuir, mobile phone => 123-555-6643, office number => 123453, office phone => 123-555-1213, state or province => Wa, user name => david@contoso.com, zip or postal code => 98052}
{address => 1 Microsoft way, city => Redmond, country or region => United States, department => Information Technology, display name => Cynthia Carey, fax => 123-555-9824, first name => Cynthia, job title => IT Manager, last name => Carey, mobile phone => 123-555-6644, office number => 123454, office phone => 123-555-1214, state or province => Wa, user name => cynthia@contoso.com, zip or postal code => 98052}
{address => 1 Microsoft way, city => Redmond, country or region => United States, department => Information Technology, display name => Melissa MacBeth, fax => 123-555-9825, first name => Melissa, job title => IT Manager, last name => MacBeth, mobile phone => 123-555-6645, office number => 123455, office phone => 123-555-1215, state or province => Wa, user name => melissa@contoso.com, zip or postal code => 98052}

How to output a CSV file in the original column order?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions