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

I have a naming convention that goes a bit like this: Date1_Date2_type.asp All of the files in my array have this convention, my problme is that I need to use parts of these names for both comparisons to today's date, and to sort the files by Date1 or Date2. I have been trying to so this using a split, however I cannot see how it's possible. Does anyone have any suggestions to help me pull certain parts of my title and use them without losing the association to the asp file? Thanks, Bill
  • Comment on How do I sort by certain parts of a filename?

Replies are listed 'Best First'.
Re: How do I sort by certain parts of a filename?
by Masem (Monsignor) on May 10, 2001 at 03:26 UTC
    Well, a split using '_' will get you "Date1", "Date2" and "type.asp", the last you can probably drop. From there, it's a rather easy (!) application of sort:
    # this searches on Date1 my @sorted_list = sort { ${split(/_/,$a)}[0] cmp ${split(/_/,$b)}[0] } @list; # [0] is Date1, [1] is Date2, [2] is type.asp
    You could also apply the so called Schwartian Transform if your Dates are not directly compariable and need a special function:
    my @sorted_list = map { $_[0] } sort { date_compare($a[1],$b[1]) } # [1] is Date1, [2] is Date2, [3] is the type.asp map { [ $_, split /_/ ] } @list;

    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      Hmmm, I don't think you can assume anything about the date unless you see examples. Date could be in many different formats.

      Unless actual example and type of sort requested is posted by wstarrs, I don't think we can be that specific :)

      .02

      cLive ;-)

      Masem wrote:
      ${split(/_/,$b)}[0] }
      To be certain, I tried it, but it doesn't give me the first field of a split. Did you mean:
      [split /_/, $b]->[0]
      ?

      (This is what I usually do, but maybe there is a better Way out there....)

      Jeroen
      "We are not alone"(FZ)

        Even shorter (and faster) is: (split /_/, $b)[0]

                - tye (but my friends call me "Tye")
(Ovid) Re: How do I sort by certain parts of a filename?
by Ovid (Cardinal) on May 10, 2001 at 03:23 UTC

    Don't know if this is the most efficient:

    my @files = qw{ Date12_type.asp Date1_type.asp Date7_type.asp Date4_ty +pe.asp }; my @sorted_files = map { join '', @{$_} } sort { $a->[1] <=> $b->[1] } map { [(/([^\d]+)(\d+)(.*)/)] } @files; $,="\n"; print @sorted_files;

    The above code prints:

    Date1_type.asp Date4_type.asp Date7_type.asp Date12_type.asp

    I was just guessing as to the file names, as the example you provided was unclear to me. The actual regular expression may need to be adjusted. Also, the sort has been done numerically, which is what I guessed you wanted.

    If this doesn't get it done, post some filename samples along with sample sorted output.

    Cheers,
    Ovid

    Update: I think Masem gave a clearer (and better) answer. Vote for him.

    Update 2: cLive ;-) pointed out that I probably misread your post and, as a result, my sort won't work. Again, post some filenames and the expected sort order, and we can better answer this.

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: How do I sort by certain parts of a filename?
by cLive ;-) (Prior) on May 10, 2001 at 03:31 UTC
    Use a sort sub-routine. Don't know how you want to sort (you give no example :), but here's a start:
    # sample data my @filenames = '20001212_20011212_text.asp', '20021212_20031212_text.asp', '20031212_20041212_image.asp', '20011212_20021212_image.asp'); # loop for (sort { sort_me(); } @filenames) { # whatever print "$_\n"; } sub sort_me { # grab info from special vars $a and $b my ($a_date1,$a_date2) = split '_', $a; my ($b_date1,$b_date2) = split '_', $b; # then do your sort - eg, to sort by date1 then date2 # (in the date format I have used above) ($a_date1 <=> $b_date1) || ($a_date2 <=> $b_date2); }

    $a and $b are special vars used within a sort. Read this for more info.

    Note, this is a rough demo - not gonna test it unless you supply actual data and comparisons... but should give you an idea where to start.

    cLive ;-)

    Update - just realised you don't need to know type, so have amended code accordingly. later one, removed semi-colon that didn't affect outcome, but still looked out of place...