Loop (ÆÄÀÏ ³»¿ë Àбâ)

ÅؽºÆ® ÆÄÀÏ¿¡¼­ ÇÑ ¹ø¿¡ ÇÑ ÁÙ¾¿ ¿­¶÷ÇÕ´Ï´Ù (FileReadLineº¸´Ù ¼öÇ༺´ÉÀÌ ÁÁ½À´Ï´Ù).

Loop, Read, InputFile [, OutputFile]

¸Å°³º¯¼ö

Read

ÀÌ ¸Å°³º¯¼ö´Â ¹Ýµå½Ã ´Ü¾î READÀ̾î¾ß ÇÕ´Ï´Ù.

InputFile

±×ÀÇ ³»¿ëÀÌ È¸µ¹ÀÌ¿¡ ÀÐÇô Áú ÅؽºÆ® ÆÄÀÏÀÇ À̸§. Àý´ë °æ·Î°¡ ÁöÁ¤µÇ¾î ÀÖÁö ¾ÊÀ¸¸é %A_WorkingDir%¿¡ ÀÖ´Ù°í °£Áֵ˴ϴÙ. Windows¿Í Unix Çü½ÄÀ» Áö¿øÇÕ´Ï´Ù; Áï, ÆÄÀÏ ÁÙÀº ij¸®Áö ¸®ÅÏ°ú ¶óÀÎÇǵå·Î (`r`n) ³¡³ª°Å³ª ¾Æ´Ï¸é ±×³É ¶óÀÎÇǵå·Î (`n) ³¡³¯ ¼ö ÀÖ½À´Ï´Ù.

OutputFile

(¼±ÅÃÀû) ȸµ¹À̸¦ ÇÏ´Â µ¿¾È ¿­¾î µÑ ÆÄÀÏÀÇ À̸§. Àý´ë °æ·Î¸¦ ÁöÁ¤ÇÏÁö ¾ÊÀ¸¸é %A_WorkingDir%¿¡ ÀÖ´Ù°í °£Áֵ˴ϴÙ.

ȸµ¹ÀÌ ¸öü ¾È¿¡¼­ ¸Å°³ º¯¼ö Çϳª¸¸ (¾º¿©Áú ÅؽºÆ®¸¸) °¡Áö°í FileAppend ¸í·É¾î¸¦ »ç¿ëÇϸé ÀÌ Æ¯º°ÇÑ ÆÄÀÏ¿¡ Ãß°¡ÇÒ ¼ö ÀÖ½À´Ï´Ù. ÀÌ·± ¹æ½ÄÀ¸·Î ÆÄÀÏ¿¡ Ãß°¡Çϸé FileAppend¸¦ ¸Å°³º¯¼ö 2 °³¿Í ÇÔ²² »ç¿ëÇÏ´Â °Íº¸´Ù ´õ ¼öÇ༺´ÉÀÌ ÁÁ½À´Ï´Ù. ¿Ö³ÄÇϸé ÆÄÀÏÀ» °¢ ¿¬»ê¸¶´Ù ´Ý°í ´Ù½Ã ¿­ ÇÊ¿ä°¡ ¾ø±â ¶§¹®ÀÔ´Ï´Ù. ÀØÁö ¸»°í ÅؽºÆ® ³¡¿¡ ¶óÀÎÇǵ带 (`n) Ãß°¡ÇϽʽÿÀ.

¾Æ¹«°Íµµ ¾º¿©ÁöÁö ¾ÊÀ¸¸é ÆÄÀÏÀº ¿­¸®Áö ¾Ê½À´Ï´Ù. ÀÌ·± ÀÏÀº Loop°¡ ÀüÇô ¹Ýº¹À» ¼öÇàÇÏÁö ¾Ê°Å³ª FileAppend ¸í·É¾î¸¦ ÀüÇô »ç¿ëÇÏÁö ¾ÊÀ» ¶§ ÀϾ´Ï´Ù.

