Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with a subroutine 4

Status
Not open for further replies.

nfaber

Technical User
Oct 22, 2001
446
US
Hello all,

I have the following subroutine:

Code:
sub RenumerateDSLines ()
{
    my @DSLinesArray = @_;

    # look for the same elements and add up their value fields
    # after loop ends, output one line for each element type 
    # with the summ field updated
    
    $index = 1;
    $newData = [];
    foreach $line (@DSLinesArray) {
        @fields = split (",",$line);
        $fields[0] = "\"$index\"";
        $line = join (",",@fields);
        push (@$newData,$line);
        $index++;
    }

  return ($newData);
}


I am having trouble undestanding what these lines are doing:

$newData = [];

push (@$newData,$line);

I think it is because you can only return a scalar var from a subroutine? But even if so, can someone explain to me how this works and what the guy is doing (I took over someone elses project and am trying to understand it.

Thanks,

Nick
 
The line
Code:
$newData = [];
is just assigning an empty anonymous array reference to the scalar $newData.

The push statement is just adding data to that array. The form "@$newData" serves to retrieve the referred to array from the reference stored in $newData. For more info on how push() works, check out `perldoc -f push`.

As for the comment that is in the code - it doesn't really seem to match up to what the code is doing. or what the subroutine name indicates that it does. It would appear that the data is comma-separated and the first value is an index number, and the code is just iterating through the data and setting the index value with the value of it's position in the array + 1. I don't see any part where values are compared for identicalness or where data values are summed.

--jim

--jim
 
... because you can only return a scalar var from a subroutine?
Not so. You can return a list, or a hash, from a sub, or pass one in, as long as it's the last, or only, item returned or passed. Problems arise only when you pass or return multiple lists or hashes. Then you must use references.


 
Thanks for the reply Coderifous. I see what you mean about the comments. This is a small part of a large program, so the comment may be out of place, and the rest is being done somewhere else.

I guess the point I should take here is my knowledge on the use of anonymous arrays (and multi-dimensional data structures) in perl is lacking, and I need to focus on learning that. The guy I took over for here is miles above me in that regard.
 
Jim/Mike,

Another question to help me understand this. For my original code, do I need the:

Code:
push (@$newData,$line);

Could I just use:

Code:
push (@newData,$line);

Thanks again,

Nick
 
nope, $newData is a scalar holding a reference to an array, it has no relation to an array potentially name @newData (something that gets addressed in Perl6...someday). @$newData is how you "cast" the reference as an array.

________________________________________
Andrew

I work for a gift card company!
 
Nick - in Jim's post above, he mentioned the line $newData = []; creates a reference to an anonymous array. The syntax @$newData is the dereferenced array that $newData is pointing at.

@newData would be an array (named @newData), @$newData is the array that $newData references.

I may not have explained that very well - for a better understanding of how references work, take a look at the perldocs perlref and perlreftut.
 
icrf, rharsh,

Thanks for the replies. Stars to you folks. It is starting to jell in my mind what you are talking about as far as references. In other words if:

$var=myarray;

and $arrref=@$var

the $arrref actually = @myarray

I have been programming perl for a few years but have always bumped my head against references to arrays and hashes. I'm just a little thick when it comes to getting it. I reviewed the O'Rielly "Programming Perl" book about references and complex data structures, and get it to a point, and then Wall just looses me.


rharsh...I will take a look at "prelref" and "perlreftut" and seee if that helps. Thanks for the info.
 
I thought I would add in regards to my last post that perl programming has been about 10% of my job in my career (been doing Network Management/SNMP) but now has been bumped up to about 50% It seems, which I do not mind. I really do love programming and the elegance of perl. I love the idea of creating something from nothing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top