Feeds:
Posts
Comments

When I was developing some Rails application, using MySQL as the database, I ran into the task of inserting very large amount of data to the DB, so I’m sharing the sql helper I created to make it faster.

I used mysqlimport, which “reads rows from a text file into a table at a very high speed“, as MySQL docs says.

I took the idea of using mysqlimport in updating bulk records as well inserting new ones, from this link:MySQL – CSV UPDATE (not INSERT) into existing table?

Assume that we are inserting large number of records of User model,

 sql_file_dir = "path/to/some/dir/to/store/sql/file"
 sql_writer = BulkDataWriter.new(User, sql_file_dir)
 alot_of_data.each do |data|
   #......
   user = User.new(user_attributes)
   sql_writer.write_record_to_sql_file(user)
 end
 sql_writer.insert_records_to_database

and here is the helper class

  class SqlWriter

    ID_STR = 'id'
    CREATED_AT_STR = 'created_at'
    UPDATED_AT_STR = 'updated_at'
    NULL_STR = '\N'
    COMMA_STR = ','
    attr_accessor :insert_sql_file, :update_sql_file
    # klass is the class of the records we will deal with
    # sql_dir_path is the directory which will contain the sql data file(text file).
    def initialize(klass, sql_dir_path)
      @klass = klass
      @temp_table_name = "temp_#{klass.table_name}_#{Time.now.to_s(:db).gsub(/-| |:/,'_')}_#{SecureRandom.hex[0..10]}"
      @insert_sql_file = File.new("#{sql_dir_path}/#{klass.table_name}.txt", 'w')
      @update_sql_file = File.new("#{sql_dir_path}/#{@temp_table_name}.txt", 'w')
      @current_time_in_db_format = Time.now.to_s(:db)
      @insert_fields = klass.new.attributes.except(ID_STR).keys
      @update_fields = klass.new.attributes.keys
      @records_need_update = false
    end

    def write_record_to_sql_file(record)
      row_data = get_sql_row(record)
      if record.new_record?
        @insert_sql_file.write("#{row_data}\n")
      else
        @update_sql_file.write("#{row_data}\n")
      end
    end

    def insert_records_to_database
      @insert_sql_file.close
      @update_sql_file.close
      config   = Rails.configuration.database_configuration
      database = config[Rails.env]["database"]
      username = config[Rails.env]["username"]
      password = config[Rails.env]["password"]
      host = config[Rails.env]["host"]
      insert_columns_orders = @insert_fields.join(',')
      `mysqlimport -u #{username} -p#{password} -h #{host} --columns='#{insert_columns_orders}' --local --fields-terminated-by=',' #{database} #{Shellwords.escape(@insert_sql_file.path)}`
      if @records_need_update
        ActiveRecord::Base.connection.execute("CREATE TABLE #{@temp_table_name} LIKE #{@klass.table_name};")
        update_columns_orders = @update_fields.join(',')
        `mysqlimport -u #{username} -p#{password} -h #{host} --columns='#{update_columns_orders}' --local --fields-terminated-by=',' #{database} #{Shellwords.escape(@update_sql_file.path)}`
        set_fields = @insert_fields.map{|field| "#{@klass.table_name}.#{field}=#{@temp_table_name}.#{field}"}.join(',')
        ActiveRecord::Base.connection.execute("UPDATE #{@klass.table_name} INNER JOIN #{@temp_table_name} ON #{@klass.table_name}.id = #{@temp_table_name}.id SET #{set_fields}")
        ActiveRecord::Base.connection.execute("DROP TABLE #{@temp_table_name}")
      end
    end

    private
      def get_sql_row(record)
        if record.new_record?
          result = record.attributes.except(ID_STR).values
          fields = @insert_fields
        else
          result =  record.attributes.values
          fields = @update_fields
          @records_need_update = true
        end
        result.each_with_index do |item, index|
          if item.class == Date || item.class == Time
            result[index] = item.to_s(:db)
          elsif item == true || item == false
            result[index] = item ? 1 : 0
          elsif item == nil
            if fields[index] == CREATED_AT_STR || fields[index] == UPDATED_AT_STR
              result[index] = @current_time_in_db_format
            else
              result[index] = NULL_STR
            end
          end
        end
        result.join(COMMA_STR)
      end
  end

I wrote this script while working atย BadrIT, you can check BadrIT blogย there

I decided to learn PHP, and I thought one interesting way to learn it would be to build a mini MVC framework. I won’t go into details but here are the files๐Ÿ™‚

General lines:

index.php is the router file where all requests go to first, the controller,action -if any- and id-if any- are extracted and the proper action is taken, routing all requests to index.php is made via .htaccess file.

