http://www.perlmonks.org?node_id=1209103


in reply to Changing an array from a sub

'Also, what's the best way to deal with arrays?'

Probably not always the best way, but sometimes handy comes the map-function:

map - perldoc.perl.org

Replies are listed 'Best First'.
Re^2: Changing an array from a sub
by harangzsolt33 (Chaplain) on Feb 14, 2018 at 06:28 UTC
    Thank you very much for all your answers! I come from a JavaScript background, and in JavaScript when you make a function call, a copy of each variable is passed to the function, but if the variable is an array, then only a reference is passed. So, writing to that array will change the original array as well. It's confusing when you combine it with perl. Lol But I am trying to sort it out.
    // The only way to work with arrays in JavaScript: myArray = [0, 1, 2, 3, 4]; ChangeArrayJS(myArray); function ChangeArrayJS(ARRAY) { ARRAY[2] = 'JS'; // changes myArray var COPY_OF_ARRAY = ARRAY; COPY_OF_ARRAY[2] = 'this will be lost'; }