Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Embedded for loop.  (Read 6320 times)

0 Members and 1 Guest are viewing this topic.

Salmon Trout

  • Guest
Re: Embedded for loop.
« Reply #15 on: August 27, 2011, 03:40:26 AM »
@Ghostdog74 - Thank you for your interest. 

Paste appears not to do what I want in that it merges corresponding lines from each file, it does not appear to interleave the lines.

yes, it does this

(File 1)
Line A
Line B

(File 2)
Line 1
Line 2

Output

Line A <TAB> Line 1
Line B <TAB> Line 2

You can select other delimiters than the dafault TAB but essentially that's what it does I think. If the delimiter could be CR/LF that might be a solution?



ghostdog74



    Specialist

    Thanked: 27
    Re: Embedded for loop.
    « Reply #16 on: August 27, 2011, 04:15:50 AM »
    Yes, salmon is right, if Betty wants to intersperse the lines,
    Code: [Select]
    paste -d "\n" fileA.txt fileB.txt
    Otherwise, the default places the lines "side by side"

    CN-DOS



      Rookie

      • Experience: Experienced
      • OS: Windows Vista
      Re: Embedded for loop.
      « Reply #17 on: August 31, 2011, 05:20:18 AM »
      Quote
      Method 1 (straight code, no subroutine, process all lines of File2 for each line of File1) 83 seconds
      Method 2 (subroutine, jump out when correct line number match reached) 51 seconds (so not quite 50%)
      Method 3 (subroutine, use SKIP and jump out after reading 1 line)  19 seconds (a bit more than 50%)

      Hi Salmon Trout,

      Could you please help test the performace of this one:

      Code: [Select]
      @echo off
      setlocal enabledelayedexpansion
      (for /f "delims=" %%a in (file1.txt) do (
          set /p _f=
          echo,%%a
          echo,!_f!
      ))<file2.txt >file3.txt

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: Embedded for loop.
      « Reply #18 on: August 31, 2011, 06:08:44 AM »
      Quote
      Both files contain the same number of lines.

      I understand the OP specs, but what if the files are of unequal length? Batch code does not support arrays, however borrowing from REXX, you can mimic the stem.tails technique. I'm also a big fan of prompts for non-automation scripts. Makes them more generic.

      This is an alternate approach, nothing more:

      Code: [Select]
      @echo off
      setlocal enabledelayedexpansion

      :file1
        set /p file1=Enter File 1 Label:
        if not exist %file1% goto file1
       
      :file2
        set /p file2=Enter File 2 Label:
        if not exist %file2% goto file2 

      for /f "tokens=* delims=" %%y in (%file1%) do (
        call set /a idx=%%idx%%+1
        call set array.%%idx%%=%%y
      )

      set array.0=%idx%

      for /f "tokens=* delims=" %%i in (%file2%) do (
        call set /a index+=1
        if !index! LEQ %array.0% (
          call echo %%array.!index!%% >> c:\temp\merged.txt
        )
        call echo %%i >> c:\temp\merged.txt
      )

      if %index% LSS %array.0% (
        for /l %%i in (%index%, 1, %array.0%) do (
          echo !array.%%i! >> c:\temp\merged.txt
        )
      )

      Powershell and VBScript can also be used with varying degrees of simplicity.

       8)
      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein

      Salmon Trout

      • Guest
      Re: Embedded for loop.
      « Reply #19 on: August 31, 2011, 10:02:56 AM »
      Could you please help test the performace of this one:

      What performance result did you get?


      CN-DOS



        Rookie

        • Experience: Experienced
        • OS: Windows Vista
        Re: Embedded for loop.
        « Reply #20 on: August 31, 2011, 10:08:10 PM »
        I used timeit.exe to count the time, and 1000 lines for each file.

        Method 3 of Salmon Trout:
        Elapsed Time:     0:00:06.364
        Process Time:     0:00:04.368

        Method of CN-DOS:
        Elapsed Time:     0:00:00.468
        Process Time:     0:00:00.265

        BTW, the PMs from NatHeim are realy boring. Is it possible for moderator to disable him to use PM? Or does this forum have black list function, so I can put him in it?

        Salmon Trout

        • Guest
        Re: Embedded for loop.
        « Reply #21 on: September 01, 2011, 12:03:22 AM »
        Method of CN-DOS 0.38 seconds elapsed time.

        In your profile you have an Ignore list