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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:11:40+00:00 2026-06-06T15:11:40+00:00

The script I have written outputs all lines from the file 2 that starts

  • 0

The script I have written outputs all lines from the file 2 that starts with a number that is in the file 1.

Question

How do I output all the other lines that didn’t matched?

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my @res;

open(FILE, '<', "1") or die $!;
while (defined (my $line = <FILE>)) {
  chomp $line;
  push @res, $line;
}
close FILE;

open(FILE, '<', "2") or die $!;
while (defined (my $line = <FILE>)) {
  chomp $line;
  $line =~ m/(\d+)/;

  if (defined $1) {
    foreach my $a (@res) {
      if ($a == $1) {
        print $line . "\n";
      }
    }
  }
}
close FILE;

File 1

155
156
157
158
159
160

File 2

150 a
151 f
152 r
153 a
154 a
155 a
156 a
157 f
158 f
159 f
  • 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-06T15:11:41+00:00Added an answer on June 6, 2026 at 3:11 pm

    Your answer is pretty close actually: it’s enough to change this

    foreach my $a (@res) {
      if ($a == $1) {
        print $line . "\n";
      }
    }
    

    … to this …

    my $found;
    foreach my $a (@res) {
      if ($a eq $1) { # we compare strings, not numbers, even if these strings are 'numeric'
        $found = 1;
        print $line . "\n";
        last; # no need to look further, we already found an item
      }
    }
    print "Not matched: $line", "\n" unless $found; 
    

    Yet still there’s something to talk about. ) See, as all these number strings in the first file are unique, it’s much better to use a hash for storing them. The code will actually not change that much:

    my %digits;
    ... # in the first file processing loop:
    $digits{$line} = 1;
    ... # in the second file processing loop, instead of foreach:
    if ($digits{$1}) { 
      print $line, "\n"; 
    } else {
      print "Not matched: $line", "\n";
    }
    

    But the point is that searching in hash is MUCH faster than looping through an array again and again. )

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

Sidebar

Related Questions

I've written a script that will be used for archiving log files from a
I have a shell script that calls a java jar file and runs an
I've written a PHP script that handles file downloads, determining which file is being
I have written a bash script that gets three paths based on input parameters
I have a bash script that runs a simulation program written in Fortran 90,
I have several test modules that are all invoked together via a driver script
I have a number of input images that contain multiple smaller images, all of
I have written a Perl script for the following bioinformatics question, but unfortunately there
I have written a ruby script that then calls another ruby script. The callee
This is the script I have written for gzipping content on my site, which

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.