Как увеличить элемент блока после найденного элемента?

Дано player-choices: ["rock" 0 "paper" 0 "scissors" 0]

Как я могу увеличить значение после "paper" в этом блоке путем поиска "paper"?

3 ответа

Решение
>> player-choices/("paper"): player-choices/("paper") + 1
== 1

Также учтите, что вам может не понадобиться использовать строки в вашем блоке данных.

player-choices: [rock 0 paper 0 scissors 0]
player-choices/paper: player-choices/paper + 1

Вы также можете написать общий incr func, вот так:

incr: function [
    "Increments a value or series index"
    value [scalar! series! any-word! any-path!] "If value is a word, it will refer to the incremented value"
    /by "Change by this amount"
        amount [scalar!]
][
    amount: any [amount 1]

    if integer? value [return add value amount]         ;-- This speeds up our most common case by 4.5x
                                                        ;   though we are still 5x slower than just adding 
                                                        ;   1 to an int directly and doing nothing else.

    ; All this just to be smart about incrementing percents.
    ; The question is whether we want to do this, so the default 'incr
    ; call seems arguably nicer. But if we don't do this it is at 
    ; least easy to explain.
    if all [
        integer? amount
        1 = absolute amount
        any [percent? value  percent? attempt [get value]]
    ][amount: to percent! (1% * sign? amount)]          ;-- % * int == float, so we cast.

    case [
        scalar? value [add value amount]
        any [
            any-word? value
            any-path? value                             ;!! Check any-path before series.
        ][
            op: either series? get value [:skip][:add]
            set value op get value amount
            :value                                      ;-- Return the word for chaining calls.
        ]
        series? value [skip value amount]               ;!! Check series after any-path.
    ]
]

А потом делай

incr 'player-choices/paper

Вы также можете сохранить ссылку на позиции значений в блоке, чтобы изменить их позже:

player-choices: ["rock" 0 "paper" 0 "scissors" 0]
rock-pos: find/tail player-choices "rock"
paper-pos: find/tail player-choices "paper"
scissors-pos: find/tail player-choices "scissors"

change paper-pos 1 + paper-pos/1
player-choices
; == ["rock" 0 "paper" 1 "scissors" 0]

Учитывая выбор игрока: ["камень" 0 "бумага" 0 "ножницы" 0] Как я могу увеличить значение после слова "бумага" в этом блоке, выполнив поиск по запросу "бумага"?

poke player-choices index? next find player-choices "paper" 1 + select player-choices "paper"

Сломано:

>> ? poke
USAGE:
     POKE series index value

DESCRIPTION: 
     Replaces the series value at a given index, and returns the new value. 
  1. Выбор игроков серии.
  2. Индекс - это позиция в выборе игрока значения после слова "бумага".
  3. Значение равно текущему значению после добавления бумаги к 1.

Найти вернет либо серию, в которой найдено значение, либо НЕТ.

>> find player-choices "paper"
== ["paper" 0 "scissors" 0]    

Но вам нужен индекс следующего значения, следовательно:

>> index? next find player-choices "paper"
== 4

Select вернет следующее значение ряда, если найдет значение, которое ему предшествует. В противном случае он не вернет ничего.

>> select pc "paper"
== 0

Но мы хотим увеличить его на единицу, поэтому

>> 1 + select pc "paper"
== 1
Другие вопросы по тегам