The model file is just a little abstraction, not a lot of functions.

Each model file must extend Model.php and each controller file must extend Controller.PHP, all models reside in the directory “models”, all controllers reside in the directory “controllers”

For each controller, there is a directory with the controller name under “views” directory, each view for each action is a php file in this directory.

There are some conventions I followed in this work: views files location, etc.. I won’t go into details as they can be inferred from the code. ( Tip:ย The controller and Model superclasses are in ‘lib’ directory)

I tried to build a mini site using this MVC framework, but so far I only built the login,logout and register- so you will find a lot of unused files-๐Ÿ™‚ anyway they are quite enough to illustrate the framework idea.

https://github.com/loaighoraba/workspace/tree/master/PHP/mysite

ุณุงูุฑุช ุฅู„ู‰ ุฃู„ู…ุงู†ูŠุง ุฃุฑุจุน ู…ุฑุงุชุŒ ู‚ุถูŠุช ุซู„ุงุซ ุฃุณุงุจูŠุน ููŠ ูƒู„ ู…ู† ุงู„ู…ุฑุชูŠู† ุงู„ุฃูˆู„ุชูŠู†ุŒ ูˆ 6 ุฃุดู‡ุฑ ููŠ ุงู„ู…ุฑุฉ ุงู„ุซุงู„ุซุฉ ูˆุฃู†ุง ุง๏ปตู† ููŠ ย ุงู„ู…ุฑุฉ ุงู„ุฑุงุจุนุฉ. ุจุฏุงูŠุฉ ุงู†ุจู‡ุฑุช ู‡ู†ุงูƒ ุจู†ุธุงู… ุงู„ุญูŠุงุฉ ุงู„ุฏู‚ูŠู‚ ูˆ ุงู„ู…ู†ุธู…ุŒ ูˆ ุฃู† ูƒู„ ุฐูŠ ุญู‚ ูŠุฃุฎุฐ ุญู‚ู‡ ูˆ ู†ุญูˆ ู‡ุฐุงุŒ ูˆู„ูƒู† ุดูŠุฆุง ูุดูŠุฆุง ูŠุฏุฑูƒ ุงู„ู…ุฑุก ุงู„ุญู‚ูŠู‚ุฉ: ุฃู† ุญุฑุต ู‡ุคู„ุงุก ุงู„ู‚ูˆู… ุนู„ู‰ ุงู„ุญู‚ูˆู‚ ูˆ ุงู„ุนุฏู„ุŒ ู„ูŠุณ ุญุจุง ุฎุงู„ุตุง ู„ู‡ุฐู‡ ุงู„ู‚ูŠู… ุฃูˆ ุนู…ู„ุง ู…ู† ุฃุฌู„ู‡ุงุŒ ุจู„ ุญุฑุตุง ุนู„ู‰ ุงู„ุฏู†ูŠุง ูˆ ุชู‚ุงุชู„ุง ุนู„ูŠู‡ุงุŒ ูˆ ุฅูู†ุงุก ู„ู„ุนู…ุฑ ููŠ ุณุจูŠู„ู‡ุงุŒ ููŠ ุญูŠุงุฉ ู…ุงุฏูŠุฉ ู„ุง ุชุทุงู‚ุŒ ู„ุง ุชุณุชุทูŠุน ุฃู† ุชุทู…ุน ู…ู†ู‡ู… ููŠ ุฑุญู…ุฉ. ูู‚ูˆู… ู„ุง ุชุณูŠุทุฑ ุนู„ูŠู‡ู… ุญู‚ูŠู‚ุฉ ุฃู†ู‡ ูŠูˆุฌุฏ ุฅู„ู‡ ู…ุทู„ุน ุณู…ูŠุน ุจุตูŠุฑุŒ ูˆ ุฃู†ู‡ ู‡ู†ุงูƒ ุจุนุฏ ุงู„ู…ูˆุช ูŠูˆู… ูŠุญุงุณุจ ููŠู‡ ุงู„ู†ุงุณุŒ ูˆ ุฃู† ุงู„ุฃุนู…ุงู„ ุงู„ุตุงู„ุญุฉ ุชุซู‚ู„ ู…ูŠุฒุงู†ูƒ ูŠูˆู…ู‡ุงุŒ ูˆ ุฃู† ุงู„ููˆุฒ ุงู„ุญู‚ูŠู‚ูŠ ู‡ูˆ ุงู„ุฌู†ุฉุŒ ู‚ูˆู… ูƒู‡ุคู„ุงุก ู„ุง ูŠุณุชุทูŠุน ุงู„ู…ุฑุก ุฃู† ูŠุนูŠุด ู…ุนู‡ู…. ุญูŠุงุฉ ู…ุงุฏูŠุฉ ูŠุนู…ู„ ุงู„ู…ุฑุก ููŠู‡ุง ูƒุทุงุญูˆู†ุฉ ููŠ ุฌูˆ ู…ู† ุงู„ุณุนุงุฑ ู„ู„ุฏู†ูŠุงุŒ ู„ุง ุชุฌุฏ ู‚ูŠู… ุงู„ุนุทุงุก ูˆ ุงู„ูƒุฑู… ูˆ ู…ูƒุงุฑู… ุงู„ุฃุฎู„ุงู‚.. ู„ุง ูŠูู‡ู…ูˆู† ู…ุนู†ู‰ ุฃู† ุชุฏุงูŠู† ุงู„ู†ุงุณ ูˆ ู„ุง ุชูƒุณุจ ู…ู† ูˆุฑุงุก ุฅู‚ุฑุงุถูƒ ู„ู‡ู…. ูู‡ูˆ ู„ู† ูŠุนูˆุฏ ุนู„ูŠู‡ ุดูŠุฆุง ุฅู† ุฃู‚ุฑุถูƒ ุซู… ุนุงุฏ ู„ู‡ ุงู„ู…ุจู„ุบ ุงู„ุฐูŠ ุฃู‚ุฑุถู‡ ูƒู…ุง ู‡ูˆ ุจุฏูˆู† ุฒูŠุงุฏุฉ. ู„ุง ุชุฌุฏ ู„ู„ุนููˆ ุนู†ุฏู‡ู… ู…ุนู†ู‰ุŒ ูˆ ู„ุง ุฃู† ุชูƒุธู… ุบูŠุธูƒ ูˆ ุฃู†ุช ู‚ุงุฏุฑ ุนู„ู‰ ุฅู†ูุงุฐู‡ุŒ ู„ุง ู…ุนู†ู‰ ู„ู„ุฏูุน ุจุงู„ุชูŠ ู‡ูŠ ุฃุญุณู†ุŒ ูˆ ู„ุง ุฃู† ุชุชุฌุงูˆุฒ ุนู† ุงู„ุณูŠุฆุฉ. ู„ุง ุญูŠุงุฉ ุฑูˆุญูŠุฉ ู‡ู†ุงูƒุŒ ูŠูƒุจุฑ ุงู„ุฃุจ ูˆ ุงู„ุฃู… ูˆ ูŠุฐู‡ุจ ุจู‡ู… ุฃุจู†ุงุคู‡ู… ุฅู„ู‰ ุฏูˆุฑ ุฑุนุงูŠุฉ ุงู„ู…ุณู†ูŠู† ุจุฏู„ุง ุฃู† ูŠูุณูƒูู†ูˆู‡ู…ุง ู…ุนู‡ู…. ู…ุฌุชู…ุน “ุจู„ุงุณุชูŠูƒ” ุจู„ุง ุณุนุงุฏุฉ ุนู„ู‰ ุงู„ุฅุทู„ุงู‚ุŒ ูŠุญุงูˆู„ูˆู† ุงู„ุญุตูˆู„ ุนู„ูŠู‡ุง ููŠ ู…ู„ุฐุงุชู‡ู… ุงู„ุฏู†ูŠูˆูŠุฉ ูƒุงู„ุฎู…ุฑ ูˆ ุงู„ู†ุณุงุก. ู„ุง ูŠุนูˆู† ู‚ูˆู„ู‡ ุชุนุงู„ู‰ :”ุงู„ู…ุงู„ ูˆุงู„ุจู†ูˆู† ุฒูŠู†ุฉ ุงู„ุญูŠุงุฉ ุงู„ุฏู†ูŠุง ูˆุงู„ุจุงู‚ูŠุงุช ุงู„ุตุงู„ุญุงุช ุฎูŠุฑ ุนู†ุฏ ุฑุจูƒ ุซูˆุงุจุง ูˆุฎูŠุฑ ุฃู…ู„ุง” ุŒ ” ุงุฏูุน ุจุงู„ุชูŠ ู‡ูŠ ุฃุญุณู†”ุŒ ” ูู…ู† ุนูุง ูˆ ุฃุตู„ุญ ูุฃุฌุฑู‡ ุนู„ู‰ ุงู„ู„ู‡”. ุฃุฐูƒุฑ ุฃู†ู†ูŠ ุฌู„ุณุช ุฐุงุช ู…ุฑุฉ ุฃู†ุง ูˆ ุงุซู†ูŠู† ู…ู† ุฒู…ู„ุงุฆูŠ ู…ุน ุฏูƒุชูˆุฑ ููŠ ุงู„ุฌุงู…ุนุฉ ุงู„ุฃู„ู…ุงู†ูŠุฉ ุนู„ู‰ ู…ุงุฆุฏุฉ ุนุดุงุก ููŠ ู…ุตุฑ-ูุฑุถ ู‡ูˆ ู†ูุณู‡ ุนู„ูŠู†ุง- ูˆ ุฃุฎุฐ ุงู„ุฑุฌู„ ูŠุชูƒู„ู… ุนู† ุฃู†ู‡ ุณุนูŠุฏ ุจุงู„ุญูŠุงุฉ ููŠ ู…ุตุฑุŒ ูˆุฃู†ู‡ ุนู†ุฏู…ุง ูŠุนูˆุฏ ู„ุฃู„ู…ุงู†ูŠุง ู…ุคู‚ุชุง ูŠุชุญุฑู‚ ุดูˆู‚ุง ู„ู„ุนูˆุฏุฉ ุฅู„ู‰ ู…ุตุฑ ูˆ ุฃุฎุฐ ูŠุดูƒูˆ ุงู„ุญูŠุงุฉ ููŠ ุฃู„ู…ุงู†ูŠุง ุจู…ุงุฏูŠุชู‡ุง ุงู„ู…ูุฑุทุฉ ูˆุงู†ุนุฏุงู… ุงู„ุฑูˆุญ ููŠู‡ุง. ู„ู… ุฃูƒู† ู‚ุฏ ุณุงูุฑุช ุฅู„ู‰ ุฃู„ู…ุงู†ูŠุง ูˆู‚ุชู‡ุง ูู„ู… ุฃุณุชุทุน ุฃู† ุฃุชูู‡ู… ูƒู„ุงู…ู‡ ุชู…ุงู…ุงุŒ ูˆู„ูƒู† ุจุนุฏ ุฃู† ุณุงูุฑุช ู‡ู†ุงูƒ ุฃุฏุฑูƒุช ุตุฏู‚ ุงู„ุฑุฌู„.

ู‚ุฏ ูŠู‚ูˆู„ ู‚ุงุฆู„: ูˆู„ูƒู† ู„ุง ูŠูˆุฌุฏ ููŠ ู…ุตุฑ ุฃูŠุถุง ู‡ุฐู‡ ุง๏ปทุฎู„ุงู‚ุŒ ูู…ุง ุงู„ูุฑู‚ ุจูŠู†ู†ุง ูˆุจูŠู†ู‡ู…ุŒ ุฃู‚ูˆู„ ู„ูƒ: ู‡ู†ุงูƒ ุทุงุฆูุฉ ู…ู† ุงู„ู…ุตุฑูŠูŠู† ู„ุฏูŠู‡ู… ู†ุณุจุฉ ุนุงู„ูŠุฉ ู…ู† ู‡ุฐู‡ ุง๏ปทุฎู„ุงู‚ ูˆุง๏ปนู†ุณุงู†ูŠุฉุŒ ูˆู‡ุฐู‡ ุงู„ุทุงุฆูุฉ ู‡ูŠ ุงู„ุชูŠ ุชุฌุนู„ ุงู„ู…ุฑุก ูŠุชุญู…ู„ ุงู„ู…ุนูŠุดุฉ ููŠ ู…ุตุฑุŒ .. ุจุฎู„ุงู ุฃู† ุงู„ู…ุฌุชู…ุน ุงู„ุฐูŠ ูŠู‚ุฑุฃ ููŠู‡ ุงู„ู‚ุฑุขู† ูŠูˆุฌุฏ ุนู†ุฏู‡ ุดูŠุก ู„ุง ูŠูˆุฌุฏ ููŠ ุงู„ู…ุฌุชู…ุน ุงู„ุฎุงู„ูŠ ู…ู† ุงู„ู‚ุฑุขู†.. ูˆู‡ูˆ ู…ุง ู„ุง ูŠู…ูƒู† ุงู„ุชุนุจูŠุฑ ุนู†ู‡ ุจุงู„ู„ุณุงู† ูˆู„ูƒู† ุจุงู„ุชุฌุฑุจุฉ.

 

