Hey all, its been a while.

I apologize for my prolonged vacancy in this community, I mean no harm.

I am forcing myself to learn crystal, as some sort of mental challenge for myself.


Code:
#!/usr/bin/env crystal

# Define a Hash to store the count of each login shell
shellcnt = Hash(String, Int32).new(0)

# Open the passwd file and iterate over each line
File.open("passwd") do |file|
  file.each_line do |line|
    # Split the line into fields using colon as the delimiter
    fields = line.chomp.split(':')
    # Extract the login shell from the last field
    shell = fields[-1]
    # Increment the count of this login shell in the Hash
    shellcnt[shell] += 1
  end
end

# Iterate over the Hash and print the counts for each login shell
shellcnt.each do |key, value|
  printf("%-20s%-8s%-d\n", key, ":", value)
end


i have written this in crystal.

it does this:

    Open a colon delimited unix password file (provided as 'passwd') in the current directory
    Read the file and count the instances of each login shell (the last field)
    For each login shell, print the number of accounts using each shell


I am trying to optimize this as best as I can, and I don't know if there's much more I can do.

thoughts?

some folk on r/unixporn's discord guild, recommended read buffers and trying other hash algos, but then again, the same people re-wrote this in rust, so go figure.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement