Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Hello fellow monks.

I am trying to write a script that compares strings from different files.

File1:
99HEADER|001|001| 99INSSCH|AVP0| 99POLCOM|||||PIP735628020||||| 99INSFAC|F1_0|| 99INSFAC +|F2_N|| 99INSFAC|F3_N|| 99INSFAC|F4_0|| 99INSFAC|F5_0|| 99INSFAC|F6_I +M|| 99INSFAC|F8_0|| 99INSFAC|F9_B|| 99INSFAC|F10_0|| 99INSFAC|F11_3|| + 99INSFAC|F12_0|| 99INSFAC|F13_Y|| 99INSFAC|F14_N|| 99INSFAC|F15_5|| +99INSFAC|F16_30/11/2011|20111130| 99INSFAC|F17_31|| 99INSFAC|F18_NO|| + 99INSFAC|F19_246|| 99INSFAC|F20_B|| 99INSFAC|F21_0|| 99INSFAC|F22_5H +|| 99INSFAC|F23_P|| 99INSFAC|F24_M|| 99INSFAC|F25_1598|| 99INSFAC|F26 +_13|| 99INSFAC|F27_9|| 99INSFAC|F28_17|| 99INSFAC|F29_15|| 99INSFAC|F +30_13|| 99INSFAC|F31_12|| 99INSFAC|F32_34|| 99INSFAC|F33_17|| 99INSFA +C|F34_5|| 99INSFAC|F35_3|| 99INSFAC|F36_12|| 99INSFAC|F37_12|| 99INSF +AC|F38_19/09/2013|20130919| 99INSFAC|F39_3|| 99INSFAC|F43_B901 400|| +99INSFAC|F44_10.00|| 99INSFAC|F47_1.000094|| 99HEADER|004|001| 99INSSCH|248| 99POLCOM|3||CAP01|66|3301R7435459||||| 99INSFAC2|MSRA01_ +1||||||"LNI10708"| 99INSSCH|391| 99POLCOM|3||CAP01|66|3301R7435459||||| 99INSFAC2|MSGAL1| +|||||"W=P1|X=335|AB=0|BB=0|JF=569|HB=0|IB=0|AD=1|GC=I1|KD=0|YE=335|BF +=B|GF=0|KF=401|0|0|0|GSB=15|HSB=99.2468394|KDB=2377.37934|LDB=A|UDB=0 +|ETB=155|HTB=51|URB=7"| 99INSSCH|116| 99POLCOM|3||CAP01|66|3301R7435459||||| 99INSFAC2|MSRZ01a +||||||"00I10587"| 99INSFAC2|MSRZ01b||||||"335000 B"| 99INSSCH|216| 99POLCOM|3||CAP01|66|3301R7435459||||| 99INSFAC2|MSRZ01a +||||||"00I10587"| 99INSFAC2|MSRZ01b||||||"335000 B"| 99HEADER|006|001 99INSSCH|091| 99POLCOM|1||IIL|62|22593465033322||||| 99INSFAC2|C00156| +|||||I1P82240|CCCN0000|INNA0000|FAAA0570|YANZ1000| 99INSSCH|084| 99POLCOM|1||IIL|62|22593465033322||||| 99INSFAC2|C00050| +|||||I1001569| 99INSSCH|052| 99POLCOM|1||IIL|62|22593465033322||||| 99INSFAC2|C00124| +|||||XAAX0800|YPAX8400|ZAAZ0401|VAAA0000|WZZA0000| 99INSSCH|222| 99POLCOM|1||IIL|62|22593465033322||||| 99INSFAC2|C00243| +|||||XAAX0800|YPAX8400|ZAAZ0401|VAAA0000|WZZA0000| 99TERMIN|
And File2:
E99INSSCH|248| E99POLCOM|3||CAP01|66|3301R7435459||||| E99INSFAC2|MSRA +01_1||||||"LNI10708"| E99HEADER|004|001| E99INSSCH|248| E99POLCOM|3||CAP01|66|3301R7435459||||| E99INSFAC2|MSRA +01_1||||||"LNI10708"| E99HEADER|001|001| E99INSSCH|AVP0| E99POLCOM|||||PIP735628020||||| E99INSFAC|F1_0|| E99IN +SFAC|F2_N|| E99INSFAC|F3_N|| E99INSFAC|F4_0|| E99INSFAC|F5_0|| E99INS +FAC|F6_IM|| E99INSFAC|F8_0|| E99INSFAC|F9_B|| E99INSFAC|F10_0|| E99IN +SFAC|F11_3|| E99INSFAC|F12_0|| E99INSFAC|F13_Y|| E99INSFAC|F14_N|| E9 +9INSFAC|F15_5|| E99INSFAC|F16_30/11/2011|20111130| E99INSFAC|F17_31|| + E99INSFAC|F18_NO|| E99INSFAC|F19_246|| E99INSFAC|F20_B|| E99INSFAC|F +21_0|| E99INSFAC|F22_5H|| E99INSFAC|F23_P|| E99INSFAC|F24_M|| E99INSF +AC|F25_1598|| E99INSFAC|F26_13|| E99INSFAC|F27_9|| E99INSFAC|F28_17|| + E99INSFAC|F29_15|| E99INSFAC|F30_13|| E99INSFAC|F31_12|| E99INSFAC|F +32_34|| E99INSFAC|F33_17|| E99INSFAC|F34_5|| E99INSFAC|F35_3|| E99INS +FAC|F36_12|| E99INSFAC|F37_12|| E99INSFAC|F38_19/09/2013|20130919| E9 +9INSFAC|F39_3|| E99INSFAC|F43_B901 400|| E99INSFAC|F44_10.00|| E99INS +FAC|F47_1.000094|| E99HEADER|006|001 E99INSSCH|091| E99POLCOM|1||IIL|62|22593465033322||||| E99INSFAC2|C001 +56||||||I1P82240,CCCN0000,INNA0000,FAAA0570,YANZ1000| E99HEADER|006|001 E99INSSCH|091| E99POLCOM|1||IIL|62|22593465033322||||| E99INSFAC2|C001 +56||||||I1P82240,CCCN0000,INNA0000,FAAA0570,YANZ1000| E99HEADER|004|001| E99INSSCH|391| E99POLCOM|3||CAP01|66|3301R7435459||||| E99INSFAC2|MSGA +L1||||||"W=P1|X=335|AB=0|BB=0|JF=569|HB=0|IB=0|AD=1|GC=I1|KD=0|YE=335 +|BF=B|GF=0|KF=401,0,0,0|GSB=15|HSB=99.2468394|KDB=2377.37934|LDB=A|UD +B=0|ETB=155|HTB=51|URB=7"| E99HEADER|004|001| E99INSSCH|116| E99POLCOM|3||CAP01|66|3301R7435459||||| E99INSFAC2|MSRZ +01a||||||"00I10587"| E99INSFAC2|MSRZ01b||||||"335000 B"| E99HEADER|004|001| E99INSSCH|391| E99POLCOM|3||CAP01|66|3301R7435459||||| E99INSFAC2|MSGA +L1||||||"W=P1|X=335|AB=0|BB=0|JF=569|HB=0|IB=0|AD=1|GC=I1|KD=0|YE=335 +|BF=B|GF=0|KF=401,0,0,0|GSB=15|HSB=99.2468394|KDB=2377.37934|LDB=A|UD +B=0|ETB=155|HTB=51|URB=7"|
However file1 contains records beginning with 99, and file2 contains records that begin with E99. I thought the easiest way would be to put all records that belong to a certain HEADER on one line and compare that line from both files.

But I have run into one tiny problem. I have no idea how to compare strings across files. Here is the code that I have so far:

#!/usr/bin/perl -w use strict; my @files = <*.in.sep>; for(@files) { s/[.]in[.]sep//g } for my $file (@files) { open (IN, "<", "$file.in.sep") || die ("cannot open $file"); open (OUT,"<", "$file.out.sep") || die ("cannot open search.txt"); undef $/; my $in = <IN>; my $out = <OUT>; my @in = split /\n/, $in; my @out = split /\n/, $out; my @final; for $a (@in) { my @result = grep/^\Q$a\E$/, @out; push (@final , @result); } print "Strings that don't match: \t@final"; }
The last for loop is a bit of a bodge job, as I haven't done this before. Could one of you lovely people please help.

Thanks

Jim

In reply to Comparing strings from different files by Jalcock501

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (4)
    As of 2014-08-02 03:30 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Who would be the most fun to work for?















      Results (54 votes), past polls