ูˆ ู‚ุฏ ูˆุฌุฏุช ุฃู† ุงู„ุดู‡ูŠุฏ -ุจุฅุฐู† ุงู„ู„ู‡- ุณูŠุฏ ู‚ุทุจ ู‚ุฏ ุนุจุฑ ู…ุง ุฃุฑุฏุช ู‚ูˆู„ู‡ ููŠ ุจูŠุงู† ู„ุบูˆูŠ ุฑุงุฆุน ูƒุนุงุฏุชู‡ุŒ ูู†ู‚ุชุจุณ ุตูุญุงุช ู…ู† ูƒุชุงุจู‡ “ู†ุญูˆ ู…ุฌุชู…ุน ุฅุณู„ุงู…ูŠ” :

ุฃุฏุฑูŠ ูƒูŠู ูŠุนูŠุด ุงู„ู†ุงุณ ููŠ ุฃู…ุฑูŠูƒุง . ุจู„ุฏ ุงู„ุฅู†ุชุงุฌ ุงู„ูุฎู… ูˆุงู„ุซุฑุงุก ุงู„ูุงุญุด ูˆุงู„ู„ุฐุงุฆุฐ ุงู„ู…ุจุงุญุฉ . . ู„ู‚ุฏ ุดู‡ุฏุชู‡ู… ู‡ู†ุงู„ูƒ ูˆุงู„ู‚ู„ู‚ ุงู„ุนุตุจูŠ ูŠุฃูƒู„ ุญูŠุงุชู‡ู… ุนู„ู‰ ุงู„ุฑุบู… ู…ู† ูƒู„ ู…ุธุงู‡ุฑ ุงู„ุซุฑุงุก ูˆุงู„ู†ุนู…ุฉ ูˆูˆุณุงุฆู„ ุงู„ุฑุงุญุฉ . ุฅู† ู…ุชุงุนู‡ู… ู‡ูŠุงุฌ ุนุตุจูŠ ูˆู…ุฑุญ ุญูŠูˆุงู†ูŠ ูˆุฅู†ู‡ ูŠุฎูŠู„ ุงู„ูŠูƒ ุฃู†ู‡ู… ู‡ุงุฑุจูˆู† ุฏุงุฆู…ุงู‹ ู…ู† ุฃุดุจุงุญ ุชุทุงุฑุฏู‡ู… ุŒ ุฅู†ู‡ู… ุงู„ุขุช ุชุชุญุฑูƒ ููŠ ุฌู†ูˆู† ูˆุณุฑุนุฉ ูˆู‡ูŠุงุฌ ู„ุง ูŠู‚ุฑ ู„ู‡ ู‚ุฑุงุฑ . ูˆูƒุซูŠุฑุงู‹ ู…ุง ูƒุงู† ูŠุฎูŠู„ ุฅู„ูŠ ุฃู† ุงู„ู†ุงุณ ู‡ู†ุงูƒ ููŠ ุทุงุญูˆู†ุฉ ุฏุงุฆุฑุฉ ู„ุง ุชู†ูŠ ู„ูŠู„ ู†ู‡ุงุฑ ุŒ ุตุจุงุญ ู…ุณุงุก ุŒ ุชุทุญู† ุจู‡ู… ูˆูŠุทุญู†ูˆู† ุŒ ู„ุง ูŠู‡ุฏุฃูˆู† ู„ุญุธุฉ . ูˆู„ุง ูŠุทู…ุฆู†ูˆู† ุฅู„ู‰ ุฃู†ูุณู‡ู… ูˆู„ุง ุฅู„ู‰ ุงู„ุญูŠุงุฉ ู…ู† ุญูˆู„ู‡ู… โ€“ ุฅู† ูƒุงู†ูˆุง ูŠุญุณูˆู† ู…ุง ุญูˆู„ู‡ู… โ€“ ู„ูŠุณุช ู‡ู†ุงู„ูƒ ู„ุญุธุฉ ู„ู„ุชุฃู…ู„ ุŒ ูˆู„ุง ุญุชู‰ ู„ู„ุดุนูˆุฑ ุจุงู„ุญูŠุงุฉ ุฐุงุชู‡ุง ูˆู‡ูŠ ุชุฏูˆุฑ ุญุชู‰ ุฃูˆู‚ุงุช ุฑุงุญุชู‡ู… ูˆุฑูŠุงุถู‡ู… ููŠ ุงู„ู…ู†ุชุฒู‡ุงุช ูˆุงู„ุบุงุจุงุช ูˆุนู„ู‰ ุดูˆุงุทูŠุก ุงู„ุฃู†ู‡ุงุฑ ูˆุงู„ุจุญูŠุฑุงุช . . . ุชุฑุงู‡ู… ููŠู‡ุง ูู†ุญุณ ุฃู†ู‡ู… ููŠ ” ุดุบู„ ุ› ” ูƒุฃูŠ ุดุบู„ ุฎู„ุงู„ ุงู„ุนู…ู„ ุŒ ูˆูƒู„ ู…ุง ู‡ู†ุงู„ูƒ ู…ู† ูุงุฑู‚ ุฃู†ู‡ู… ููŠ ู…ูƒุงู† ุบูŠุฑ ุงู„ู…ูƒุงู† ุŒ ูˆููŠ ุนู…ู„ ุบูŠุฑ ุงู„ุนู…ู„ . ูˆู„ูƒู† ู„ุง ุฑุงุญุฉ ูˆู„ุง ู‡ุฏูˆุก ูˆู„ุง ุชุฃู…ู„ ุŒ ูˆู„ุง ุงุทู…ุฆู†ุงู† .

