如何在嵌套循环中订购进程?

发布于 2025-02-05 06:43:57 字数 2255 浏览 2 评论 0原文

因此,在这里,我正在构建一个脚本,该脚本将从列表中获取一个令牌,并将其从另一个列表发送到钱包。对我来说,按顺序进行操作非常重要,因此Wallet 1接收令牌1,Wallet 2接收令牌2,依此类推。我正在做一个嵌套循环,因为我需要同时从两个TXT文件中获取信息。

因此,这是代码:

const shell = require("shelljs");
const fs = require("fs");

const numberToAirdrop = 1;
const pathToTheWLFile = "./wl.txt" // list of wallets
const pathToTheTKFile = "./tk.txt" // list of tokens


const wlFile = fs.readFileSync(pathToTheWLFile).toString().replace(/\r/g, '').split("\n");
const tkFile = fs.readFileSync(pathToTheTKFile).toString().replace(/\r/g, '').split("\n");


console.log(wlFile)
console.log(tkFile)

// Here's where I struggle:

for (const [index, address] of wlFile.entries()) for (const [index2, address2] of tkFile.entries()) {

    if (address.includes("(sent) ") && address2.includes("(sent) ")) {
        console.log(`[${index}][${index2}] Skipping ${address} and ${address2} as has been processed before`);
        continue;
    }

    console.log(`[${index}] [${index2}] Airdropping ${numberToAirdrop} of ${address2} to ${address}`);
    console.time("Sent in")

    const output = shell.exec(`spl-token transfer ${address2} ${numberToAirdrop} ${address} --allow-unfunded-recipient --fund-recipient -v`); // transfer process

    if (output.stderr && output.code !== 0) {
        throw new Error(
            `[${index}] ${index2}] Failed to send ${address2} to the ${address} ` +
            `last '${"(sent) to" + wlFile[index] && tkFile[index2]}'`
        )
    }

    tkFile[index2] = "(sent) " + tkFile[index2]
    wlFile[index] = "(sent) " + wlFile[index]
    fs.writeFileSync(pathToTheWLFile, wlFile.join("\n"));
    fs.writeFileSync(pathToTheTKFile, tkFile.join("\n"));

    console.log(`[${index}][${index2}] Airdropping ${numberToAirdrop} of ${address2} to ${address} has been successful\n\n`);
    console.timeEnd("Sent in")
}


const notDone = wlFile.find((i) => !i.includes("(sent)"));
if (notDone) {
    console.log("Upload did not finish for all the addresses please re run")
    return;
}

console.log("Upload is done! All the tokens have been airdropped!")

基本上是有效的,但是以嵌套的循环方式,我的意思是将令牌1发送到Wallet 1,但随后尝试将同样的令牌发送到Wallet 2,然后将令牌2发送到Wallet 1。我需要。它可以按顺序工作,因此钱包1接收令牌1,钱包2接收令牌2,依此类推,而无需重复操作。在嵌套循环中可能吗?

So here I'm building a script which will take a token from the list and send it to the wallet from another list. It's very important for me to do it in order, so wallet 1 receives token 1, wallet 2 receives token 2 and so on. I'm doing a nested loop, because I need to get info from both TXT files at the same time.

So here's the code:

const shell = require("shelljs");
const fs = require("fs");

const numberToAirdrop = 1;
const pathToTheWLFile = "./wl.txt" // list of wallets
const pathToTheTKFile = "./tk.txt" // list of tokens


const wlFile = fs.readFileSync(pathToTheWLFile).toString().replace(/\r/g, '').split("\n");
const tkFile = fs.readFileSync(pathToTheTKFile).toString().replace(/\r/g, '').split("\n");


console.log(wlFile)
console.log(tkFile)

// Here's where I struggle:

for (const [index, address] of wlFile.entries()) for (const [index2, address2] of tkFile.entries()) {

    if (address.includes("(sent) ") && address2.includes("(sent) ")) {
        console.log(`[${index}][${index2}] Skipping ${address} and ${address2} as has been processed before`);
        continue;
    }

    console.log(`[${index}] [${index2}] Airdropping ${numberToAirdrop} of ${address2} to ${address}`);
    console.time("Sent in")

    const output = shell.exec(`spl-token transfer ${address2} ${numberToAirdrop} ${address} --allow-unfunded-recipient --fund-recipient -v`); // transfer process

    if (output.stderr && output.code !== 0) {
        throw new Error(
            `[${index}] ${index2}] Failed to send ${address2} to the ${address} ` +
            `last '${"(sent) to" + wlFile[index] && tkFile[index2]}'`
        )
    }

    tkFile[index2] = "(sent) " + tkFile[index2]
    wlFile[index] = "(sent) " + wlFile[index]
    fs.writeFileSync(pathToTheWLFile, wlFile.join("\n"));
    fs.writeFileSync(pathToTheTKFile, tkFile.join("\n"));

    console.log(`[${index}][${index2}] Airdropping ${numberToAirdrop} of ${address2} to ${address} has been successful\n\n`);
    console.timeEnd("Sent in")
}


const notDone = wlFile.find((i) => !i.includes("(sent)"));
if (notDone) {
    console.log("Upload did not finish for all the addresses please re run")
    return;
}

console.log("Upload is done! All the tokens have been airdropped!")

Basically it works, but in a nested loop way, I mean it sends token 1 to wallet 1, but then tries to send the same token to wallet 2, then it sends token 2 to wallet 1. And I need it to work in order, so wallet 1 receives token 1, wallet 2 receives token 2 and so on, without repeatable actions. Is it possible in a nested loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛歌少女 2025-02-12 06:43:57

您正在做一个嵌套的循环。您需要的是一个循环

for (let i = 0; i < list1.length && i < list2.length; i++) {
    let number1 = list1[i];
    let number2 = list2[i];
    //do your stuff...
}

You are making a nested for-loop. What you need is a single for-loop instead

for (let i = 0; i < list1.length && i < list2.length; i++) {
    let number1 = list1[i];
    let number2 = list2[i];
    //do your stuff...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文