Created
October 22, 2010 13:28
-
-
Save gdemir/640528 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'csv' | |
##[global]############################################################# | |
$kisiler = {} #db.csv'den okunan bilgilerin tutuldugu sozluk | |
$csvname = "db.csv" #okunacak db'nin adı. | |
$kafa = [] #csv dosyasının ilk satırı.(örn : ["ad", "soyad", "no"]) | |
$key = "no" #herhangi bir csv de key olarak seçeceginiz ifade. | |
$value = [] #$kafa'nın keysiz hali.(örn : ["ad", "soyad"]) | |
####################################################################### | |
# | |
# | |
# csv'de alanlar "ad", "sd" ,"no", "un" olsun. | |
# global olarak $key = "no" seçerseniz, yükleme sırasında | |
# kisiler = { | |
# no => { "ad" => "gokhan", "sd" => "demir", "un" => "gdemir"}, | |
# no => { "ad" => "emin", "sd" => "eker", "un" => "eeker" }, | |
# } | |
# | |
# ve $kafa = ["no", "ad", "sd", "un"], $value = ["ad", "sd", "un" ]olacak, | |
# | |
# şekilde yükleyecektir.Örneğin csv'de gokhanın no'su "12345" olsun. | |
# | |
# ÖRNEK[1] : ogrenci = $kisiler["12345"] denirse, | |
# print ogrenci --> {"ad" => "gokhan", "sd" => "demir", "un" => "gdemir"} döner. | |
# | |
# ÖRNEK[2] : ogrenci = $kisiler["12345"] | |
# print ogrenci["ad"] --> "gokhan" döner. | |
# | |
# ÖRNEK[3] : for no in $kisiler.keys | |
# print $kisiler[no]["ad"] --> "gokhan", "emin" döner. | |
# | |
# ÖRNEK CSV : | |
# no, ad , sd, un | |
# 1,emin,eker,, | |
# 2,emre,yılmaz,, | |
# | |
def _values(kisi, key_indis, sozluk) | |
$kafa.each_index{ |indis| | |
alan = $kafa[indis] | |
sozluk[alan] = kisi[indis] if indis != key_indis | |
} | |
sozluk | |
end | |
def yukle | |
oku = CSV.open($csvname, "r") | |
$kafa = oku.shift # ["no","ad","sd","un"] | |
*$value = *$kafa | |
key_indis = $kafa.index($key) | |
$value.delete($key) # ["ad", "sd", "un"] | |
oku.each { |kisi| | |
$kisiler[kisi[key_indis]] = _values(kisi, key_indis, {}) | |
} | |
end | |
def bosalt | |
CSV::Writer.generate(File.open($csvname, "w")) do |csv| | |
csv << $kafa | |
$kisiler.each do |no, value| | |
yaz = [] | |
yaz << no | |
$value.each { |v| yaz << value[v]} | |
csv << yaz | |
end | |
end | |
end | |
### DEMO | |
yukle | |
$kisiler.each { |no| puts no + " : " + $kisiler[no] } | |
bosalt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
no | ad | sd | un | |
---|---|---|---|---|
10060264 | zinnur | yeşilyurt | ||
10060265 | baki | savaş | ||
10060266 | kamer kevser | bağlı | ||
10060267 | gökhan | ceylan | ||
10060268 | emre | çavuş |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment