Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8054881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:14:47+00:00 2026-06-05T08:14:47+00:00

So I am trying to learn Linked Lists using Perl. I am reading Mastering

  • 0

So I am trying to learn Linked Lists using Perl. I am reading Mastering Algorithms with Perl by Jon Orwant. In the book he explains how to create a linked list.
I understand most of it, but I just simply fail to understand the command/index/key NEXT in the second last line of the code snippet.

$list=undef;
$tail=\$list;

foreach (1..5){
  my $node = [undef, $_ * $_];
  $$tail = $node;
  $tail = \${$node->[NEXT]}; # The NEXT on this line? 
}

What is he trying to do there?

Is $node a scalar, which stores the address of the unnamed array? Also even if we are dereferencing $node, should we not refer to the individual elements by an index number, such as (0,1). If we do use NEXT as a key, is $node a reference to a hash?
I am very confused.

Something in plain English will be highly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:14:48+00:00Added an answer on June 5, 2026 at 8:14 am

    NEXT is a constant, declared earlier in the script. It contains an integer value representing the index of the current node’s member element that refers to the next node.

    Under this scheme, each node is a small anonymous array. One element of this anonymous array contains the payload, and the other contains a reference pointing to the next node.

    If you look at some of the earlier examples in that chapter you will see the following declarations:

    use constant NEXT    => 0;
    use constant VAL     => 1;
    

    So $node->[NEXT] is synonymous to $node->[0], which contains a reference to the next node in the linked list chain, while $node->[VAL] is synonymous with $node->[1]; the value (or payload) stored in the current node.

    I’ll comment on the code snippet you provided:

    foreach (1..5){
      my $node = [undef, $_ * $_];    # Create a new node as an anon array.
    
      # Set the previous node's "next node reference" to point to this new node.
      $$tail = $node;                 
    
      # Remember a reference to the new node's "next node reference" element.
      # So that it can be updated when another new element is added on next iteraton.
      $tail = \${$node->[NEXT]}; # The NEXT on this line? 
    }
    

    Excellent book, by the way. I’ve got several algorithms books, and that one continues to be among my favorites after all these years.

    Update: I do agree that the book isn’t a model of current idiomatic Perl, or current “best practices” Perl, but do feel it is a nice resource for gaining an understanding of the application of classic algorithms with Perl. I still refer back to it from time to time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn linked-lists in Java and had some questions about the code
I was trying learn lex and yacc using the oreilly book. I tried following
I am trying to implement a simple linked list using c++. I am probably
I'm trying to create a linked list, I've got it working, but I'm still
Possible Duplicate: Copy a linked list Hello stackoverflow! I am trying to learn more
I am trying to learn about data structures and algorithms on my own. I
Hello I am trying to create a for loop that loops through the linked
I have been trying to learn from reading tutorials online and stuff but I
I am trying to learn using DOMDocument for parsing HTML code. I am just
I am trying to learn the basics of socket programming and am using some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.