ÀÌÁø ¸ðµå: ÅؽºÆ® ¸ðµå°¡ ¾Æ´Ï¶ó ÀÌÁø ¸ðµå·Î Ãß°¡ÇÏ·Á¸é, ÆÄÀÏ À̸§ ¾Õ¿¡ º°Ç¥¸¦ ¹èÄ¡ÇϽʽÿÀ. ÀÌ·¸°Ô ÇÏ¸é °¢ ¶óÀÎÇÇµå ¹®ÀÚ°¡ (`n) À©µµ¿ìÁîÀÇ Ç¥ÁØ CR+LF°¡ ¾Æ´Ï¶ó ´ÜÀÏ ¶óÀÎÇǵå·Î ¾º¿©Áý´Ï´Ù (LF). ¿¹¸¦ µé¾î: *C:\My Unix File.txt. º°Ç¥°¡ ¾ø´õ¶óµµ, FileAppend¸¦ óÀ½ »ç¿ëÇÒ ¶§ Loop¿¡¼­ ij¸®Áö ¸®ÅÏ°ú ¶óÀÎÇǵ带 ¾²¸é (`r`n) ÀÌÁø ¸ðµå°¡ ÀÚµ¿À¸·Î ÄÑÁý´Ï´Ù(?).

Ç¥ÁØ Ãâ·Â (stdout): º°Ç¥¸¦ (*) OutputFile¿¡ ÁöÁ¤Çϸé FileAppend°¡ ¾²´Â ¸ðµç ÅؽºÆ®´Â Ç¥ÁØ Ãâ·ÂÀ¸·Î Àü¼ÛµË´Ï´Ù (stdout). ±×·± Ãâ·ÂÀ» ÆÄÀÏ¿¡ ¹æÇâÀüȯÇϰųª, ¶Ç´Ù¸¥ EXE·Î ÆÄÀÌÇÁ ó¸®Çϰųª, ¶Ç´Â ¸ÚÁø ÅؽºÆ® ÆíÁý±â·Î ³ªÆ÷ÇÒ ¼ö´Â ÀÖÁö¸¸, ±âµ¿µÈ ¸í·É¾î ÇÁ·ÒÇÁÆ®¿¡ ½ºÅ©¸³Æ®°¡ ³ªÅ¸³ªÁö ¾Ê½À´Ï´Ù. ´õ ÀÚ¼¼ÇÑ °ÍÀº FileAppend¸¦ ÂüÁ¶ÇϽʽÿÀ.

ÇÇ½ÅµÈ ½°Ç¥: ´ëºÎºÐÀÇ ´Ù¸¥ ¸í·É¾îÀÇ ¸¶Áö¸· ¸Å°³º¯¼ö¿Í ´Ù¸£°Ô, OutputFile¿¡¼­ ½°Ç¥´Â ¹Ýµå½Ã ÇǽŽÃÄÑ¾ß ÇÕ´Ï´Ù (`,).

³íÆò

ÆÄÀÏ-Àбâ ȸµ¹ÀÌ´Â ÅؽºÆ® ÆÄÀÏ¿¡ µç °¢ ÁÙÀ» ÇÑ ¹ø¿¡ Çϳª¾¿ ó¸®ÇÒ ¶§ À¯¿ëÇÕ´Ï´Ù. FileReadLineÀ» »ç¿ëÇÏ´Â °Íº¸´Ù ´õ ¼öÇ༺´ÉÀÌ ÁÁ½À´Ï´Ù. ¿Ö³ÄÇá¸é: 1) ¿¬»ê ±â°£µ¿¾È ÆÄÀÏÀÌ ¿­¸° »óÅ·ΠÀÖÀ» ¼ö ÀÖ½À´Ï´Ù; ±×¸®°í 2) ±× ÆÄÀÏÀº ¿äûµÈ ÁÙ ¹øÈ£Ãú ã±â À§ÇØ ¸Å¹ø ´Ù½Ã ½ºÄµÇÒ ÇÊ¿ä°¡ ¾ø½À´Ï´Ù.

