Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: database sorter script

by santander (Acolyte)
on Dec 06, 2004 at 12:25 UTC ( #412634=note: print w/ replies, xml ) Need Help??


in reply to Re: database sorter script
in thread database sorter script

Sorry, its flat text database, books catalog, sort by authors data: Last Name, First Name.


Comment on Re^2: database sorter script
Re^3: database sorter script
by bart (Canon) on Dec 06, 2004 at 12:35 UTC
    Then you'll have to go through the records, using grep if you want to filter the whole list at once, or using if if going through them one at a time.

    Assuming you have a hash per record:

    • One at a time:
      my @section; while(my $r = $db->fetchrow) { push @section, $r if $r->{name} =~ /^A/i; }
    • All at once:
      my @section = grep { $_->{name} =~ /^A/i } $db->fetchrows;
    This is pseudocode, you must fill in the details on how to get the data from the flatfile, yourself.
Re^3: database sorter script
by tachyon (Chancellor) on Dec 06, 2004 at 12:36 UTC

    Assuming it is relatively small and unsorted by default:

    #!/usr/bin/perl -w my $db = '/some/file'; my $letter = 'Y'; open DB, $db or die "Can't read $db: $!\n"; my @matches; while(<DB>){ push @matches, $_ if m/^$letter/i; } close DB; print sort @matches;

    Or more succinctly on *nix:

    $ grep -i -e ^Y db.file | sort

    cheers

    tachyon

      To be more detail, database contain near 9000 records; top part contains some 'digital' names, then goes a normal alphabet.
      example:

      666
      666
      Active
      040 Feat Erica Baxter
      16B
      2 Brothers On The 4th Floor
      2 Eivissa
      2 Unlimited
      20 Fingers
      2Pac
      3-O-Matic
      4 Clubbers
      4-2 The Floor
      60 Minute Man
      A Trance Communications Classic
      A. Kay B.J.
      A.D.A.M. Feat. Amy
      A1
      Aaliyah
      AB Logic
      ABBA
      Above and Beyond
      Accela
Re^3: database sorter script
by thor (Priest) on Dec 06, 2004 at 12:40 UTC
    A way to do this would be like this:
    my $letter = "B"; open(my $in, "input_file") or die "Couldn't open file for read: $!"; while(<$in>) { print if m/^$letter/; }
    You could improve it slightly by stopping once you're done processing a certain letter, but I leave that as an exercise to the reader...:)

    thor

    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    For all of us waiting, your kingdom will come

Re^3: database sorter script
by hmerrill (Friar) on Dec 06, 2004 at 12:42 UTC
    There are at least a few free *real* databases around - one being PostgreSQL. I'm not sure what MySQL's current license situation is, but it might be free as well.

    My advice is to convert your flat text file database into a real database so that you can use "LIKE" in your SELECT, something like this (as someone else already pointed out):

    SELECT * FROM your_table WHERE author_last_name LIKE 'A%'
    Actually, if your flat file database is CSV (Comma Separated Value) or similar, you might be able to use Perl's DBI and DBD::CSV modules to do what you want, and you might be able to use a SELECT similar to the one above that uses the "LIKE", but I'm just speculating ;-)

    HTH.

      To be more detail, database contain near 9000 records; top part contains some 'digital' names, then goes a normal alphabet.
      example:

      666
      666
      Active
      040 Feat Erica Baxter
      16B
      2 Brothers On The 4th Floor
      2 Eivissa
      2 Unlimited
      20 Fingers
      2Pac
      3-O-Matic
      4 Clubbers
      4-2 The Floor
      60 Minute Man
      A Trance Communications Classic
      A. Kay B.J.
      A.D.A.M. Feat. Amy
      A1
      Aaliyah
      AB Logic
      ABBA
      Above and Beyond
      Accela
      ...
        Your original message said your flat file database was a "Books" database where each book was listed with Author's Last Name and First Name - I don't see that in this data that you posted. How is your data organized? What data is present there? Where is the Book title? Where is the book author last name, first name?
Re^3: database sorter script
by jZed (Prior) on Dec 06, 2004 at 20:39 UTC
    If it's a flatfile database, then either convert it to SQLite or Pg or use DBD::CSV or DBD::AnyData directly on the flatfile. With those two DBDS you can use "SELECT $cols FROM $table WHERE $field LIKE 'A%' ORDER BY $field" on a flatfile. If you don't care about the case of the letter use UPPER($field), otherwise it will find only "Ant", not "ant".

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://412634]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2014-12-22 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (109 votes), past polls