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

I have to write a script that can take a file that is in the format:
TRU-01(A),07Jul2008 03:00:01,ECG X/X,SAO2 X/X,NBP 115/59 77 02:41,CO2 +X/X/X,ST X/X/X,ST X/X/X,ST X/X/X,ST X/X/X
Most of the time, the file is missing certain statistics, such as SAO2, RESP, etc.

I need to be able to create a standardized formatting each time, so the log file will always have name1 data1, name 2 data2, etc.

However, the problem is that the data is often arranged differently, so NBP and its data might come before ECG and its data.

I think the easiest way would simply be to read the file for all the different names, sort those alphabetically, and use that to create a standard order for the data to appear in.

If anyone could suggest a way to do that, i would greatly appreciate it.

Replies are listed 'Best First'.
Re: Sort log file alphabetically
by pc88mxer (Vicar) on Jul 28, 2008 at 15:14 UTC
    Read in all the records into an array of hashes. Then you can decide how to re-format/sort them them.

    Here's a simple parser based on what your format seems to be:

    my @list; while (<>) { chomp; my (%hash, @rest); ($hash{first}, $hash{date}, @rest) = split(",", $_); for my $r (@rest) { my ($k, $v) = split(' ', $r, 2); $hash{$k} = $v; } push(@list, \%hash); }
    Now it doesn't matter what order the fields are in. To determine all the fields present in any of the records, you can use:
    my %seen; for (@list) { for (keys %$_) { $seen{$_}++ } }; delete $seen{first}; delete $seen{date}; my @allkeys = ('first', 'date', sort keys %seen);
    I'm just assuming that you want the first two fields to remain where they are. The rest will be sorted alphabetically.
      thanks, the problems been solved thanks to your help.
      thanks, the stuff u posted seems to work fine, as it prints out an alphabetized list of all the different names when i add a print command to the end. However, how would i tell perl to rearrange each line according to this order? especially since i need to make sure the correct data stays with the identifier
        You don't have to worry about the data staying with the identifier since that association is saved in the hash for the line.

        Re-formatting the lines is just looping over @list:

        my @keys = ...order you want the named keys in... for my $h (@list) { print join(",", $h->{first}, $h->{date}, map { $_." ".$h->{$_} } @keys), "\n"; }
Re: Sort log file alphabetically
by numberninja (Initiate) on Jul 28, 2008 at 15:11 UTC
    it might be important to mention that there must always be placeholders for data. (RESP,NBP "data" wouldnt work, but RESP "placeholders",NBP "data" would)I think that's something i can create myself, but it might be of concern.