Getting file lines randomly

  • Follow


Is there any way, using a dcl procedure, to select 3 lines in a file ,
randomly ?
Example:

$ ty file.txt

orange
strawberry
pineapple
banana
lime

$ @randomly

banana
oranga
lime

0
Reply contracer11 (156) 7/24/2006 2:24:10 PM

On Mon, 24 Jul 2006 contracer11@gmail.com wrote:

> Is there any way, using a dcl procedure, to select 3 lines in a file ,
> randomly ?

Sure.  DCL is a programming language.

On the VMS FAQ, you will find a random number generator.

You can use SEARCH/STAT (for example) to find out how many lines are 
in file.

You can use the DCL OPEN command to open the file and the READ command 
to read the file sequentially, discarding lines that you don't want to 
print.

If you had, or were willing to create, unique keys for each record, 
you could create an indexed file and use READ/KEY and avoid reading 
the file sequentially.

Don't forget to issue the DCL CLOSE command to close the file.


-- 

Rob Brown                        b r o w n a t g m c l d o t c o m
G. Michaels Consulting Ltd.      (780)438-9343 (voice)
Edmonton                         (780)437-3367 (FAX)
                                  http://gmcl.com/

0
Reply mylastname3 (505) 7/24/2006 3:02:08 PM


contracer11@gmail.com wrote:
> Is there any way, using a dcl procedure, to select 3 lines in a file ,
> randomly ?

    Yes, this selection is easily possible by acquiring any of various 
values (often derived from the system time and I/O counts and other such 
factors) and using this to generate an index on a loop that reads in the 
records, stopping when the pseudo-random index value is reached.

   Writing some sort of password tool?  If so, there are rather better 
ways to deal with this particular situation.  (There are tools and 
mechanisms that allow rather more random passwords, and there are DCL 
procedures that use these tools available for examination.)

   There are various "toys" that use a sequence similar to what you seem 
to want here, the most common one I can think of has three sets of 
(usually) ten words, and can be used as a buzz-phrase generator.  For 
example, and realize I have no connection with any of the following two 
URLs:

     http://www.acronymfinder.com/buzzgen.asp
     http://www.cafepress.com/iqthereforeiam.35260768

   Buzz-phrase bingo is a pursuit encountered occasionally in various 
presentations, as well.  Whether subversive or a survival instinct 
depends on whether you're calling the game or playing it, of course.
0
Reply hoff-remove-this (566) 7/24/2006 3:05:36 PM

Rob Brown wrote:
> On Mon, 24 Jul 2006 contracer11@gmail.com wrote:
>
> > Is there any way, using a dcl procedure, to select 3 lines in a file ,
> > randomly ?
>
> Sure.  DCL is a programming language.
>
> On the VMS FAQ, you will find a random number generator.
>
> You can use SEARCH/STAT (for example) to find out how many lines are
> in file.
>
> You can use the DCL OPEN command to open the file and the READ command
> to read the file sequentially, discarding lines that you don't want to
> print.
>
> If you had, or were willing to create, unique keys for each record,
> you could create an indexed file and use READ/KEY and avoid reading
> the file sequentially.
>
> Don't forget to issue the DCL CLOSE command to close the file.
>
>
> --
>
> Rob Brown                        b r o w n a t g m c l d o t c o m
> G. Michaels Consulting Ltd.      (780)438-9343 (voice)
> Edmonton                         (780)437-3367 (FAX)
>                                   http://gmcl.com/

What I am doing wrong ??
procedure below only show me file first line...

Alpha>> ty ap.com
$ set nover
$   now_time = F$time()
$   hrs = F$cvtime(now_time,, "hour")
$   min = F$cvtime(now_time,, "minute")
$   sec = F$cvtime(now_time,, "second")
$   hun = F$cvtime(now_time,, "hundredth")
$
$   !
$   ! Get a good seed to start the madness.
$   !
$   seed = 360000 * hrs + 6000 * min + 100 * sec + hun
$   seed = seed .and. %XFFFF
$
$   highval = 100           ! Get a number from 0 to 100.
$   gosub _RANDOM           ! Get the random
$!   write sys$output random
$ open/read file users.lis
$ read/index='random' file  registro
$ write sys$output "''registro'"
$ close file
$exit
$_RANDOM:
$   seed = (seed * 69069 + 1) .and. %XFFFFFFFF
$   rand = (seed / %X100) .and. %XFFFF
$   t = rand / highval
$   k = %XFFFF / highval
$   if rand .gt. (k * highval) then goto _RANDOM
$   random = rand - t * highval + 1
$
$ Return
$ open/read file users.lis
$ read/index='random' file  registro
$ write sys$output "''registro'"
$ close file


