Skip to main content
Edit: Fixed an inconsistency in the given example data format.
Source Link
tbtom
  • 71
  • 6

Edited to fix an inconsistency in the given example data format.

I'm trying to write a simple function to load in a csv list of names which is formatted as "Lastname, Firstname, Initial" Initial " and convert it to "Firstname Lastname".

The following code works:

data = Import["class roll.csv", "CSV"]
names = StringCases[StringTrim@data, LetterCharacter ..][[;; , {2, 1}]]
names = StringJoin[#[[1]], " ", #[[2]]] & /@ names

but it feels a lot like a non-Mathematica type of way, i.e. I'm trying to break my old programming habits and learn how to implement this in more "native" Mathematica way.

If you have any suggestions, alternatives, more efficient ways, ... you'd like to share that'd be much appreciated.

Additional info in response to Ajasja's comment, the example data looks like:

ImportString["
lastname1 firstname2 Ii                             
lastname2 first2 Ii                                 
lastna3 longfirstname2 Ii                           
", "CSV"]

the last/first/initials are separated by a space, and each line is padded with spaces to get it to an equal (but large) width.

P.S. The [[-2;;1;;-1]] approach is very nice.

Thank you all.

I'm trying to write a simple function to load in a csv list of names which is formatted as "Lastname, Firstname, Initial" and convert it to "Firstname Lastname".

The following code works:

data = Import["class roll.csv", "CSV"]
names = StringCases[StringTrim@data, LetterCharacter ..][[;; , {2, 1}]]
names = StringJoin[#[[1]], " ", #[[2]]] & /@ names

but it feels a lot like a non-Mathematica type of way, i.e. I'm trying to break my old programming habits and learn how to implement this in more "native" Mathematica way.

If you have any suggestions, alternatives, more efficient ways, ... you'd like to share that'd be much appreciated.

Additional info in response to Ajasja's comment, the example data looks like:

ImportString["
lastname1 firstname2 Ii                             
lastname2 first2 Ii                                 
lastna3 longfirstname2 Ii                           
", "CSV"]

the last/first/initials are separated by a space, and each line is padded with spaces to get it to an equal (but large) width.

P.S. The [[-2;;1;;-1]] approach is very nice.

Thank you all.

Edited to fix an inconsistency in the given example data format.

I'm trying to write a simple function to load in a csv list of names which is formatted as "Lastname Firstname Initial " and convert it to "Firstname Lastname".

The following code works:

data = Import["class roll.csv", "CSV"]
names = StringCases[StringTrim@data, LetterCharacter ..][[;; , {2, 1}]]
names = StringJoin[#[[1]], " ", #[[2]]] & /@ names

but it feels a lot like a non-Mathematica type of way, i.e. I'm trying to break my old programming habits and learn how to implement this in more "native" Mathematica way.

If you have any suggestions, alternatives, more efficient ways, ... you'd like to share that'd be much appreciated.

Additional info in response to Ajasja's comment, the example data looks like:

ImportString["
lastname1 firstname2 Ii                             
lastname2 first2 Ii                                 
lastna3 longfirstname2 Ii                           
", "CSV"]

the last/first/initials are separated by a space, and each line is padded with spaces to get it to an equal (but large) width.

P.S. The [[-2;;1;;-1]] approach is very nice.

Thank you all.

added example data.
Source Link
tbtom
  • 71
  • 6

I'm trying to write a simple function to load in a csv list of names which is formatted as "Lastname, Firstname, Initial" and convert it to "Firstname Lastname".

The following code works:

data = Import["class roll.csv", "CSV"]
names = StringCases[StringTrim@data, LetterCharacter ..][[;; , {2, 1}]]
names = StringJoin[#[[1]], " ", #[[2]]] & /@ names

but it feels a lot like a non-Mathematica type of way, i.e. I'm trying to break my old programming habits and learn how to implement this in more "native" Mathematica way.

If you have any suggestions, alternatives, more efficient ways, ... you'd like to share that'd be much appreciated.

Additional info in response to Ajasja's comment, the example data looks like:

ImportString["
lastname1 firstname2 Ii                             
lastname2 first2 Ii                                 
lastna3 longfirstname2 Ii                           
", "CSV"]

the last/first/initials are separated by a space, and each line is padded with spaces to get it to an equal (but large) width.

P.S. The [[-2;;1;;-1]] approach is very nice.

Thank you all.

I'm trying to write a simple function to load in a csv list of names which is formatted as "Lastname, Firstname, Initial" and convert it to "Firstname Lastname".

The following code works:

data = Import["class roll.csv", "CSV"]
names = StringCases[StringTrim@data, LetterCharacter ..][[;; , {2, 1}]]
names = StringJoin[#[[1]], " ", #[[2]]] & /@ names

but it feels a lot like a non-Mathematica type of way, i.e. I'm trying to break my old programming habits and learn how to implement this in more "native" Mathematica way.

If you have any suggestions, alternatives, more efficient ways, ... you'd like to share that'd be much appreciated.

I'm trying to write a simple function to load in a csv list of names which is formatted as "Lastname, Firstname, Initial" and convert it to "Firstname Lastname".

The following code works:

data = Import["class roll.csv", "CSV"]
names = StringCases[StringTrim@data, LetterCharacter ..][[;; , {2, 1}]]
names = StringJoin[#[[1]], " ", #[[2]]] & /@ names

but it feels a lot like a non-Mathematica type of way, i.e. I'm trying to break my old programming habits and learn how to implement this in more "native" Mathematica way.

If you have any suggestions, alternatives, more efficient ways, ... you'd like to share that'd be much appreciated.

Additional info in response to Ajasja's comment, the example data looks like:

ImportString["
lastname1 firstname2 Ii                             
lastname2 first2 Ii                                 
lastna3 longfirstname2 Ii                           
", "CSV"]

the last/first/initials are separated by a space, and each line is padded with spaces to get it to an equal (but large) width.

P.S. The [[-2;;1;;-1]] approach is very nice.

Thank you all.

Source Link
tbtom
  • 71
  • 6

Swap first and last names in a list

I'm trying to write a simple function to load in a csv list of names which is formatted as "Lastname, Firstname, Initial" and convert it to "Firstname Lastname".

The following code works:

data = Import["class roll.csv", "CSV"]
names = StringCases[StringTrim@data, LetterCharacter ..][[;; , {2, 1}]]
names = StringJoin[#[[1]], " ", #[[2]]] & /@ names

but it feels a lot like a non-Mathematica type of way, i.e. I'm trying to break my old programming habits and learn how to implement this in more "native" Mathematica way.

If you have any suggestions, alternatives, more efficient ways, ... you'd like to share that'd be much appreciated.