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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:10:14+00:00 2026-06-10T22:10:14+00:00

Have an array of hashes, my @arr = get_from_somewhere(); the @arr contents (for example)

  • 0

Have an array of hashes,

my @arr = get_from_somewhere();

the @arr contents (for example) is:

@arr = (
  { id => "id2",    requires => 'someid', text => "another text2" },
  { id => "xid4",   requires => 'id2',    text => "text44" },
  { id => "someid", requires => undef,    text => "some text" },
  { id => "id2",    requires => 'someid', text => "another text2" },
  { id => "aid",    requires => undef,    text => "alone text" },
  { id => "id2",    requires => 'someid', text => "another text2" },
  { id => "xid3",   requires => 'id2',    text => "text33" },
);

need something like:

my $texts = join("\n",  get_ordered_texts(@arr) );

soo need write a sub what return the array of texts from the hashes, – in the dependent order, so from the above example need to get:

"some text",     #someid the id2 depends on it - so need be before id2
"another text2", #id2    the xid3 and xid4 depends on it - and it is depends on someid
"text44",        #xid4   the xid4 and xid3 can be in any order, because nothing depend on them
"text33",        #xid3   but need be bellow id2
"alone text",    #aid    nothing depends on aid and hasn't any dependencies, so this line can be anywhere

as you can see, in the @arr can be some duplicated “lines”, (“id2” in the above example), need output only once any id.

Not providing any code example yet, because havent any idea how to start. ;(
Exists some CPAN module what can be used to the solution?

Can anybody points me to the right direction?

  • 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-10T22:10:16+00:00Added an answer on June 10, 2026 at 10:10 pm

    Using Graph:

    use Graph qw( );
    
    my @recs = (
       { id => "id2",    requires => 'someid', text => "another text2" },
       { id => "xid4",   requires => 'id2',    text => "text44" },
       { id => "someid", requires => undef,    text => "some text" },
       { id => "id2",    requires => 'someid', text => "another text2" },
       { id => "aid",    requires => undef,    text => "alone text" },
       { id => "id2",    requires => 'someid', text => "another text2" },
       { id => "xid3",   requires => 'id2',    text => "text33" },
    );
    
    sub get_ordered_recs {
       my %recs;
       my $graph = Graph->new();
       for my $rec (@_) {
          my ($id, $requires) = @{$rec}{qw( id requires )};
    
          $graph->add_vertex($id);
          $graph->add_edge($requires, $id) if $requires;
    
          $recs{$id} = $rec;
       }
    
       return map $recs{$_}, $graph->topological_sort();
    }
    
    my @texts = map $_->{text}, get_ordered_recs(@recs);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Noob question here! I have an array with hashes that looks like this: arr
For example, I have array of single hashes a = [{a: :b}, {c: :d}]
I have array of hashes: @array = [{:id => 1, :status=>R}, {:id => 1,
I have an array of hashes. Each hash looks like: 'date'=>6/23/2011, value1=>6, value2=>8, value3=>3,
I have an array of hashes which look like: ward = {id: id, name:
In Ruby, I have an array of hashes and an array. In my array
I have a Ruby method that searches an array of hashes and returns a
I have an array of hashes, many of which have shared keys. I would
If I have an array of hashes, each with a day key: [ {:day=>4,:name=>'Jay'},
In perl , i have an array of hashes like 0 HASH(0x98335e0) 'title' =>

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.