Thanks so much! This is great because I'm new to Perl and your comments make things much easier to understand!
Thanks again, Debbie -----Original Message----- From: Jeff 'japhy' Pinyan [mailto:[EMAIL PROTECTED] Sent: Wednesday, June 16, 2004 2:54 PM To: Debbie Cooper Cc: [EMAIL PROTECTED] Subject: Re: Replace tab delimited headings in file On Jun 16, Debbie Cooper said: >I need to replace the tab delimited headings in a text file so that I end up >with shorter (lengthwise) headings. I have headings like the following: > >Heading_1,122 <tab> Heading_2,122 <tab> AnotherHeading_1,1 .......... I'm assuming this will be the *first* line of a file with data in it. >I want to replace these headings with something like the following: > >x1 <tab> x2 <tab>.... and so on > >but I need to keep some kind of file that cross references x1 with >Heading_1,122. > >Is this something that Perl can handle? Certainly. Here's how I might approach the problem: open INDEX, "> heading.idx" or die "can't create heading.idx: $!"; { # $^I dictates the back-up file's extension # and @ARGV holds the file(s) we're working on. # these two together, plus the use of <>, results # in magical in-place file editing. local ($^I, @ARGV) = (".bak", "tabbed-data.txt"); my $head = "x00"; while (<>) { # if we're dealing with the first line, # change the headings to simpler ones if ($. == 1) { chomp; # remove newline my @headings = split /\t/; # get each of the headings for my $old (@headings) { $head++; # generate new short heading print INDEX "$old\t$head\n"; # cross-reference $old = $head; # replace with new heading } # @headings now has (x01, x02, x03, ...) # change $_ to reflect new headings $_ = join("\t", @headings) . "\n"; } print; } } close INDEX; I haven't tested it (but the code WILL produce a backup file of your data, so you needn't fear losing it), but that should do the trick. It modifies your data file to have the headers be x01, x02, etc., and creates a file called heading.idx which holds tab-separated data in it, the old and the new headings: Heading_1,122 x01 Heading_2,122 x02 AnotherHeading_1,1 x03 This is only one way to solve the problem. -- Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ CPAN ID: PINYAN [Need a programmer? If you like my work, let me know.] <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>