02-26-2025, 11:27 PM
(This post was last modified: 02-26-2025, 11:29 PM by DerVVulfman.)
Sort File Snippet
Introduction
This is a very simple executable method that will:
- Open a text file
- Sort the lines alphabetically
- Re-write the txt file with the sorted lines
Screenshots
This sorts a file... what would you see???
Script
Code:
#==============================================================================
# ** Sort File Snippet
#------------------------------------------------------------------------------
# This is a very simple executable method that will:
# 1) Open a text file
# 2) Sort the lines alphabetically
# 3) Re-write the txt file with the sorted lines
#==============================================================================
#--------------------------------------------------------------------------
# * Sort a file
#--------------------------------------------------------------------------
#
def sort_a_file
#
# Part 1: Define the file (in the project's root directory)
filename = "supercalifragilistic_expialidocious.txt"
#
# Part 2: Sort the contents
file_handle = File.new(filename) # Create a File's I/O Source variable
# ---------------------------------------------------------------------
# # Optionally, an I/O Source variable
# # may have been opened with the 'r'
# # flag meaning 'readable'
#
# file_handle = File.open(filename, 'r')
# ---------------------------------------------------------------------
lines = file_handle.readlines # Read all lines from the I/O source
file_handle.close # Close the I/O source
#
# Part 3: Sort the contents
lines.sort! # Sort all the lines read
#
# Part 4: Write the contents back
file_handle = File.open(filename, 'w') # Set a I/O Source as a writeable file
file_handle.write(lines) # Write all lines into the I/O Source
file_handle.close # Close the I/O source
#
end
# Execute the method at start
sort_a_file
Instructions
This is more of a simple feature and/or a tool, and not really used as a game system in itself. Comically, I've used it to sort through massive lists I've accumulated.
FAQ
A lot of the file size is from comments explaining what each line generally performs.
Compatibility
Given it is nothing but RUBY, it isn't just an RMXP script but should work cross-Ruby-platform.
Terms and Conditions
No credit required. Its too simple.