random line from file

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
zorgski
Newbie
Posts: 2
Joined: Thu Jun 10, 2004 7:42 am

random line from file

Post by zorgski » Thu Jun 10, 2004 7:46 am

hi!
i just started out doing some stuff with macros and need a function to work properly..
I´m tryin to read a random line from a file called question.ini and return the value of that random line, the function i have malfunctions sometimes and returns a strange value

in the macro i use
ReadIniFile>C:\random\question.ini,a,k
and then
Message>k
but sometimes it returns a,k

Can someone help me out with doing a correct random function?

Best regards

Jack

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Thu Jun 10, 2004 8:13 pm

You could use VB to generate a random number. Something like this:
VBSTART
Function NewNumber()
Randomize
'NewNumber =RND() 'Random 15 decimal number
'NewNumber =Int((RND*9)+1) 'Random number from 1 to 9
'NewNumber =Int(RND*10) 'Random number from 0 to 9
NewNumber =Int(RND*100) 'Random number from 00 to 99
End Function
VBEND

VBEVAL>NewNumber(),NewNumber
Message>%NewNumber%
Sample here generates from 00-99 with other lines provided as examples. Now you can use that number to generate a line number. Modify RND formula as needed to get the range of numbers that you want.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Fri Jun 18, 2004 12:46 pm

Or you can use Macro Scheduler's own Random command:

Random>100,RndLineNumber
MJT Net Support
[email protected]

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Fri Jun 18, 2004 1:43 pm

HUH? :shock:

I can't believe I missed that...totally forgot about Random> :oops:

That is a much more sensible solution, shame on me! :oops: :roll: :oops:
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

surferdude

Post by surferdude » Fri Jun 18, 2004 3:18 pm

Is there any simple/efficient way to grab the last line of a large text file?

Guest

Post by Guest » Fri Jun 18, 2004 8:15 pm

Console utilities for Win32. These are Unix-like utilities to be used from the command line in a DOS box. They are true Win32 applications:
Count: Count bytes, characters, letters, vowels, consonants, words, lines sentences, words per sentence and letters per word.
Cpy: Copy and updates files. Enhanced xcopy command.
Cut: Extract some columns, rows, fields or bytes from a file.
Detab: Substitute spaces for Tabs.
Entab: Substitute Tabs for spaces.
FileComp: Compare lines from files.
Head: Dump the first few lines or characters of a file.
Paste: Merge lines from different files.
Sort: Enhanced sort and merge utility.
Tail: Dump the last few lines or characters of a file.
Trail: Fix end of lines.
Uniq: Delete repeated lines.

[Download]

----

Of course, you could do a line count and afterwards read the last detected line.

surferdude

Post by surferdude » Mon Jun 21, 2004 5:08 pm

Anonymous wrote:Console utilities for Win32
Great, thanks, very useful.
Of course, you could do a line count and afterwards read the last detected line.
I know, I know, just looking for the easy way out.

Lumumba

Post by Lumumba » Tue Jun 22, 2004 7:13 am

Let>k=0

Label>start
Add>k,1
ReadLn>c:\temp\test.txt,k,line
If>line##EOF##,start

Sub>k,1
ReadLn>c:\temp\test.txt,k,line
Message>Macro Scheduler proudly present the last line:%CRLF%line

-----

Main part of the script stolen from the Macro Scheduler help :!: RTFM :wink:

Guest

Post by Guest » Tue Jun 22, 2004 8:43 pm

But that runs through the whole file - right? I asked for "simple/efficient" and was hoping someone knew of a magic bullet that wouldn't have to scan all the way through 100K lines to snag the last one. Having said that RTFM is always a good idea.

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Tue Jun 22, 2004 10:30 pm

No time to provide all code right now, but you can use FIND with the TYPE command to get the number of lines in a file.

Redirect the output to a file. Read that file to get the Line Count.

Then you can do ReadLn> as Lumumba suggested, with the line count from the earlier steps.

Operates almost instantly, no need to run through all lines in a file.

Here are abbreviated primary line samples using variables:
Run Program>C:\WINNT\System32\cmd.exe /c find /c /v "%Seek2%" %Source%>>%CountFile%
ReadLn>%CountFile%,2,Result

//From Lumumba:
ReadLn>%Source%,%Result%,line
Message>Macro Scheduler proudly present the last line:%CRLF%line
Hopefully this will be enought to get you started.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

surferdude

Post by surferdude » Wed Jun 23, 2004 3:21 pm

Thank you Bob, I'm continually in awe of your and Lumumba's expertise in this area :shock:

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts