Ah, this is classic split territory. Also, hashes were made for precisely this kind of thing, so let's roll that way, too. Now is as good of a time as any to give them a try, right?

The following code shows one way of doing what you want:

#!/usr/bin/perl use warnings; use strict; # read the files into a hash of ( filename => title ) my %files; while (<DATA>) { chomp; # get rid of line-ending if (my ($file, $title) = split ' ', $_, 2) { $files{$file} = $title; } } # print out the files in our hash, sorted by file name foreach my $file (sort keys %files) { my $title = $files{$file}; print "$file = $title\n"; } __DATA__ RS0029.DOC INTER UNIT HARNESS REQUIREMENT SPECIFICATION RS0036.DOC INSTRUMENT ELECTRONICS UNIT RS0037.DOC MECHANISM CONTROL ELECTRONICS RS0041.DOC IOU DESCAN MECHANISM RS0042.DOC IOU GENERIC MECHANISMS
(For convenience, I put the list of files in the code's __DATA__ section, but you'll read them from a separate file.)

The only tricky part is our split invocation, which says, "split lines on whitespace into two parts." If we're successful in splitting the current line, we get back the filename and its title, which we store in the variables $file and $title respectively. These, in turn, we store in a hash called, appropriately enough, %files.

The foreach loop shows how to read values out of the hash. I just print them out, but I trust that you can convert each into the appropraite hypertext link. Here's the code's output:

RS0029.DOC = INTER UNIT HARNESS REQUIREMENT SPECIFICATION RS0036.DOC = INSTRUMENT ELECTRONICS UNIT RS0037.DOC = MECHANISM CONTROL ELECTRONICS RS0041.DOC = IOU DESCAN MECHANISM RS0042.DOC = IOU GENERIC MECHANISMS

Cheers,
Tom


In reply to Re: searching a file, results into an array by tmoertel
in thread searching a file, results into an array by Anonymous Monk

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.