in reply to Re^2: Bug in Sort::Fields?
in thread Split(), Initial Spaces, & a limit?

regardless of where the data is (field 1 or field 2).

The key must be either in field 1 or in field 2. It can't vary by row. You're providing

Field 1 Field 2 Field 3 ----------- ----------- ----------- 56 1752.eps "", "56", "1752.eps" key in 2 1160 trace.exe "1160", "trace.exe" key in 1 123 foo bar.pl "123", "foo", "bar.pl" key in 1

You need to normalize your fields so that they are the same for each row. I did it by removing the extraneous delimiter in the front of some lines.

Field 1 Field 2 Field 3 ----------- ----------- ----------- 56 1752.eps "56", "1752.eps" key in 1 1160 trace.exe "1160", "trace.exe" key in 1 123 foo bar.pl "123", "foo", "bar.pl" key in 1

You could also add an extraneous delimiter to the lines that don't have one.

Field 1 Field 2 Field 3 Field 4 ----------- ----------- ----------- ----------- 56 1752.eps "", "56", "1752.eps" 1160 trace.exe "", "1160", "trace.exe" 123 foo bar.pl "", "123", "foo", "bar.pl"

By the way, why not just let ls do the sorting if you're going to use ls?

Update: Improved visuals.

Replies are listed 'Best First'.
Re^4: Bug in Sort::Fields?
by cmv (Chaplain) on Jul 20, 2010 at 19:14 UTC
    Yes, I agree with you that the key cannot vary by row.

    Since this is what Sort::Fields is doing, are you agreeing with me that this is a bug in that module?

    As I implied earlier, the module should sort the exact same data, the same way, no matter where that data shows up (field 1, field 2, or field N).

    Thanks

    -Craig

    update: Ah, sorry, you were still editing, and added more to the reply while I was responding.

    I think my point is that Sort::Fields will "do the right thing" if the data containing initial spaces is in any place other than field 1. If the data is in field 1, then it does something different.

    I believe this is because the author specifically uses /\s+/ as the field delimiter in his code. This works fine for every field except field 1 (as shown by the discussion at the beginning of this post). I would like to see this module do the same thing on the same data, no matter what field it show up in.

    I believe this should be easily fixable, by replacing the /\s+/ with /' '/, as you showed me in an earlier post. I just can't figure out how to do that in his code. I also don't know what side effects that would have.

      Yes, I agree with you that the key cannot vary by row.

      Then don't pass such data to Sort::Fields.

      are you agreeing with me that this is a bug in that module?

      Most definitely not. It's sorting using the specified field as the key. It's not its fault you didn't put the key in that field.

      I would like to see this module do the same thing on the same data, no matter what field it show up in.

      That's exactly what it does.

      What's left of the first space group is the first field.
      What's left of the second space group is the second field.
      What's left of the third space group is the third field.

      You're the one who wants to treat the first field specially. That's not a problem, but you'll have to do it yourself.

        ikegami-

        This is an extremely interesting discussion for me, thanks for staying with it.

        It seems that my perception is that the first field is not being treated the same, and I'm wishing it was.

        I'm starting to understand that your perception is that it is being treated the same.

        I offer this bit of code to try and show my perspective:

        use strict; use warnings; use Sort::Fields; use Data::Dumper; my @data = `ls -ls | tail -5`; chomp(@data); print "Field 1 sort:\n", Dumper(fieldsort( ['1n'], @data)); @data = `ls -l | tail -5`; chomp(@data); print "Field 5 sort:\n", Dumper(fieldsort( ['5n'], @data));
        The field 1 sort is not sorted at all. We know why, we've been beating that poor horsie all afternoon.

        The field 5 sort is sorted correctly. Only because field 1 didn't have any leading spaces in any of the lines.

        What I'm hearing you say is "As long as there are no leading spaces in field 1, Sort::Fields will treat all fields the same way".

        What I believe I'm saying is "If Sort::Fields can handle initial spaces in field 5, why does it handle initial spaces in field 1 differently?".

        Is that a fair assessment?