ุฅู†ู‡ู… ูŠู†ุชุฌูˆู† ูƒุซูŠุฑุงู‹ . ู…ุง ููŠ ุฐู„ูƒ ุดูƒ . ุฅู†ู‡ู… ูŠูƒุณุจูˆู† ูƒุซูŠุฑุงู‹ ู…ุง ููŠ ู‡ุฐุง ุดูƒ ุฃูŠุถุงู‹ ูˆู„ูƒู† ู„ู…ู† ูŠู†ุชุฌูˆู† ูˆู„ู…ู† ูŠูƒุณุจูˆู† ุŸ ู„ุฐุงุช ุงู„ูƒุณุจ ูˆู„ุฐุงุช ุงู„ุงู†ุชุงุฌ ุ› ุงู„ุนู†ุตุฑ ุงู„ุฅู†ุณุงู†ูŠ ู„ุง ูˆุฌูˆุฏ ู„ู‡ ุŒ ุชุฃู…ู„ ุฐู„ูƒ ุงู„ูƒุณุจ ูˆุฐู„ูƒ ุงู„ุงู†ุชุงุฌ ุงู„ุงุญุณุงุณ ุจุฏูˆุงูุนู‡ ูˆู†ุชุงุฆุฌู‡ ููŠ ูŠู‚ุธุฉ ููƒุฑ ูˆุญุณุงุณูŠุฉ ู‚ู„ุจ ุŒ ุชุฐูˆู‚ู‡ ุจุญุณ ุงู„ุฅู†ุณุงู† ุงู„ู…ุชู…ูŠุฒ ุนู† ุญุณ ุงู„ุขู„ุฉ . . ูƒู„ ุฐู„ูƒ ู„ุง ุชู„ู…ุญู‡ ููŠ ุณูŠู…ุง ูˆุฌู‡ ูˆู„ุง ููŠ ุชุนุจูŠุฑ ู„ุณุงู† !

