Hi Ryan, Try the the code below, it should help. ==========<CODE>=============
#!/usr/bin/perl -w use strict; my $ln=""; my ($yr,$cat,$win)=("","",""); my $filename="New_output.txt"; chomp(my $raw_file=<@ARGV>); open READFILE,"<","$raw_file" or die "can't open $!"; open OUTPUTFILE,">","$filename" or die "cannot read $!"; while(<READFILE>){chomp; $ln.="\n" if /^\W.?+$/; if(/^\d{4}/){$yr=$&;} # get the year if(/^[A-Z].+/){ $cat=$&; # get the Category $cat=join"",split /,/,$cat; # remove the comma in front $ln.=" $yr: ".$cat; # add both the year and Category } if(/\--.+/){$win=$`; # get the winner $win=join"",split /[\*,\"]/,$win; $ln.=": ".$win."\n"; #### If you use "$ln.=": ".$win. $&. "\n";" #### you get "-- It Happened One Night {""Peter Warne""}",etc added to what you have } } print OUTPUTFILE $ln; close OUTPUTFILE; close READFILE; ============================================================ I used special match variables ($`, $&, and $'), which means $` ==> before match variable, $& ==> match variable and $' ==> after match variable. If the code doesn't like you want it you might have to play around with regular expressions! Regards. On Thu, Aug 4, 2011 at 2:38 AM, Ryan Lagola <carped...@live.com> wrote: > Hello, > I have been scratching my head on this problem and was wondering if someone > can help me out. Basically I need to take a raw list of data (a snippet of > it is below my email) and create another file with the information > formatted > in the following format: "Date: Category: Winner." The example of the > finished file is as follows: > > 1934: Actor: Clark Gable > 1934: Actress: Claudette Colbert > 1934: Art Direction: The Merry Widow > > As I am not a programmer by nature, I'm trying to figure out how to work > out > the logic of this program. The "Date" does not repeat with each category > but only changes when the next year of results is displayed in the data > file. How do I setup my logic to support this? Any help that can be > provided would be much appreciated. > > Just an FYI - here is my crack at finding the lines that match each > attribute: > Date: print $_ if $_ =~ /^(\d{4})*/ > #look for four digits at the beginning of a string > Category: print $_ if $_ =~ /^[A-Z]+/ > #look for one or more all caps characters at the beginning of a string > Winner: print $_ if $_ =~ /(\*)*(--)/ > #look for a field that starts with an asterisks and contains "--" > > I am open to comments on my regular expressions. Thanks! > > > ==================== SNIPPET OF RAW DATA FILE ==================== > > 1934 (7th), > ACTOR, > *,"Clark Gable -- It Happened One Night {""Peter Warne""}" > ACTRESS, > *,"Claudette Colbert -- It Happened One Night {""Ellie Andrews""}" > ART DIRECTION, > *,"The Merry Widow -- Cedric Gibbons, Fredric Hope" > ,[NOTE: won by two votes] > ASSISTANT DIRECTOR, > *,Viva Villa! -- John Waters > CINEMATOGRAPHY,per > *,Cleopatra -- Vicxtor Milner > DIRECTING, > *,It Happened One Night -- Frank Capra > FILM EDITING, > *,Eskimo -- Conrad Nervig > MUSIC (Scoring), > *,"One Night of Love -- Columbia Studio Music Department, Louis Silvers, > head of department (Thematic Music by Victor Schertzinger and Gus Kahn)" > MUSIC (Song), > *,"""The Continental"" from The Gay Divorcee -- Music by Con Conrad; Lyrics > by Herb Magidson" > OUTSTANDING PRODUCTION, > *,It Happened One Night -- Columbia > SHORT SUBJECT (Cartoon), > *,"The Tortoise and the Hare -- Walt Disney, Producer" > SHORT SUBJECT (Comedy), > *,"La Cucaracha -- Kenneth Macgowan, Producer" > SHORT SUBJECT (Novelty), > *,"City of Wax -- Stacy Woodard and Horace Woodard, Producers" > SOUND RECORDING, > *,"One Night of Love -- Columbia Studio Sound Department, John Livadary, > Sound Director" > WRITING (Adaptation), > *,It Happened One Night -- Robert Riskin > WRITING (Original Story), > *,Manhattan Melodrama -- Arthur Caesar > SPECIAL AWARD, > *,"To Shirley Temple, in grateful recognition of her outstanding > contribution to screen entertainment during the year 1934." > SCIENTIFIC OR TECHNICAL AWARD (Class II), > *,"To ELECTRICAL RESEARCH PRODUCTS, INC. for their development of the > Vertical Cut Disc Method of recording sound for motion pictures (hill and > dale recording). [Sound]" > SCIENTIFIC OR TECHNICAL AWARD (Class III), > *,"To COLUMBIA PICTURES CORPORATION for their application of the Vertical > Cut Disc Method (hill and dale recording) to actual studio production, with > their recording of the sound on the picture One Night of Love. [Sound]" > *,To BELL AND HOWELL COMPANY for their development of the Bell and Howell > Fully Automatic Sound and Picture Printer. [Laboratory] > , > 1935 (8th), > ACTOR, > *,"Victor McLaglen -- The Informer {""Gypo Nolan""}" > ACTRESS, > *,"Bette Davis -- Dangerous {""Joyce Heath""}" > ART DIRECTION, > *,The Dark Angel -- Richard Day > ASSISTANT DIRECTOR, > *,"The Lives of a Bengal Lancer -- Clem Beauchamp, Paul Wing" > CINEMATOGRAPHY, > *,A Midsummer Night's Dream -- Hal Mohr > ,[NOTE: THIS IS NOT AN OFFICIAL NOMINATION. Write-in candidate.] > DANCE DIRECTION, > *,"Dave Gould -- ""I've Got a Feeling You're Fooling"" number from Broadway > Melody of 1936; and ""Straw Hat"" number from Folies Bergere" > DIRECTING, > *,The Informer -- John Ford > FILM EDITING, > *,A Midsummer Night's Dream -- Ralph Dawson > MUSIC (Scoring), > *,"The Informer -- RKO Radio Studio Music Department, Max Steiner, head of > department (Score by Max Steiner)" > MUSIC (Song), > *,"""Lullaby of Broadway"" from Gold Diggers of 1935 -- Music by Harry > Warren; Lyrics by Al Dubin" > OUTSTANDING PRODUCTION, > *,Mutiny on the Bounty -- Metro-Goldwyn-Mayer > SHORT SUBJECT (Cartoon), > *,"Three Orphan Kittens -- Walt Disney, Producer" > SHORT SUBJECT (Comedy), > *,"How to Sleep -- Jack Chertok, Producer" > SHORT SUBJECT (Novelty), > *,Wings over Mt. Everest -- Gaumont British and Skibo Productions > SOUND RECORDING, > *,"Naughty Marietta -- Metro-Goldwyn-Mayer Studio Sound Department, Douglas > Shearer, Sound Director" > WRITING (Original Story), > *,"The Scoundrel -- Ben Hecht, Charles MacArthur" > WRITING (Screenplay), > *,The Informer -- Dudley Nichols > ,"[NOTE: Mr. Nichols initially refused the award, but Academy records > indicate that he was in possession of a statuette by 1949.]" > SPECIAL AWARD, > *,"To David Wark Griffith, for his distinguished creative achievements as > director and producer and his invaluable initiative and lasting > contributions to the progress of the motion picture arts." > SCIENTIFIC OR TECHNICAL AWARD (Class II), > *,To AGFA ANSCO CORPORATION for their development of the Agfa infra-red > film. [Film] > *,To EASTMAN KODAK COMPANY for their development of the Eastman > Pola-Screen. > [Lenses and Filters] > SCIENTIFIC OR TECHNICAL AWARD (Class III), > *,"To METRO-GOLDWYN-MAYER STUDIO for the development of anti-directional > negative and positive development by means of jet turbulation, and the > application of the method to all negative and print processing of the > entire > product of a major producing company. [Laboratory]" > *,"To WILLIAM A. MUELLER of Warner Bros.-First National Studio Sound > Department for his method of dubbing, in which the level of the dialogue > automatically controls the level of the accompanying music and sound > effects. [Sound]" > *,"To MOLE-RICHARDSON COMPANY for their development of the ""Solar-spot"" > spot lamps. [Lighting]" > *,To DOUGLAS SHEARER and METRO-GOLDWYN-MAYER STUDIO SOUND DEPARTMENT for > their automatic control system for cameras and sound recording machines and > auxiliary stage equipment. [Stage Operations] > *,"To ELECTRICAL RESEARCH PRODUCTS, INC. for their study and development of > equipment to analyze and measure flutter resulting from the travel of the > film through the mechanisms used in the recording and reproduction of > sound. > [Sound]" > *,"To PARAMOUNT PRODUCTIONS, INC. for the design and construction of the > Paramount transparency air turbine developing machine. [Laboratory]" > *,"To NATHAN LEVINSON, Director of Sound Recording for Warner Bros.-First > National Studio, for the method of intercutting variable density and > variable area sound tracks to secure an increase in the effective volume > range of sound recorded for motion pictures. [Sound]" > , >