1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

pass by reference not working using 2d array variable

Discussion in 'JavaScript' started by zak100, May 27, 2022.

  1. #1
    Hi,

    I am passing an empty 2d array in a function. Function is storing some value in the 2d array. But when the function returns, I can't print that value. Following is my code:
    
    const path = require("path");
    const fs = require("fs");
    module.exports = async function(callback)
    {
    try {
    let argStr = [[],[]]
    let argStr2 = "address to, uint amount"
    find_the_arguments(argStr, argStr2)
    console.log("outside argStr[0][0]"+argStr[0][0])
    console.log("outside argStr[0][1]"+argStr[0][1])
    console.log("outside argStr[1][0]"+argStr[1][0])
    console.log("outside argStr[1][1]"+argStr[1][1])
    }
    catch (error) {
    console.log(error)
    }
    callback();
    }
    
    function find_the_arguments(argStr, argStr2) {
    argStr = argStr2.split(",");
    for (let i = 0; i < argStr.length; i++) {
    argStr[I] = argStr[I].split(" ").filter(s => s !== '')
    }
    console.log("inside argStr[0][0]"+argStr[0][0])
    console.log("inside argStr[0][1]"+argStr[0][1])
    console.log("inside argStr[1][0]"+argStr[1][0])
    console.log("inside argStr[1][1]"+argStr[1][1])
    }
    Code (JavaScript):
    Following is my output:

    $ truffle exec separateArguments.js
    Using network 'development'.

    inside argStr[0][0]address
    inside argStr[0][1]to
    inside argStr[1][0]uint
    inside argStr[1][1]amount
    outside argStr[0][0]undefined
    outside argStr[0][1]undefined
    outside argStr[1][0]undefined
    outside argStr[1][1]undefined
    I want both outside and inside statements should print same value. Some body please guide me.

    Zulfi.


    pre.cjk { font-family: "DejaVu Sans Mono", monospace }p { margin-bottom: 0.1in; line-height: 115% }code.cjk { font-family: "DejaVu Sans Mono", monospace }[/I][/I]
     
    Last edited by a moderator: May 28, 2022
    zak100, May 27, 2022 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    I've saved your code as a jsfiddle: https://jsfiddle.net/itamer/9e13n7qg/3/
    you can throw anything into a console.log and separate with commas - saves you having to worry about variable types

    I gave the first function a name but it would help if you could add in some real world data in calls and show us what you expect the result to be.
     
    sarahk, May 28, 2022 IP
  3. zak100

    zak100 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Hi,
    Thanks for your response. @sarahk This question is not related to console.log. I am asking about parameter passing.

    Zulfi.
     
    zak100, May 28, 2022 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #4
    yeah, that was just a morsel incase you didn't know.

    I'm really confused about what your function is trying to do. You pass argStr and then promptly overwrite it. Not only is it bad practice to overwrite parameters it also defeats the purpose of passing it.

    I've made a few more changes: https://jsfiddle.net/itamer/9e13n7qg/8/

    which gives a predictable response. If you're after something else you should probably rework the jsfiddle to give a clearer picture of your requirements - or state them here.
    result:
    "doihaveaname:", [[], []], "address to, unit amount"
    "params original:", [[], []], "address to, unit amount"
    "params reworked:", [["address", "to"], ["unit", "amount"]], "address to, unit amount"
    Code (ApacheConf):
    async function doihaveaname() {
      try {
        let argStr = [
          [],
          []
        ]
        let argStr2 = "address to, unit amount"
        console.log("doihaveaname:", argStr, argStr2)
        find_the_arguments(argStr, argStr2)
       
    
      } catch (error) {
        console.log(error)
      }
    
    }
    
    function find_the_arguments(argStr, argStr2) {
      console.log("params original:", argStr, argStr2)
      argStr = argStr2.split(",");
      for (let i = 0; i < argStr.length; i++) {
        argStr[i] = argStr[i].split(" ").filter(s => s !== '')
      }
      console.log("params reworked:", argStr, argStr2)
    
    }
    doihaveaname()
    
    Code (JavaScript):
     
    sarahk, May 29, 2022 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #5
    While arrays are passed by reference, creating a new array and assigning it to that reference breaks the association. It's a NEW array as such the pointer (reference) is no longer valid. The MOMENT you do:

    argStr = argStr2.split(",");

    You've broken the reference. Thus to do what you're trying to do, you'll need one more variable local to the scope to pull this off.

    
    async function doIHaveName() {
      try {
        let
          argStr = [ [], [] ],
          argStr2 = "address to, unit amount";
        console.log("Before call:", argStr, argStr2);
        findTheArguments(argStr, argStr2);
        console.log("After call:", argStr, argStr2);
      } catch (error) {
        console.log(error);
      }
    } // doIHaveName
    
    function findTheArguments(argStr, argStr2) {
      console.log("start call:", argStr, argStr2);
      let splitStr = argStr2.split(",");
      for (let i = 0; i < argStr.length; i++) {
        argStr[i] = splitStr[i].split(" ").filter(s => s !== '');
      }
      console.log("end call:", argStr, argStr2);
    }
    
    doIHaveName();
    
    Code (markup):
    At which point stop screwing around trying to modify by reference and just use return.

    Also wondering why you think you need an operation like this to be asynchronous given you aren't doing anything event related. You're just making the code harder to work with.
     
    deathshadow, Jun 18, 2022 IP
    sarahk likes this.