ุฅู†ู‡ุง ุงู„ุทุงุญูˆู†ุฉ ุงู„ุฏุงุฆุฑุฉ ู„ูŠู„ ู†ู‡ุงุฑ : ุชุทุญู† ุŒ ูˆุชุจุนุซุฑ ู…ุง ุชุทุญู†ู‡ . ูˆุชุฌู…ุนู‡ ู…ุฑุฉ ุฃุฎุฑู‰ ู„ุชุทุญู†ู‡ ู…ู† ุฌุฏูŠุฏ ! ูˆุงู„ู†ุงุณ ูˆุงู„ุฃุดูŠุงุก ูˆุงู„ุฒู…ุงู† ูˆุงู„ู…ูƒุงู† . . ูƒู„ู‡ุง ุชุฏูˆุฑ ููŠ ุชู„ูƒ ุงู„ุทุงุญูˆู†ุฉ ุงู„ุฏุงุฆุฑุฉ ุงู„ุชูŠ ู„ุง ุชูƒู„ ูˆู„ุง ุชู…ู„ ุŒ ูˆู„ุง ุชูƒู ู„ุญุธุฉ ุนู† ุงู„ุฏูˆุฑุงู† . .

ุฅู†ู‡ ุงู„ุฏูˆุงุฑ ! ! !

