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 888743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:26:58+00:00 2026-05-15T13:26:58+00:00

I am inserting 2-dimensional array references into my heap in Perl. How should I

  • 0

I am inserting 2-dimensional array references into my heap in Perl.
How should I define the ‘elements’ attribute when constructing my heap so that I can properly use my comparator function?

my $heap = Heap::Simple->new( order     => \&byNumOrStr,
                              elements  => [Array => 0]
                             );

sub byNumOrStr
{
    my ( $a, $b ) = @_;

    $b->[0] <=> $a->[0]  #0-th element is a number. 
            ||
    $a->[1] cmp $b->[1]; #1-st element is a number
}

I keep getting back this error:

Can’t use string (“2.55”) as an ARRAY ref while “strict refs” in use … (This means I might actually have to compare my “number string” numerically)

  • 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-05-15T13:26:59+00:00Added an answer on May 15, 2026 at 1:26 pm

    Well, it’s likely that either $a or $b is being passed in as a string. Try printing out this variables after the assignment.

    From what I can see from the documentation, when you pass elements => [ Array => 0 ], unless the 0th item in the array is an array then you’ll only be comparing the values in the first slot of the array.

    [Array => $index]

    Indicates that the elements are array references, with the key at index $index. So now the element can be not just the key, but also associated data.

    This means that if 2.55 is in the array like [ 2.55, … ] then that’s what’s being passed in as $a or $b.

    The elements entry tells H::S how you want to derive the key. For a completely generic way, it says that you can pass [Function => $code_ref_for_key]. You could make it like this:

    sub first_two_slots {
    my $array_ref = shift;
    return [ @$array_ref[0,1] ];
    }

    And then with the order as specified, it would pass that array into your order and specify

    my $heap = Heap::Simple->new( order     => \&byNumOrStr,
                                  elements  => [Function => \&first_two_slots]
                                 );
    

    Original comment left in place: (It’s not relevant to how Heap::Simple calls order).

    if byNumOrStr is called from sort DON’T assign $a and $b in it. Those values are set by sort. If there is something coming in @_ it’s probably not what you want.

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

Sidebar

Ask A Question

Stats

  • Questions 532k
  • Answers 532k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have a class that sometimes is not necessary to… May 17, 2026 at 12:09 am
  • Editorial Team
    Editorial Team added an answer That's not possible outside your own app May 17, 2026 at 12:09 am
  • Editorial Team
    Editorial Team added an answer A rule-of-thumb while creating CSS styles is that, as much… May 17, 2026 at 12:09 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I've got MATLAB code inserting n-dimensional points (n >1) into a matrix ( myPointMatrix
I'm dynamically inserting some html into the document (by using obj.innerHTML += 'some html').
I am inserting two variables countryVar and langVar into a node in my xml
I'm inserting a value in table A, that has a serial type as primary
What's faster: inserting into a priority queue, or sorting retrospectively? I am generating some
I am inserting a table in to a file that already has some text
I have an assignment where I have questions that ask for the following implementations:
What the question says... Does jQuery have any methods that will allow you to
Creating hashes of hashes in Ruby allows for convenient two (or more) dimensional lookups.
I have people inserting video code to be displayed on my website. I want

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.