I'm trying to read a textfile that is broken up into several profiles. Each profile is separated by a blank line. It looks something like this:
pos = 004 2574 2633 2571 2566 0088,1491,2365 0088,1514,2134 0088,1531,2100 0088,1568,2081 0088,1631,2070 0088,1716,2013 0088,1818,1954 0088,1921,1917 0088,2019,1889 0088,2108,1859 0088,2193,1881 0088,2266,1907 0088,2339,1843 0088,2398,1876 0088,2453,1822 0088,2488,1849 AB: 01000050 T%: 0456 INR: 018017 ID: 00004115 pos = 007 2470 2505 2484 2573 0088,1468,2406 0088,1473,2275 0088,1474,2143 0088,1482,2083 0088,1489,2009 0088,1510,1924 0088,1535,1965 0088,1573,1958 0088,1615,1935 0089,1667,1935 0088,1718,1935 0089,1770,1936 0088,1822,1943 0088,1869,1954 0089,1915,1965 0089,1961,1935 0089,1999,1932 0089,2034,1966 0089,2068,1942 0089,2098,1959 0089,2127,1976 0089,2152,1945 AB: 01000050 T%: 0725 INR: 018034 ID: 00004115
Sometimes these textfiles contatin several copies of the same profile. So I want to collapse this data into an array where inputting the same key erases the original value and replaces it with the current value so there's only one unique profile for each key. Here's the code that I've written thus far, but it isn't working at all.
open(IN, "testB.txt") or die "Can't open testB.txt"; open(OUT, ">>testB8.txt")or die "Can't open the file for writing.\n"; %Array = (); #Create an empty array $/ = "" ; #Enable paragraph mode $* = 1; #Enable multi-line strings #Read each paragraph in as a string ($Profile). Split the #first line of the string so that the first line is the #$Key and the rest of the $Profile is the $Value. Then #input these into the %Array. while (<IN>) { $_ = $Profile; ($Key, $Value) = split(/\r/, $Profile, 2); %Array = ("$Key" => "$Value"); } #For each $Key of the %Array, first sort the keys, then #print their associated values in the textfile OUT. foreach $Key (sort(keys(%Array))) { print (OUT "$Array{$Key} \n"); }
Any and all help is MUCH appreciated!

In reply to Arrays and regular expressions by mel1rose

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.