Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Relational textfile?

by peppiv (Curate)
on Jan 09, 2002 at 01:43 UTC ( [id://137266]=perlquestion: print w/replies, xml ) Need Help??

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

I have a pipe delimited text file. Looks like this:

Jimmy | yes | | | yes | yes | yes
Robert | yes | yes | | | |
JohnPaul | yes | yes | yes | yes | |
Bonzo | | | yes | yes | yes | |

How can I make a list from multiple field choices?
example - List 1 = those who have "yes" in field #2 but nothing in field #3
List 2 = those who have "yes" in field #2 and field #7
List 3 = "yes" in field #2, nothing in field #3, but yes in field #6

Oh, where to start? Please! I need help!

peppiv

Replies are listed 'Best First'.
Re (tilly) 1: Relational textfile?
by tilly (Archbishop) on Jan 09, 2002 at 01:50 UTC
    Are you familiar with SQL? If so then I would suggest trying to use DBI together with either DBD::CSV or DBD::RAM as database drivers.

    A side benefit is that when you are done it will be relatively little work to move your script to running against a real relational database.

Re: Relational textfile?
by dash2 (Hermit) on Jan 09, 2002 at 01:56 UTC
    Assuming that you never have pipes inside your data: (untested)

    use strict; my @list_defs = ( {2 => 'yes', 3 => ''}, {2 => 'yes', 7 => 'yes'}, {2 => 'yes', 3 => '', 6 => 'yes'}, ); my @lists; while (<FILE>) { my @vals = split /\|/; my $name = $vals[0]; my $i = 0; LIST: foreach my $l (@list_defs) { foreach (keys %$l) { next LIST unless $vals[$_] eq $l->{$_}; } push $lists[$i], $name; # or \@vals; $i++; } }

    I'm sure there are quicker ways, and more readable ones.

    dave hj~

Re: Relational textfile?
by Akhasha (Scribe) on Jan 09, 2002 at 02:30 UTC
    I think the first comment is a really good bet. However I would have iterated over the file calling a selection function passed in as a reference. Think of the selection function as your query, if it returns true this name should go into the list you are building. An intermediate function to build a more friendly data structure for the selectors to operate on would be handy too.
Re: Relational textfile?
by n3dst4 (Scribe) on Jan 09, 2002 at 15:42 UTC
    I don't wan't to discourage you from using DBI, but if you wanted an alternative, and there isn't too much data, you could try reading in the data as a sort of pseudo-bitfield (untested and I've only just woken up...):
    while (<IN>) { ($name, @fields) = split '|'; for (0..5) { $value += 2**$_ if $fields[$_] } $people{$name} = $value; } # People with fields 0 and 3 but nothing else for (keys %people) { print if $people{$_} ^ 58; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://137266]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-23 18:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found