ู‡ุฏูˆุก ุงู„ู‚ู„ุจ . ุงุทู…ุฆู†ุงู† ุงู„ู†ูุณ . ุฑุงุญุฉ ุงู„ุถู…ูŠุฑ . ู„ุฐุฉ ุงู„ูุฑุญ ุงู„ูŠู‚ุธ ุจุซู…ุฑุงุช ุงู„ุฌู‡ุฏ ูˆุงู„ุงุฑุชูŠุงุญ . ุงู„ู…ูˆุฏุงุช ุงู„ุญู„ูˆุฉ ุจูŠู† ุงู„ู†ุงุณ ุงู„ุชุฌุงูˆุจ ุงู„ุฑูˆุญูŠ ุจูŠู† ุงู„ุงุตุฏู‚ุงุก . ุงู„ุงู‡ุชู…ุงู…ุงุช ุงู„ู†ุงุดุฆุฉ ุนู† ุงู„ูˆุดุงุฆุฌ ุงู„ูˆุซูŠู‚ุฉ ููŠ ุงู„ุงุณุฑุฉ ุชู„ูƒ ุงู„ู…ุดุงุนุฑ ุงู„ุชูŠ ุชุดุนุฑ ุงู„ูุฑุฏ ุฃู†ู‡ ู„ูŠุณ ูˆุญุฏู‡ . ูˆุชู…ู†ุญู‡ ุงู„ุซู‚ุฉ ูˆุงู„ุทู…ุฃู†ูŠู†ุฉ ูˆุงู„ุฑุงุญุฉ ุจุนุฏ ุงู„ุฌู‡ุฏ ูˆุงู„ูƒุฏ ูˆุงู„ุนู†ุงุก ุงู„ุนู‚ูŠุฏุฉ ููŠ ู‚ูˆุฉ ุฃูƒุจุฑ ู…ู† ู‚ูˆุฉ ุงู„ุฃุฑุถ ุŒ ุชู„ูƒ ุงู„ุนู‚ูŠุฏุฉ ุงู„ุชูŠ ุชุดุนุฑ ุงู„ูุฑุฏ ุฃู†ู‡ ู„ูŠุณ ุฐุฑุฉ ุชุงุฆู‡ุฉ ููŠ ู‡ุฐุง ุงู„ูƒูˆู† ุงู„ุนุฑูŠุถ ุจู„ุง ุฃุตู„ ูˆู„ุง ู‚ุฑุงุฑ . . ูƒู„ ู‡ุฐุง ู„ุง ูˆุฌูˆุฏ ู„ู‡ ููŠ ู‚ุงู…ูˆุณ ุงู„ุญูŠุงุฉ ุงู„ุฃู…ุฑูŠูƒูŠุฉ ุŒ ูˆู„ุง ููŠ ู…ุญูŠุท ุงู„ู†ูุณ ุงู„ุฃู…ุฑูŠูƒูŠุฉ .

ุฅู†ู‡ ุงู„ุฎูˆุงุก ! ! !

ุงู„ุฎูˆุงุก ุนู„ู‰ ุงู„ุฑุบู… ู…ู…ุง ูŠุจุฏูˆ ู…ู† ุฒุญู…ุฉ ููŠ ุงู„ุญูŠุงุฉ ูˆุงู…ุชู„ุงุก .

ู‡ู†ุงู„ูƒ ู…ุฑุญ ูƒุซูŠุฑ ุŒ ูŠุฎูŠู„ ุฅู„ู‰ ู…ู† ู„ุง ูŠุนุฑู ุฃู†ู‡ ุณุนุงุฏุฉ . . . ุชู„ูƒ ุงู„ุถุญูƒุงุช ุงู„ุชูŠ ุชุฑู† ููŠ ุงู„ู‡ูˆุงุก . ุชู„ูƒ ” ุงู„ู…ู‡ุงุฑุดุงุช ” ุงู„ุชูŠ ุชุชุญุณุณ ู…ุณุงู‚ุท ุงู„ู„ุฐุฉ ููŠ ุงู„ุฃุฌุณุงุฏ . ุชู„ูƒ ุงู„ูƒุคูˆุณ ุงู„ุชูŠ ู„ุง ุชูุฑุบ ู…ู† ุงู„ุฎู…ุฑ ุŒ ุชู„ูƒ ุงู„ุถุฌุฉ ุงู„ุชูŠ ู„ุง ุชู‡ุฏุฃ ูˆู„ุง ุชุณูƒู† . . ูˆู„ูƒู†ู‡ ุงู„ู…ุฑุญ ุงู„ุญูŠูˆุงู†ูŠ ู„ุง ุงู„ุณุนุงุฏุฉ ุŒ ูˆู„ุง ุงู„ูุฑุญ ุŒ ุฅุฐ ุนุฑุจุฏุฉ ุงู„ุณูƒุงุฑู‰ ู„ูŠุณุช ุณุนุงุฏุฉ ุŒ ูƒุฐู„ูƒ ุงู„ู…ุฑุญ ุงู„ุญูŠูˆุงู†ูŠ ู„ูŠุณ ูุฑุญุงู‹ ุŒ ุฅู†ู‡ ุงู†ุทู„ุงู‚ ุงู„ุทุงู‚ุฉ ุงู„ู…ูƒุจูˆุชู‡ ุชุญุช ุถุบุท ุงู„ุนู…ู„ ุงู„ู…ุฑู‡ู‚ . ุฅู†ู‡ุง ู‚ุฑู‚ุนุฉ ูƒู‚ุฑู‚ุนุฉ ุงู„ุขู„ุงุช ู„ุชูุฑูŠุบ ุงู„ุจุฎุงุฑ . . .

