# Beginner array manipulation questions

**URL:** <https://www.ruby-forum.com/t/beginner-array-manipulation-questions/57389>\
**Category:** Ruby\
**Created:** [April 10, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/beginner-array-manipulation-questions/57389 "2006-04-10T16:27:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [April 10, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/beginner-array-manipulation-questions/57389/1 "2006-04-10T16:27:21Z")

</div>

Suppose the variable path is an array of fully qualified path and file  
names (e.g., in Windows, an item might be “c:\folder\file.txt”). What  
would be an efficient way of creating another array that contained only  
the file extensions (one or more characters following the last .  
character, if any)? I know I could use a while loop and a counter, but  
figure there is a better way in Ruby.

Alternatively, how could I change the array so that it was two  
dimensional where each “row” had two “columns” consisting of the full  
name and the extension only?

Jamal

---

<div class="post-metadata">

**Author:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [April 10, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/beginner-array-manipulation-questions/57389/2 "2006-04-10T16:39:45Z")

</div>

On Apr 10, 2006, at 9:26 AM, Jamal M. wrote:

> Suppose the variable path is an array of fully qualified path and file  
> names (e.g., in Windows, an item might be “c:\folder\file.txt”). What  
> would be an efficient way of creating another array that contained  
> only  
> the file extensions (one or more characters following the last .  
> character, if any)? I know I could use a while loop and a counter,  
> but  
> figure there is a better way in Ruby.

> > files = %w{c:\folder\file1.txt c:\folder\file2.exe c:\folder  
> > \file3.pdf} =\> [“c:\folder\file1.txt”, “c:\folder\file2.exe”, “c:  
> > \folder\file3.pdf”]
> 
> > files.map { |f| File.extname(f) }  
> > =\> [".txt", “.exe”, “.pdf”]

> Alternatively, how could I change the array so that it was two  
> dimensional where each “row” had two “columns” consisting of the full  
> name and the extension only?

> > files.map { |f| e = File.extname(f); [f[0…(-1 - e.length)], e] }  
> > =\> [[“c:\folder\file1”, “.txt”], [“c:\folder\file2”, “.exe”], [“c:  
> > \folder\file3”, “.pdf”]]

Hope that helps.

James Edward G. II
