Reading tab delimited File & sort everything according item 5 of every line

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Reading tab delimited File & sort everything according item 5 of every line

    let say that the file contains these items (every item is seperated
    with a tab)

    one title state name code1 number
    two title2 state2 name2 code2 number2
    one title3 state3 name3 code3 number3
    four title4 state4 name4 code4 number4
    six title5 state5 name5 code1 number5
    dip title6 state6 name6 code1 number6
    fun title7 state7 name7 code2 number7

    the thing i'am looking for is that it is sorted by item 5 and writes
    back to the file
    with an extra line if item 5 is different

    so I would come up with:

    one title state name code1 number
    six title5 state5 name5 code1 number5
    dip title6 state6 name6 code1 number6

    two title2 state2 name2 code2 number2
    fun title7 state7 name7 code2 number7

    one title3 state3 name3 code3 number3

    four title4 state4 name4 code4 number4


    Is there someone that can help me
    --
    Oooo.
    oooO ( )
    ( ) ) /
    \ ( (_/
    \_)

    thanks
    Bjorn Van Blanckenberg

    Bjorn Van Blanckenberg Guest

  2. Similar Questions and Discussions

    1. Reading Last Line in a Txt File
      Hi, I'm trying to read the last line in a file using "ASFileRead" function. Im having trouble specifying the last parameter which counts the...
    2. Reading a Comma Delimited File
      Hello all, So as the title says, I need to read a comma delimited file generated by MS Excel. I know that by doing GetListAt() or ListLen()...
    3. Parsing pipe delimited file
      Hello everyone, Thanks to everyone who helped with my last problem last week. I've hit a snag in another problem this week. I need to parse...
    4. Reading a line at a time from a file
      Hey, Just wondering what the best way to do this is? What i specifically want to do is check if a user exists on a unix machine. The best way...
    5. Reading a scalar line by line
      Hi, does anyone know how I would do this? something like: while ($string) print _$ } Google searches did not turn up anything.
  3. #2

    Default Re: Reading tab delimited File & sort everything according item 5 of every line

    On 01/23/04 03:36, Bjorn Van Blanckenberg wrote:
    > the thing i'am looking for is that it is sorted by item 5 and writes
    > back to the file
    > with an extra line if item 5 is different
    A variation on the Swartzian transform: (read from bottom up)

    #!/usr/bin/perl

    use strict;
    use warnings;

    print map { $_->[1] . "\n" }
    sort {$a->[0] cmp $b->[0]}
    map { chomp; [(split /\t/, $_)[4], $_] }
    <DATA>;

    __DATA__
    one title state name code1 number
    two title2 state2 name2 code2 number2
    one title3 state3 name3 code3 number3
    four title4 state4 name4 code4 number4
    six title5 state5 name5 code1 number5
    dip title6 state6 name6 code1 number6
    fun title7 state7 name7 code2 number7
    Randy W. Sims Guest

  4. #3

    Default Re: Reading tab delimited File & sort everything according item 5 of every line

    On Fri, 23 Jan 2004 09:36:00 +0100
    Bjorn Van Blanckenberg <bjornvb@ppc.be> wrote:
    > let say that the file contains these items (every item is seperated
    > with a tab)
    >
    > one title state name code1 number
    > two title2 state2 name2 code2 number2
    > one title3 state3 name3 code3 number3
    > four title4 state4 name4 code4 number4
    > six title5 state5 name5 code1 number5
    > dip title6 state6 name6 code1 number6
    > fun title7 state7 name7 code2 number7
    >
    > the thing i'am looking for is that it is sorted by item 5 and writes
    > back to the file
    > with an extra line if item 5 is different
    >
    > so I would come up with:
    >
    > one title state name code1 number
    > six title5 state5 name5 code1 number5
    > dip title6 state6 name6 code1 number6
    >
    > two title2 state2 name2 code2 number2
    > fun title7 state7 name7 code2 number7
    >
    > one title3 state3 name3 code3 number3
    >
    > four title4 state4 name4 code4 number4

    Well you can try;
    -------------------------------------------------------------



    #!/usr/bin/perl -w

    chomp(@fields = <DATA>); # slurp in the file
    $lastbit=1;

    @sorted =
    map { $_->[0] }
    sort { $a->[5] cmp $b->[5] }
    map { [ $_ , (split /\t/) ] } @fields;#tab separated fields

    foreach (@sorted){
    @bits = split;
    print "\n" if ($bits[4] ne $lastbit);
    print "$_\n";
    $lastbit=$bits[4];
    }

    __DATA__
    one title state name code1 10
    two title2 state2 name2 code2 21
    one title3 state3 name3 code3 13
    four title4 state4 name4 code4 14
    six title5 state5 name5 code1 number5
    dip title6 state6 name6 code1 number6
    fun title7 state7 name7 code2 number7

    ----------------------------------------------
    and it produces


    21:42:56 [~/perltest]#perl sortdata1.pl

    one title state name code1 10
    six title5 state5 name5 code1 number5
    dip title6 state6 name6 code1 number6

    two title2 state2 name2 code2 21
    fun title7 state7 name7 code2 number7

    one title3 state3 name3 code3 13

    four title4 state4 name4 code4 14



    --
    Owen
    Owen Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139