in reply to Re^2: Code Critique
in thread Code Critique
This is a very simple CSV file (no quote characters or embedded quotes within quotes), so just split on the comma's and then do another split on space to get the basic value and measurement units from the line. I used what is called a list slice to assign directly to variables without any numeric subscript stuff.
The "formula" for this sort of thing is: open files, read and separate the input data into one "record" (a "quanta" if you will that makes sense for processing), either save that for processing in a collection of those "records" or process the records as you go.
In your first example, I don't think there is a need to save anything past the "current record". For this one, I don't know.. explain more and show more...
If a new problem is being discussed, please start a new node.#!/usr/bin/perl use strict; use warnings; # the same as #!/usr/bin/perl -w in first line while (<DATA>) { next if /^\s*$/; # skip blank lines # sometimes a last blank line # causes problems! s/\s*$//; # or chomp; fine also my ($date, $description, $measurement) = (split (/,/,$_))[0,1,3]; my ($value, $units) = split (/\s+/,$measurement); print "date = $date\n", "descript = $description\n", "value = $value\n", "units = $units\n\n"; # instead of print, do something here ... } =prints date = 20/08/2007 descript = Erythrocyte sedimentation rate value = 3 units = mm/h date = 20/08/2008 descript = Total white blood count value = 6.7 units = 10*9/L date = 04/04/2007 descript = Haemoglobin estimation 12.9 g/dL value = 12.9 units = g/dL =cut __DATA__ 20/08/2007,Erythrocyte sedimentation rate,,3 mm/h 20/08/2008,Total white blood count,,6.7 10*9/L 04/04/2007,Haemoglobin estimation 12.9 g/dL,,12.9 g/dL
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Code Critique
by rhiridflaidd (Novice) on Oct 05, 2010 at 06:57 UTC | |
by Marshall (Canon) on Oct 05, 2010 at 08:15 UTC | |
by rhiridflaidd (Novice) on Oct 08, 2010 at 09:42 UTC | |
by Marshall (Canon) on Oct 10, 2010 at 00:05 UTC | |
by rhiridflaidd (Novice) on Oct 19, 2010 at 22:17 UTC |