Alpha>> @ap
      OpenVMS User Processes at 24-JUL-2006 11:24:22.57
Alpha>> @ap
      OpenVMS User Processes at 24-JUL-2006 11:24:22.57
Alpha>> @ap
      OpenVMS User Processes at 24-JUL-2006 11:24:22.57
Alpha>>

0
Reply contracer11 (156) 7/24/2006 4:33:20 PM

On Mon, 24 Jul 2006 contracer11@gmail.com wrote:

> What I am doing wrong ??
> procedure below only show me file first line...
>
> Alpha>> ty ap.com
> ...
> $ open/read file users.lis
> $ read/index='random' file  registro
> $ write sys$output "''registro'"

If you created users.lis by SHOW USERS/OUTPUT=USERS.LIS, then 
users.lis is *not* an indexed file and you are limited to reading the 
file sequentially.  In other words, /INDEX and /KEY will not work.

Since it appears that you want to randomly pick some logged on users 
by line number, it would not seem efficient to create an indexed file 
with line numbers.  Other things are possible, but I think you might 
as well just read the file sequentially, counting records, and 
selecting the records that match your random numbers.


-- 

Rob Brown                        b r o w n a t g m c l d o t c o m
G. Michaels Consulting Ltd.      (780)438-9343 (voice)
Edmonton                         (780)437-3367 (FAX)
                                  http://gmcl.com/

0
Reply mylastname3 (505) 7/24/2006 5:35:09 PM

contracer11@gmail.com wrote:
> Rob Brown wrote:
> > On Mon, 24 Jul 2006 contracer11@gmail.com wrote:
> >
> > > Is there any way, using a dcl procedure, to select 3 lines in a file ,
> > > randomly ?
> >
> > Sure.  DCL is a programming language.
> >
> > On the VMS FAQ, you will find a random number generator.
> >
> > You can use SEARCH/STAT (for example) to find out how many lines are
> > in file.
> >
> > You can use the DCL OPEN command to open the file and the READ command
> > to read the file sequentially, discarding lines that you don't want to
> > print.
> >
> > If you had, or were willing to create, unique keys for each record,
> > you could create an indexed file and use READ/KEY and avoid reading
> > the file sequentially.
> >
> > Don't forget to issue the DCL CLOSE command to close the file.
> >
>
> What I am doing wrong ??
> procedure below only show me file first line...
>
> Alpha>> ty ap.com
> $ set nover
> $   now_time = F$time()
> $   hrs = F$cvtime(now_time,, "hour")
> $   min = F$cvtime(now_time,, "minute")
> $   sec = F$cvtime(now_time,, "second")
> $   hun = F$cvtime(now_time,, "hundredth")
> $
> $   !
> $   ! Get a good seed to start the madness.
> $   !
> $   seed = 360000 * hrs + 6000 * min + 100 * sec + hun
> $   seed = seed .and. %XFFFF
> $
> $   highval = 100           ! Get a number from 0 to 100.

Are there always 100 records in the file? If not, you need the exact
count and since you need to read through the file anyway:

$   highval = 0
$   open/read file users.lis
$!
$loop1:
$   read/end=counted file registro
$   highval = highval + 1
$   save'highval' = registro
$   goto loop1
$!
$counted:
$   close file
$   if highval .gt. 0
$     then
$       count = 0
$   loop2:
$       gosub _RANDOM           ! Get the random
$       if random .gt. 0  -
   .and. random .le. highval       ! paranoia test
$         then
$               write sys$output save'random'
$               count = count + 1
$               endif
$       if count .lt. 3 then goto loop2
$     else
$       write sys$output "File is empty."
$   endif
$exit
$!
$_RANDOM:
$...

Haven't really tested your _RANDOM routine, thus the paranoia test.
Others have explained the read/index problem.

0
Reply dphill46 (609) 7/25/2006 9:31:31 PM

contracer11@gmail.com wrote:
> 
> $ read/index='random' file  registro

To repeat, /INDEX is used for indexed (ISAM) files, only. It is
meaningless for other file formats.

See http://www.djesys.com/vms/mentor/rms.html for information about RMS
file formats.

-- 
David J Dachtera
dba DJE Systems
http://www.djesys.com/

Unofficial OpenVMS Marketing Home Page
http://www.djesys.com/vms/market/

Unofficial Affordable OpenVMS Home Page:
http://www.djesys.com/vms/soho/

Unofficial OpenVMS-IA32 Home Page:
http://www.djesys.com/vms/ia32/

Unofficial OpenVMS Hobbyist Support Page:
http://www.djesys.com/vms/support/
0
Reply djesys.no (1536) 7/26/2006 2:37:01 AM

6 Replies
58 Views

(page loaded in 0.309 seconds)


Reply: