in reply to searching a file, results into an array
The regex is rather simple for this but I think split would be even better (more efficient) if you are sure they will always be seperated by a first tab and any line with a first tab is valid. I put them into an array of arrays, more efficient than a hash keyed on doc if you only ever want to read through them sequentialy. Just another way to do it....
Note the regex given is a bit more fussy than the obvious /(.*)\t(.*)/ which would cause you grief if the title contained a tab (if you don't know why read up about greedy pattern matching, it is very important)#!/usr/local/bin/perl -w use strict; my @documents; while (<DATA>) { # uncomment following line for the regex way # if (/^([\S]*.DOC)\t(.*)/) {push @documents, [$1, $2]} # uncomment these to use the split method # chomp; # next unless (my ($doc, $title)=split /\t/, $_, 2); # push @documents, [$doc, $title]; } print "I found the following docs\n\n"; foreach (@documents) { print "Doc: $_->[0] \t Title: $_->[1]\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
Cheers,
R.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: searching a file, results into an array
by perlcapt (Pilgrim) on Oct 14, 2004 at 02:58 UTC | |
by Random_Walk (Prior) on Oct 14, 2004 at 10:41 UTC |