Solved

Help with Compare script

Posted on 2014-09-07
8
127 Views
Last Modified: 2014-09-10
Need a script to compare two lists
0
Comment
Question by:gaurav sharma
  • 5
  • 3
8 Comments
 
LVL 26

Expert Comment

by:wilcoxon
Comment Utility
my @list1 = (5,3,7,2,4);
my @list2 = (3,6,8,2,1);
my %hash1 = map { $_ => 1 } @list1;
foreach my $k (@list2) {
    if (exists $hash1{$k}) {
        print "$k found in both lists\n";
        delete $hash1{$k};
    } else {
        print "$k not found in list1\n";
    }
}
print map { "$_ not found in list2\n" } keys %hash1;

Open in new window

0
 

Author Comment

by:gaurav sharma
Comment Utility
I have two text files workspace.txt and invalids.txt .Attached are both the text files. The perl script :

1. Needs to have the paths to the workspace.txt and invalids.txt as arguments like

    compare_perl.pl  /path/to/invalids.txt /path/to/workspace.txt  



2.  Check if each string in workspace.txt is present in invalids.txt. If a match is found in the invalids.txt then print
     the corresponding line in invalids.txt

3. The above check in invalids.txt is to look for string between the dot(.)and colon(') at the end.
       
      For instance in the file invalids.txt consider the line  'PACKAGE BODY: TRON2000.ts_k_jrp_300901sfsfs0_mma'      
      for comparison only consider the string

      ts_k_jrp_300901sfsfs0_mma


4. If a match is found then print the complete corresponding line in invalids.txt  like


     'PACKAGE BODY: TRON2000.ts_k_jrp_300901sfsfs0_mma' is invalid

      or else just print the text from workspace.txt is valid ....in this case print

      ts_k_jrp_300901sfsfs0_mma is valid


4 . Finally while comparison ignore the last row in invalids.txt n this case the line

    2 rows
invalids.txt
workspace.txt
0
 

Author Comment

by:gaurav sharma
Comment Utility
Desired output :

Since the only first string in workspace.txt matches the string in the invalids.txt the desired output should be

 'PACKAGE BODY: TRON2000.ts_k_jrp_300901sfsfs0_mma' is invalid
we_fg_hj_k_mma is valid
fd_hf_ks_kd_mma is valid
fg_cb_vn_kd_kl_mma is valid
qw_kj_lk_oi_lm_mma is valid
qw_df_vc_bh_lk_mma is valid
ts_k_jrp_3dfdssd01140_mma is valid
rtewtwert_dfsg_dafasd_mma is valid
adf_afaf_afa_afA_mma is valid
0
 

Author Comment

by:gaurav sharma
Comment Utility
@WILCOXON: Thanks for your response. I was trying to clearly explain what I need.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
Comment Utility
Based on your description, this should do what you want:
use strict;
use warnings;
use Fatal qw(open close);
my $finv = shift or die "Usage: $0 <invalids_file> <workspace_file>\n";
my $fwrk = shift or die "Usage: $0 <invalids_file> <workspace_file>\n";
open IN, $finv or die "could not open $finv: $!";
my %chk;
while (<IN>) {
    chomp;
    next if m{^\d+\s+rows?\s*$};
    if (m{\.(.*)'\s*$}) {
        $chk{$1} = $_;
    }
}
close IN;
open IN, $fwrk or die "could not open $fwrk: $!";
while (<IN>) {
    chomp;
    if (exists $chk{$_}) {
        print "$chk{$_} is invalid\n";
    } else {
        print "$_ is valid\n";
    }
}
close IN;

Open in new window

0
 

Author Comment

by:gaurav sharma
Comment Utility
The script works perfectly fine. Thank you for quick response. I had one last correction to make in the requirements. The strings in invalids.txt are all uppercase.  
Before checking if the string in workspace.txt exists in invalids.txt , convert the all the strings in workspace.txt to uppercase.
0
 

Author Comment

by:gaurav sharma
Comment Utility
@Wilcoxon: Let me know if you need anything
0
 
LVL 26

Expert Comment

by:wilcoxon
Comment Utility
It should work to just change line 19 to:
if (exists $chk{uc($_)}) {

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now