MSFN Forum: Nested Loops of Array Elements - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Nested Loops of Array Elements Rate Topic: -----

#1 User is offline   SciFi 

  • Group: Members
  • Posts: 4
  • Joined: 07-February 12
  • OS:none specified
  • Country: Country Flag

Posted 07 February 2012 - 11:29 PM

Hey, can I do this in dos?
Used to do similar on Unix but can't see how to make it work for Windows.

-----
Set Pets=(Cats Dogs Horses)
Set Cats=(FiFi FruFru Fluffy)
Set Dogs=(Lassie MrMuggles RinTinTin)
Set Horsese=(MrEd Trigger)

FOR %%A in %Pets% DO (
FOR %%B in %%A DO (
Echo “%%A %%B”
)
)
-----
Output should be…
Cats FiFi
Cats FruFru
Cats Fluffy
Dogs Lassie
Dogs MrMuggles
Dogs RinTinTin
Horses MrEd
Horses Trigger

This obviously does not work, is there any way to achieve this?

Simon.


#2 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 9,108
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 08 February 2012 - 03:20 AM

View PostSciFi, on 07 February 2012 - 11:29 PM, said:

This obviously does not work, is there any way to achieve this?

Are you talking of DOS or of a NT system (command.com vs. CMD.EXE)?

jaclaz

#3 User is offline   Scr1ptW1zard 

  • Junior
  • Pip
  • Group: Members
  • Posts: 57
  • Joined: 05-July 07

Posted 08 February 2012 - 03:54 AM

I do not have a DOS system to test this on, only a Windows 7 command prompt (cmd.exe).
This will do for the specific conditions you have listed.
Changes will need to be made if any of the pet names will have spaces.

@echo off

Set Pets=Cats Dogs Horses
Set Cats=FiFi FruFru Fluffy
Set Dogs=Lassie MrMuggies RinTinTin
Set Horses=MrEd Trigger

For %%a In (%Pets%) Do (
	For /f "Tokens=1,* delims== " %%b In ('set %%a') Do (
		For %%n In (%%c) Do (
			Echo %%a %%n
		)
	)
)


#4 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 9,108
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 08 February 2012 - 04:33 AM

If I may, if DELAYEDEXPANSION is set, you don't even need the FOR /F:
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

Set Pets=Cats Dogs Horses
Set Cats=FiFi FruFru Fluffy
Set Dogs=Lassie MrMuggles RinTinTin
Set Horses=MrEd Trigger


For %%A In (%Pets%) Do (
        For %%B In (!%%A!) Do (
                For %%C In (%%B) Do (
                        Echo %%A %%C
                )
        )
)


jaclaz

#5 User is offline   gunsmokingman 

  • MSFN Master
  • Group: Super Moderator
  • Posts: 2,019
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 08 February 2012 - 11:48 AM

Here is a way of doing what the poster wants using VBS script.
 Dim Pets :Pets = Array("Cats","Dogs","Horses")
 Dim a,c,i,z 
'-> Loop Threw Pets Array
  For Each a In Pets 
    c = c + 1
   call SortArray(a, c)
  Next
'-> Function To Display Pets Array And Names
   Function SortArray(Item, N)
    Select Case N
     Case 1 :i = Array("FiFi", "FruFru", "Fluffy")
     Case 2 :i = Array("Lassie", "MrMuggles", "RinTinTin")
     Case 3 :i = Array("MrEd", "Trigger", "Silver")
    End Select 
    For Each z In i
      WScript.Echo Item & vbTab & z
    Next 
   End Function



Produces this output
Cats	FiFi
Cats	FruFru
Cats	Fluffy
Dogs	Lassie
Dogs	MrMuggles
Dogs	RinTinTin
Horses	MrEd
Horses	Trigger
Horses	Silver




#6 User is offline   SciFi 

  • Group: Members
  • Posts: 4
  • Joined: 07-February 12
  • OS:none specified
  • Country: Country Flag

Posted 09 February 2012 - 09:06 PM

Thanks all, I'll run that on all systems (NT, XP, W7) today and see how it goes.
Not going to have issues with spaces, naming conventions we use prohibit it.

cheers

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy