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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:57:06+00:00 2026-06-11T07:57:06+00:00

Looking for a way, how to get the values from a hash, in an

  • 0

Looking for a way, how to get the values from a hash, in an order defined by another list.

The “demo” code (real values are different):

use 5.014;
use warnings;

my $href = {
    'Long one' => 'v1',
    'xxx two'  => 'v2',
    'another3' => 'v3',
    'xfour'    => 'v4',
    'some5'    => undef,
};

#keys from the $href in defined order
my @order = ('Long one', 'xfour', 'nono', 'another3', 'some5', 'xxx two');

#in the real code:
#my $href = some_sub(......);  my @order = another_sub(....);

#cleanup the @order form undefined values
@order = grep { exists $href->{$_} && defined $href->{$_} } @order;

#my input
while(<DATA>) {
    chomp;

    #filter out nonexistent keys and undefined values
    my @defined_data = grep { exists $href->{$_} && defined $href->{$_} } split '/';

    my $str = "xx";
    $str = join('/',
               map { $href->{$_} }  some_my_sort(@defined_data)
    ) if @defined_data;

    say $str;
}

sub some_my_sort {
    my(@list) = @_;

    # "somewhat"sort the @list in order defined by @order
    # haven't any idea how to do this :(

    # __HERE NEED HELP__ to sort the @list, to the order defined in the @order

    #and get only first two values. if exists only one value, return only one
    if($#list > 0) {
        return ($list[0], $list[1]);
    }
    else {
        return($list[0]);
    }
}

__DATA__
another3/some5/Long one/xfour/xxx two
xxx two/blabla/some5/another3/xfour
some5
notexists/some5/xxx two/Long one
some5/another3

for the above input want get the next output:

v1/v4
v4/v3
xx
v1/v2
v3

Form @ikegami solution:

use 5.014;
use warnings;

my $href = { 'Long one' => 'v1', 'xxx two'  => 'v2', 'another3' => 'v3', 'xfour'    => 'v4', 'some5'    => undef, };
my @order = ('Long one', 'xfour', 'nono', 'another3', 'some5', 'xxx two');
@order = grep { exists $href->{$_} && defined $href->{$_} } @order;

my %order = map { $order{$_} => $_ } 0..$#order;
while (<DATA>) {
    chomp;
    my @keys = grep { defined $href->{$_} } split '/';
    @keys = sort { $order{$a} <=> $order{$b} } @keys;
    splice(@keys, 2) if @keys > 2;
    @keys = 'xx' if !@keys;
    say join '/', @{$href}{ @keys };
}

get the next – error – and don’t really understand why:

Global symbol "%order" requires explicit package name at ike line 8.
Execution of ike aborted due to compilation errors.
  • 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-11T07:57:08+00:00Added an answer on June 11, 2026 at 7:57 am

    After defining @order, define %order_h:

    my %order_h;
    my $i = 0;
    $order_h{$_} = $i++ for @order;
    

    Then, instead of the comment about sorting, add this line:

    @list = sort { $order_h{$a} <=> $order_h{$b} } @list;
    

    Here’s a cleaned-up version of the entire code:

    my %order = map { $order[$_] => $_ } 0..$#order;
    
    while (<DATA>) {
        chomp;
        my @keys = grep { defined $href->{$_} } split '/';
        @keys = sort { $order{$a} <=> $order{$b} } @keys;
        splice(@keys, 2) if @keys > 2;
        say join '/', @keys ? @{$href}{ @keys } : 'xx';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a way to get distinct values from a jagged array. I've
I am looking for a way to extract/get values from an array and assign
I'm looking for a way to get the values of params being set, whether
I'm looking for a way to get from the GPS it's longitude and latitude
I'm looking for a way to get all the form inputs and respective values
I'm looking for a way that would let me get specific fields from ID3
I'm trying to get a list of unique values from the 'type' field from
I'm looking for an elegant way to return multiple values from one subtemplate. why?
I'm looking for the most effective way to get configurable product attribute values on
I'm looking for a way to get the categories defined in the site directory

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.