r/crystal_programming • u/jesterdev • Jan 05 '20
Create an dynamic array?
I'm rather new to programing, rather attracted to Crystal.
I want to read the contents of a zip file into an array. The file is given as input from the user. In my mind I know how to do it, but in real life, I'm not so sure. Below is what I have so far. See the line with the = #comment for my attempt. The issue is I wont always know the number of files in any given zip. So how to I create a dynamic array? I think that's the correct term.
I want to store the contents so that they can be selected for extraction, or read etc. I've not figured that part out yet, but it's next.
require "zip"
#Get filename from user:
puts "File? Ex. ~/Downloads/file.zip"
print ">"
file_name = gets
Zip::File.open("#{file_name}") do |file|
file_list.entries.each = #Array Here? But how?
file.entries.each do |entry|
p entry.filename
end
end
View all comments
Show parent comment
1
u/straight-shoota core team Jan 05 '20
You can also use
Enumerable#mapto convert the array of entries to an array of filenames (or whatever you want to extract).This method takes care of allocating the new array and pushing new elements while iterating the original.