in reply to Sorting big text lists

Functions of particular interest to you will be split and sort. Here's a code example to get you on your way.

#! /usr/bin/perl use strict ; use warnings ; $|++ ; # Read the data into a 2d array chomp( my @lines = <DATA> ) ; my @data = () ; foreach ( @lines ) { my @cells = split ; push @data, \@cells ; } # Sort the list by user. my @data_by_name = sort { $a->[0] cmp $b->[0] } @data ; # Sort the list by date & time. my @data_by_date = sort { $a->[3] <=> $b->[3] || $a->[4] <=> $b->[4] } @data ; __DATA__ u12345 x10 qwerty 20020725 1421 u12357 x11 asdf;; 20020727 1524 u12245 x12 perl 20020722 1941 u12145 x13 python 20020725 1825 u12945 x14 /bin/sh 20020724 1331 u12545 x15 grep 20020721 1921

_______________
D a m n D i r t y A p e
Home Node | Email

Replies are listed 'Best First'.
Re: Re: Sorting big text lists
by Chady (Priest) on Jul 31, 2002 at 13:55 UTC
    foreach ( @lines ) { my @cells = split ; push @data, \@cells ; }

    I wouldn't exactly be showing this to a newbie. As the original poster pointed out, he has "never even programmed", I have been using perl for about two years, and I still have some problems with references..

    maybe building a hash with original lines as keys and specific column as values would be easier to understand (but maybe harder on the memory, depends on how big is the list.) don't you agree?


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/

      ++, though I tend to disagree regarding references. Your suggestion was to build a hash with original lines as keys, and a specific column as values. To me, that seems inefficient, and no easier to understand. The data is already in a tabular format. The operations we want to do (sorting by a specific column) are easily realized with tabular data. Why not, then, continue to treat the data as a table once it's loaded into memory? The only thing I might do differently here would be to use an AoH (array of hashes) instead of an AoA (array of arrays), thus giving the columns names instead of numbers.

      If I am wrong, and the code I presented is actually way over the original poster's head, forgive me. Here are some resources that will help:


      _______________
      D a m n D i r t y A p e
      Home Node | Email