Great!

I did write some more code for you to demonstrate subroutines and I hope amplify my point about indenting.

At some point, you may want to sort these test records by date. You actually have a very good basic date format to work from as there are leading zero's for the month and date.

Below I show some code that has 2 subroutines, one to convert the file's date format into something that can be easily sorted as a simple string and one to convert it back. VERY important here is that it is easy to understand what code belongs to what subroutine!

Clarity of thought and the grouping of those thoughts into logical, readable "units" is the single most important thing that you can do towards writing "great code".

Go for the improvements and report back with progress! I understand more about your record format in the input file. And Perl has some very cool ways of dealing with record processing like this. But at this point, you need to do more work before things can proceed further. And I'm sure you will do that.

#!/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 chomp; # removes trailing \n but not spaces my ($date) = split (/[,]/,$_); print "date_original = $date\n", "date_sortable = ", year_first($date), "\n", "original date back = ", day_first(year_first($date)), "\n\n"; } sub year_first # convert 20/08/2007 to 2007-08-20 { my ($date) = @_; # one way to get the sub's input value my @tokens = split (/\//, $date); @tokens = reverse @tokens; my $new_date = join('-',@tokens); return $new_date; } sub day_first # convert 2007-08-20 to 20/08/2007 { my $date = shift; # another way to get a single value my @tokens = split (/-/, $date); @tokens = reverse @tokens; my $normal_date = join('/',@tokens); return $normal_date; } =prints date_original = 20/08/2007 date_sortable = 2007-08-20 original date back = 20/08/2007 date_original = 20/08/2008 date_sortable = 2008-08-20 original date back = 20/08/2008 date_original = 04/04/2007 date_sortable = 2007-04-04 original date back = 04/04/2007 =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

In reply to Re^5: Code Critique by Marshall
in thread Code Critique by rhiridflaidd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.