³»Àå º¯¼ö A_LoopReadLineÀº ¸ðµç ÆÄÀÏ-Àбâ ȸµ¹ÀÌ ¾È¿¡ Á¸ÀçÇÕ´Ï´Ù. ¾È¿¡ ÇöÀç ÁÙÀÇ ³»¿ëÀÌ ´ã°Ü ÀÖ½À´Ï´Ù. ÁÙ ³¡À» Ç¥½ÄÇϴ ij¸®Áö ¸®ÅÏ°ú ¶óÀÎÇǵå´Â Á¦¿ÜµÇ¾î ÀÖ½À´Ï´Ù (`r`n). ³»ºÎ ÆÄÀÏ-Àбâ ȸµ¹ÀÌ°¡ ¹Ù±ù ÆÄÀÏ-Àбâ ȸµ¹ÀÌ·Î µÑ·¯ ½Î¿© ÀÖ´Ù¸é, °¡Àå ¾ÈÂÊ È¸µ¹ÀÌÀÇ ÆÄÀÏ-ÁÙÀÌ ¿ì¼± ¼øÀ§¸¦ °®½À´Ï´Ù.

ÁÙ ´ç 65,534 ¹®ÀÚ ±æÀ̱îÁö ÀÐÀ» ¼ö ÀÖ½À´Ï´Ù. ÁÙ ±æÀÌ°¡ ÀÌ Á¦ÇÑÀ» ÃÊ°úÇϸé, ³ª¸ÓÁö ¹®ÀÚµéÀ» ´ÙÀ½ ȸµ¹ÀÌ ¶§ ÀÐÈü´Ï´Ù.

InputFileÀ¸·ÎºÎÅÍ ¿­¶÷µÈ °¢ ÁÙÀÇ ³»¿ëÀ» ºÐÇØÇϱâ À§ÇØ StringSplit ¶Ç´Â ÆÄ½Ì È¸µ¹ÀÌ°¡ Á¾Á¾ »ç¿ëµË´Ï´Ù. ¿¹¸¦ µé¾î, InputFileÀÇ °¢ ÁÙÀÌ ÅÇÀ¸·Î-±¸ºÐµÈ ÀÏ·ÃÀÇ Çʵå·Î ±¸¼ºµÇ¾î ÀÖ´Ù¸é, ±× ÇʵåµéÀº ´ÙÀ½ ¿¹Á¦¿Í °°ÀÌ °³º°ÀûÀ¸·Î ¿­¶÷ÇÒ ¼ö ÀÖ½À´Ï´Ù:

Loop, read, C:\Database Export.txt
{
    Loop, parse, A_LoopReadLine, %A_Tab%
    {
        MsgBox, Field number %A_Index% is %A_LoopField%.
    }
}

Àüü ÆÄÀÏÀ» º¯¼ö ¾ÈÀ¸·Î ÀûÀçÇÏ·Á¸é, FileRead¸¦ »ç¿ëÇϽʽÿÀ. ¿Ö³ÄÇϸé ȸµ¹À̺¸´Ù ¼öÇ༺´ÉÀÌ (ƯÈ÷ ¹æ´ëÇÑ ÆÄÀÏ¿¡ ´ëÇÏ¿©) ÈξÀ ´õ ÁÁ±â ¶§¹®ÀÔ´Ï´Ù.

¿©·¯ ÆÄÀÏÀ» µ¿½Ã¿¡ ¿­·Á¸é, ´ÙÀ½ ¿¹Á¦¿¡ º¸¿©ÁÖ´Â ¹Ù¿Í °°ÀÌ DllCall()¸¦ »ç¿ëÇϽʽÿÀ.

Blocks, Break, Continue, ±×¸®°í (¸ðµç À¯ÇüÀÇ È¸µ¹ÀÌ¿¡ Á¸ÀçÇÏ´Â) A_Index º¯¼ö¿¡ °üÇÑ Á¤º¸´Â Loop¸¦ ÂüÁ¶ÇϽʽÿÀ.

