# How to identify unpaired files in a list

**URL:** <https://www.ruby-forum.com/t/how-to-identify-unpaired-files-in-a-list/172302>\
**Category:** Ruby\
**Created:** [July 18, 2009, 11:21pm UTC](https://www.ruby-forum.com/t/how-to-identify-unpaired-files-in-a-list/172302 "2009-07-18T23:21:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [July 18, 2009, 11:21pm UTC](https://www.ruby-forum.com/t/how-to-identify-unpaired-files-in-a-list/172302/1 "2009-07-18T23:21:03Z")

</div>

Hello there. I have a problem where I am trying to identify unpaired  
files in a directory.

## The directory may have files like the following:

## abcd0001.txt abcd0001.def.bak abcd0002.txt abcd0003.txt abcd0003.ghi.bak abcd0004.xyz.bak

What I’d like to do is identify the unpaired files ‘abcd0002.txt’ and  
‘abcd0004.xyz.bak’ in the list above.

I’ve tried bringing all the filenames into a single array but I’m not  
sure how to delete items that have duplicate _root_ names. Deleting  
duplicates is trivial, of course, but the extensions on the 2nd files  
always change.

I also tried creating 2 separate arrays (one for each file type) so  
that I can compare the root filenames, but then I’m left with looping  
through one array many times… which will have a huge performance hit  
when the filenames number in the 1,000’s.

Does anyone have any good suggestions that they can offer?

Please let me know. Thanks. Paul.

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [July 18, 2009, 11:36pm UTC](https://www.ruby-forum.com/t/how-to-identify-unpaired-files-in-a-list/172302/2 "2009-07-18T23:36:59Z")

</div>

> I also tried creating 2 separate arrays (one for each file type) so  
> that I can compare the root filenames, but then I’m left with looping  
> through one array many times… which will have a huge performance hit  
> when the filenames number in the 1,000’s.
> 
> Does anyone have any good suggestions that they can offer?

Perhaps nested hashes.  
=r

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [July 18, 2009, 11:39pm UTC](https://www.ruby-forum.com/t/how-to-identify-unpaired-files-in-a-list/172302/3 "2009-07-18T23:39:12Z")

</div>

> Perhaps nested hashes.

Oops I meant hashes of arrays, i.e. final data structure:

{‘abcd0001’ =\> [‘abcd0001.txt’, ‘abcd0001.bak.txt’], ‘abcd0003’ =\>  
[‘abcd0003’]}

then iterate through looking for arrays with length 1 only.  
GL.  
=r  
filename[’

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [July 19, 2009, 2:05am UTC](https://www.ruby-forum.com/t/how-to-identify-unpaired-files-in-a-list/172302/4 "2009-07-19T02:05:48Z")

</div>

On Jul 18, 5:39 pm, Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Perhaps nested hashes.
> 
> Oops I meant hashes of arrays, i.e. final data structure:
> 
> {‘abcd0001’ =\> [‘abcd0001.txt’, ‘abcd0001.bak.txt’], ‘abcd0003’ =\>  
> [‘abcd0003’]}
> 
> then iterate through looking for arrays with length 1 only.  
> GL.  
> =r

That’s cool. I’ll give it a try. Thanks.

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [July 19, 2009, 12:34am UTC](https://www.ruby-forum.com/t/how-to-identify-unpaired-files-in-a-list/172302/5 "2009-07-19T00:34:05Z")

</div>

I had the same requirement and ended up using Roger’s method except  
with an array of arrays:  
[[‘abcd0001.txt’, ‘abcd0001.bak.txt’],[‘abcd0003’], etc.]