ูˆู„ูƒู† ุฃูŠู† ุงู„ุฅู†ุณุงู† ุŸ ููŠ ูƒู„ ู‡ุฐุง ุงู„ุฑูƒุงู… ุŸ ุฃูŠู† ุงู„ุฅู†ุณุงู† ุงู„ู…ุชู…ูŠุฒ ุนู† ุงู„ุขู„ุฉ ูˆุนู† ุงู„ุญูŠูˆุงู† ุŸ ูˆู„ุณุช ุงุชุตูˆุฑ ู…ู† ูˆุฑุงุก ุงู„ูู„ุณูุฉ ุงู„ู…ุงุฏูŠุฉ ููŠ ุฑูˆุณูŠุง ุฅู„ุง ุญูŠุงุฉ ุฃุญุท ู…ู† ุชู„ูƒ ุงู„ุญูŠุงุฉ . ูุญุชู‰ ุฐู„ูƒ ุงู„ู…ุฑุญ ุงู„ุญูŠูˆุงู†ูŠ ุงู„ู†ุงุดูŠุก ู…ู† ุงู„ุทู„ุงู‚ุฉ ูˆุงู„ุซุฑุงุก ููŠ ุงู…ุฑูŠูƒุง ู„ุง ุฃุชุตูˆุฑู‡ ู‡ู†ุงูƒ ุ› ูˆููŠ ู‡ุฐุง ุงู„ุฏุฑูƒ ุชุณุชู‚ุฑ ุงู„ุจุดุฑูŠุฉ ุงู„ูŠูˆู… ููŠ ุงู„ุดุฑู‚ ูˆููŠ ุงู„ุบุฑุจ ุณูˆุงุก .

ุฅู† ุงู„ุจุดุฑูŠุฉ ูƒู„ู‡ุง ููŠ ุญุงุฌุฉ ุงู„ูŠู†ุง : ููŠ ุญุงุฌุฉ ุฅู„ู‰ ุนู‚ูŠุฏุฉ ููŠ ุงู„ุถู…ูŠุฑ ุŒ ูŠุณุชุฑูˆุญ ููŠ ุธู„ู‡ุง ู…ู† ู‡ุฐุง ุงู„ู‡ุฌูŠุฑ ุงู„ู‚ุงุฆุธ . ูˆูŠุทู…ุฆู† ููŠ ุฑุญุงุจู‡ุง ู…ู† ุฐู„ูƒ ุงู„ู‚ู„ู‚ ุŒ ูˆูŠุณุชู‚ุฑ ููŠ ุญุถู†ู‡ุง ุฅู„ู‰ ู‚ุฑุงุฑ”.

ูˆ ุฎุชุงู…ุง: ุงู„ู„ู‡ู… ุงุฌุนู„ู†ุง ู‡ุฏุงุฉ ู„ู„ุฅู†ุณุงู†ูŠุฉ ููŠ ุจุญุซู‡ุง ุนู† ุญู‚ูŠู‚ุฉ ุงู„ุญูŠุงุฉ ูˆ ุงู„ุฅู†ุณุงู†ุŒ ูˆ ุฃู† ู†ุฎุฑุฌู‡ุง ู…ู† ุญูŠุฑุชู‡ุง ูˆ ุชุฎุจุทู‡ุง ุฅู„ู‰ ู†ูˆุฑ ุงู„ุฅุณู„ุงู… ูˆ ุฏูุก ุงู„ุฅูŠู…ุงู†. ุงู„ู„ู‡ู… ุขู…ูŠู†.

In some task today I had to use zigzag scanning on a grid rather than normal traversal , this is an illustration of this kind of scanning.

I thought of sharing the code I wrote so that anyone can use this ready-made one..I don’t claim it is the best nor the most elegant๐Ÿ™‚

C++ source code :


/**
 * traverses m*n grid in zigzag order
 */
void traverseZigZag(int m, int n) {

	int i = 0, j = 0, up=1;
	bool turned = false;
	int d[2][2] = { { 1, -1 }, { -1, 1 } };
	int corner[2][4] = { { 1, 0, 0, 1 }, { 0, 1, 1, 0 } };
	while (i < m && j < n) {
		//here you have your (i,j), do what you want with them.
        printf("%d %d\n",i,j);
		if (i == 0 || j == 0 || i == m - 1 || j == n - 1) {
			if (!turned) {
				int k = 2 * (up * (j / (n - 1)) | (1 - up) * (i / (m - 1)));
				i += corner[up][k];
				j += corner[up][k + 1];
				turned = true;
				up = 1 - up;
				continue;
			} else
				turned = false;
		}
		i += d[up][0];
		j += d[up][1];
	}
}