¹ÙÀÌÆ® ¼ø¼­ Ç¥½ÄÀÌ ¾øÀ» ¶§ ÆÄÀÏÀ» ¾î¶»°Ô µðÄÚµåÇÒÁö Á¦¾îÇÏ·Á¸é FileEncodingÀ» »ç¿ëÇϽʽÿÀ.

°ü·Ã Ç׸ñ

FileEncoding, FileOpen/File Object, FileRead, FileReadLine, FileAppend, Sort, Loop, Break, Continue, Blocks, FileSetAttrib, FileSetTime

¿¹Á¦

; ¿¹Á¦ #1: 1st ÆÄÀÏ¿¡¼­ ´Ü¾î FAMILY°¡ µç Áٵ鸸 2nd ÆÄÀÏ¿¡ ¾º¿©Áý´Ï´Ù.
; ±âÁ¸ÀÇ ÆÄÀÏ¿¡ Ãß°¡ÇÏ´Â°Ô ¾Æ´Ï¶ó µ¤¾î¾²·Á¸é ù ÁÙÀ» ÁÖ¼® ÇØÁ¦ÇϽʽÿÀ.
;FileDelete, C:\Docs\Family Addresses.txt

Loop, read, C:\Docs\Address List.txt, C:\Docs\Family Addresses.txt
{
    IfInString, A_LoopReadLine, family, FileAppend, %A_LoopReadLine%`n
}

 

; ¿¹Á¦ #2: ÅؽºÆ® ÆÄÀϷκÎÅÍ ¸¶Áö¸· ÁÙÀ» ¿­¶÷ÇÕ´Ï´Ù.
Loop, read, C:\Log File.txt
    last_line := A_LoopReadLine  ; ȸµ¹ÀÌ°¡ ³¡³¯ ¶§, ¿©±â¿¡ ±× ¸¶Áö¸· ÁÙÀ» º¸À¯ÇÕ´Ï´Ù.

 

; ¿¹Á¦ #3: ´ÙÀ½ ½ºÅ©¸³Æ®´Â HTMLÀ̳ª ÅؽºÆ® ÆÄÀϷκÎÅÍ
; FTP¿Í HTTPÀÇ URLÀ» ¸ðµÎ ÃßÃâÇÏ·Á°í ½ÃµµÇÕ´Ï´Ù:
FileSelectFile, SourceFile, 3,, Pick a text or HTML file to analyze.
if SourceFile =
    return  ; ÀÌ °æ¿ì Á¾·áÇÕ´Ï´Ù.

SplitPath, SourceFile,, SourceFilePath,, SourceFileNoExt
DestFile = %SourceFilePath%\%SourceFileNoExt% Extracted Links.txt

IfExist, %DestFile%
{
    MsgBox, 4,, Overwrite the existing links file? Press No to append to it.`n`nFILE: %DestFile%
    IfMsgBox, Yes
        FileDelete, %DestFile%
}

LinkCount = 0
Loop, read, %SourceFile%, %DestFile%
{
    URLSearchString = %A_LoopReadLine%
    Gosub, URLSearch
}
MsgBox %LinkCount% links were found and written to "%DestFile%".
return


URLSearch:
; ´ÙÀ½°ú °°ÀÌ Æ¯º°ÇÑ ¹æ½ÄÀ¸·Î 󸮵˴ϴÙ. ¿Ö³ÄÇÏ¸é ¾î¶² URLÀº ´Ù¸¥ URLÀ» ÀڽŠ¾È¿¡ ³»ÀåÇϱ⠶§¹®ÀÔ´Ï´Ù:
StringGetPos, URLStart1, URLSearchString, http://
StringGetPos, URLStart2, URLSearchString, ftp://
StringGetPos, URLStart3, URLSearchString, www.

; °¡Àå ¿ÞÂÊÀÇ ½ÃÀÛ ÁöÁ¡À» ã½À´Ï´Ù:
URLStart = %URLStart1%  ; ±âº» ½ÃÀÛ ÁöÁ¡À» ¼³Á¤ÇÕ´Ï´Ù.
Loop
{
    ; "URLStart%A_Index%"¸¦ ÇÑ ¹ø¸¸ °áÁ¤Çϸé
    ; (Àû¾îµµ ¸¹Àº º¯¼ö°¡ ÀÖ´Â ½ºÅ©¸³Æ®¿¡¼­) ¼öÇ༺´ÉÀÌ Çâ»óµË´Ï´Ù
    ArrayElement := URLStart%A_Index%
    if ArrayElement =  ; ÀÇ»ç-¹è¿­ÀÇ ³¡¿¡ µµ´ÞÇß½À´Ï´Ù.
        break
    if ArrayElement = -1  ; ÀÌ ¿ä¼Ò´Â ÀÚ°ÝÀÌ ¾ø½À´Ï´Ù.
        continue
    if URLStart = -1
        URLStart = %ArrayElement%
    else ; URLStart´Â ¾È¿¡ À¯È¿ÇÑ À§Ä¡¸¦ °¡Áö°í ÀÖ½À´Ï´Ù.. ±×·¡¼­ ±×°ÍÀ» ArrayElement¿Í ºñ±³ÇÕ´Ï´Ù.
    {
        if ArrayElement <> -1
            if ArrayElement < %URLStart%
                URLStart = %ArrayElement%
    }
}

if URLStart = -1  ; URLÀÌ URLSearchString¿¡ Á¸ÀçÇÏÁö ¾Ê½À´Ï´Ù.
    return

; ±×·¸Áö ¾ÊÀ¸¸é, ÀÌ URLÀ» ÃßÃâÇÕ´Ï´Ù:
StringTrimLeft, URL, URLSearchString, %URLStart%  ; ½ÃÀÛ/ºÎÀûÀý ºÎºÐÀ» Á¦°ÅÇÕ´Ï´Ù.
Loop, parse, URL, %A_Tab%%A_Space%<>  ; (ÀÖ´Ù¸é) ù ¹ø° ½ºÆäÀ̽º, ÅÇ, ¶Ç´Â ¾Þ±Û ¹®ÀÚ¸¦ ã½À´Ï´Ù.
{
    URL = %A_LoopField%
    break  ; Áï, ù ¹ø° "Çʵå"¸¦ °¡Á® ¿À±â À§ÇØ ¿ÀÁ÷ ÇÑ ¹ø¸¸ ȸµ¹À̸¦ ¼öÇàÇÕ´Ï´Ù.
}
; Á¾·á ¹®ÀÚ°¡ ¹ß°ßµÇÁö ¾Ê¾Æ¼­ À§ÀÇ È¸µ¹ÀÌ¿¡ ¹Ýº¹ÀÌ ¾ø´Ù¸é,
; URL º¯¼öÀÇ ³»¿ëÀ» ±×´ë·Î µÓ´Ï´Ù.

; URLÀÌ °ãµû¿ÈÇ¥·Î ³¡³ª¸é, °ãµû¿ÈÇ¥¸¦ Á¦°ÅÇÕ´Ï´Ù. Áö±Ý ´çÀåÀº StringReplace°¡ »ç¿ëµË´Ï´Ù.
; ±×·¯³ª °ãµû¿ÈÇ¥°¡ URL ¾È¿¡ Á¸ÀçÇصµ À¯È¿ÇÑ °Í °°½À´Ï´Ù.
; ±×·¡¼­ Á¦°ÅÇÏ¸é ¼Õ»óÀ» ÀÔÈú ¼öµµ ÀÖ½À´Ï´Ù:
StringReplace, URLCleansed, URL, ",, All
FileAppend, %URLCleansed%`n
LinkCount += 1

; ÀÌ ÁÙ¿¡ ´Ù¸¥ URLµéÀÌ ÀÖ´Â ¾Ë¾Æ º¾´Ï´Ù:
StringLen, CharactersToOmit, URL
CharactersToOmit += %URLStart%
StringTrimLeft, URLSearchString, URLSearchString, %CharactersToOmit%
Gosub, URLSearch  ; Àڽſ¡°Ô Àç±Í È£ÃâÀ» ÇÕ´Ï´Ù.
return