Показать сообщение отдельно

  #2  
Старый 12.12.2009, 05:08
lukmus
Постоянный
Регистрация: 18.11.2009
Сообщений: 709
Провел на форуме:
1410429

Репутация: 214


По умолчанию

на ruby писал для себя, помимо вычитание умеет еще складывать, сортировать, поднимать и опускать регистр
Код:
#!/usr/bin/ruby

n=ARGV.size
if n<=3 or n>5 
  puts "Not enought arguments\n <firstfile> <secondfile> <+|-> [outputfile] [-su<d|U>]"
  exit
end
$fi_file=ARGV[0]
$se_file=ARGV[1]
$sign=ARGV[2]
if $sign!='+' and $sign!='-'
  puts "Unknow argument #{$sign}"
  exit
end


def make (str)
  sort=false
  udcase=0
  uniq=false
  if str.include? 's' then sort=true end
  if str.include? 'd' then udcase=1 else
    if str.include? 'U' then udcase=2 else udcase=0 end
  end
  if str.include? 'u' then uniq=true end
  return sort,udcase,uniq
end

if n>3
  if !(ARGV[3].include?('-s') or ARGV[3].include?('-d') or ARGV[3].include?('-U') or ARGV[3].include?('-u'))
    $outfile=ARGV[3]
  else
    $outfile=$fi_file+$se_file+'_out'
    $sort,$udcase,$uniq=make ARGV[3]
  end

  if n==5 then  $sort,$udcase,$uniq=make ARGV[4] end

end


begin
 f_arr=IO.readlines($fi_file)
rescue => e
  puts "Can't read file #{$fi_file}:#{e}"
end

begin
 s_arr=IO.readlines($se_file)
rescue => e
  puts "Can't read file #{$se_file}:#{e}"
end

f_arr.each {|x| x.chop!}
s_arr.each {|x| x.chop!}

case $sign
when '-'
  r_arr=f_arr-s_arr
when '+'
  r_arr=f_arr+s_arr
end


if $udcase==1 
  puts "Downcase..."
  r_arr.each {|x| x.downcase!} 
end
if $udcase==2
  puts "Upcase..." 
  r_arr.each {|x| x.upcase!} 
end
if $uniq 
  puts "Delete repetitions..."
  r_arr.uniq! 
end
if $sort 
  puts "Sort..."
  r_arr.sort! 
end

begin
  puts "Create output file #{$outfile}"
  File.open($outfile,"w") {|file| r_arr.each {|x| file.puts x }}
rescue => e
  puts "Can't create file #{$outfile}:#{e}"
end
я думаю преобразование в cgi особого труда не составит
 
Ответить с цитированием