mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Jun 2004
Posts: 5
B
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
B
Joined: Jun 2004
Posts: 5
Hi, here is what I am trying to accomplish, but I cannot seem to find a good way to do it (if it is possible).

I want to read a random line from a file ( $read(filename.txt) ), then delete that line.
I know that $readn will return the line number of the recently $read(..) line, and that /write -dl<number> filename.txt will delete line <number>.
Is there any way to get mirc to evaluate $readn within the parameters of the write command, to delete line number ($readn)?
for instance, something like:
/write -dl[$readn] filename.txt

When I try that command, it just deletes line 1 every time.
The only way I've been able to accomplish this so far is with a really long and ugly if/then else/then statement with a line for every possible line number that $readn might have returned. Anyone know of a more elegant way to do this?

For more information, I have a text file that represents a deck of cards, with one card on each line, and I want to remove each card from the deck once I have randomly $read it to avoid dealing duplicate cards. If there is another more elegant alternative to avoid dealing duplicate cards, that would be welcome information also.

Thanks for any input on this,
Ben

Joined: Feb 2004
Posts: 714
Z
Hoopy frood
Offline
Hoopy frood
Z
Joined: Feb 2004
Posts: 714
Code:
 .
 .
$read(filename.txt) 
var %line = $readn
write -dl $+ %line
 .
This is untested and might not work, since im not an expert in the subject.. but give it a try smile

Hope it helps,
Zyzzyx.


"All we are saying is give peace a chance" -- John Lennon
Joined: Jun 2004
Posts: 5
B
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
B
Joined: Jun 2004
Posts: 5
Ah, perfect, thanks!
After reading this, I looked into the use of $+ and it filled in a lot of blanks in the syntax for me..
As it turns out, I can even just use:
/write -ld $+ $readn blah.txt

Thanks!
Ben

Joined: Feb 2004
Posts: 714
Z
Hoopy frood
Offline
Hoopy frood
Z
Joined: Feb 2004
Posts: 714
Most welcome wink


"All we are saying is give peace a chance" -- John Lennon
Joined: Apr 2003
Posts: 701
K
Hoopy frood
Offline
Hoopy frood
K
Joined: Apr 2003
Posts: 701
Here's another way to shuffle, using a hash file (best thing to do now: /help hash files)
Code:
alias shuffle {
  var %i = $hget(cards,nbCards)  - 1
  .hadd -s cards cardsDealt 0
  while (%i) {
    var %rand = $rand(0, %i)
    var %temp = $hget(cards, %i)
    .hadd -s cards %i    $hget($cards, %rand)
    .hadd -s cards %rand %temp
    dec %i
  }
}
alias deal {
  if ($hget(cards,nbCards) == $hget(cards,cardsDealt)) {
    echo -s error: stack empty
    return
  }
  echo -s new card: $hget(cards,$hget(cards,cardsDealt))
  .hadd -s cards cardsDealt $calc($hget(cards,cardsDealt) + 1)
}

[color:green]; load hash table[/color]
alias loadcards {
  .hmake -s cards 10
  .hload -s cards cards.txt
  shuffle
}


Now make a text file named cards.txt with this format:
nbCards
52
0
Ace of Hearts
1
Two of Hearts
...
50
Queen of Spades
51
King of Spades


Good luck with it...


Link Copied to Clipboard