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

  • Home
  • SEARCH
  • 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 8371041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:04:07+00:00 2026-06-09T14:04:07+00:00

This is a simple problem but cannot find any working solution for it. I

  • 0

This is a simple problem but cannot find any working solution for it. I have 2 files and the first file holds all the ID that I am interested in, for example “tomato”, “cucumber”, but also the ones I am not interested in, which hold no value in the second file. The second file has the following data structure

tomato    red

tomato    round

tomato    sweet

cucumber    green

cucumber    bitter

cucumber    watery

What I need to get is a file containing all the IDs with all the matching values from the second file, everything tab-seperated, like this:

tomato    red    round    sweet

cucumber    green    bitter    watery   

What I did so far is create a hash out of the IDs in the first file:

 while (<FILE>) {  
     chomp;  
     @records = split "\t", $_; 
     {%hash = map { $records[0] => 1 } @records};
 }

And this for the second file:

  while (<FILE2>) {
      chomp;
      @records2 = split "\t", $_; 
      $key, $value = $records2[0], $records2[1];
      $data{$key} = join("\t", $value);
  }

 close FILE;

 foreach my $key ( keys %data )
 {
     print OUT "$key\t$data{$key}\n"
     if exists $hash{$key} 
 }

Would be grateful for some simple solution for combining all the values matching the same ID! 🙂

  • 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-09T14:04:08+00:00Added an answer on June 9, 2026 at 2:04 pm

    for th first file:

    while (<FILE>) {  
        chomp;  
        @records = split "\t", $_; 
        $hash{$records[0]} = 1;
    }
    

    and for the second:

    while (<FILE2>) {
        chomp;
        @records2 = split "\t", $_;
        ($key,$value) = @records2;
        $data{$key} = [] unless exists $data{$key};
        push @{$data{$key}}, $value;
    }
    close FILE;
    
    foreach my $key ( keys %data ) {
        print OUT $key."\t".join("\t", @{$data{$key}})."\n" if exists $hash{$key};
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been searching and this problem seems simple but cannot find answer. I
I have one obviously simple problem, but cannot find good solution, so I would
This is a really simple problem, but I couldn't find a solution anywhere. I'm
I searched the site but I could not find any solution to this problem.
hey guys having this really simple problem but cant seem to figure out have
this may seem like a simple problem but I couldn't find it in the
All, this seems like a simple problem but it's giving me fits. Lets say
I'm guessing this is a simple problem, but I'm just learning... I have this:
This is such a simple problem, but I can't find an answer anywhere... I
There must be a simple solution to this problem but for the love of

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.