Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a small dilemma (It is large for me but with so many experts I am sure it is small for you).

I have this file that is passed to my script every time (it is a required option within my script) the problem I have is sometimes the data will come in the wrong order. Order should be "ACTION, TITLE, WPR_ID, US_RELEASE_DATE, TITLE_TALENT, GENRE, FOX_ENTITY, ACQUSITION, PRODUCTION_RELATION, THEATRICAL_RIGHT" but when the file comes all this data could be (and has been) in different locations.

The file that I open will always have the “title” line of which should read like the values listed above as the first line. The actual data I need are the values listed directly below the “title” line. Example:
ACTION TITLE WPR_ID …
Insert Gorilla War Fare TBD …

I need to build some kind of reference point that has the constant order of the “should be model” and then reference the file that is opened to the “should be model”.

Any ideas? Thoughts? Need more information? This is hard to describe but I hope I have done a half way decent job. Please let me know what you can come up with.

Thank you,
Robert

Replies are listed 'Best First'.
Re: Complex Mapping
by hsmyers (Canon) on Jun 27, 2003 at 20:02 UTC
    This is what hashes are for. Your parser can be built to 'not care' about the order of the information it gets. All it has to do is put the 'TITLE' information in the 'TITLE' place, i.e. %things{'TITLE'} = $result_of_parsing_title_information; When you finish parsing, the hash now has everything you've gathered, avaiable in whatever order you wish.

    --hsm

    "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
Re: Complex Mapping
by artist (Parson) on Jun 27, 2003 at 20:12 UTC
    How you are reading the data from the datafile?

    If first line always mention the 'titles' and second line mentions the values in the same order as 'titles', then you work is easy. Assuming you have no spaces in your individual data-value, here is the code that can help

    chomp($title_line = <DATAFILE>); chomp($value_line = <DATAFILE>); @titles = split /\s+/,$title_line; @values = split /\s+/,$value_line; map { $Data->{$_} = shift @values } @titles;
    Thus you have build the hash of Keys and values which you can use in your script directly or use reference file to write these key-values and read from it.

    artist

      man i love map solutions, but just in case anonomo robert would like to see a map-less version...

      my $order_should_be = 'ACTION,TITLE,WPR_ID,US_RELEASE_DATE,TITLE_TALEN +T,GENRE,FOX_ENTITY,ACQUSITION,PRODUCTION_RELATION,THEATRICAL_RIGHT'; my @should_be_model = split /\,/, $order_should_be; my $file_layout = <DATA>; my @file_layout_keys = split /\,/,$file_layout; while ( my $file_data = <DATA> ) { # build a hash of the file data my %hash; my @file_data_values = split /\,/,$file_data; for (@file_layout_keys) { $hash{$_} = shift @file_data_values; } # print it out in the correct order: foreach (@should_be_model) { # take care of unintialzed data $hash{$_} = '' if ! $hash{$_}; print "$_ => $hash{$_}\n"; } } __DATA__ TITLE,US_RELEASE_DATE,GENRE The Adventures of Buckaroo Banzai Across the 8th Dimension,1984,ACTION Highlander,1986,ACTION The Adventures of Bob & Doug McKenzie: Strange Brew,1983,COMEDY

      also i assumed these are comma separated values. otherwise, what will you do if there are spaces in the title?