At 9:06 AM -0500 3/23/01, Scott R. Godin wrote:
>on 03/20/2001 04:35 PM, Christopher Palmer at [EMAIL PROTECTED] wrote:
>>  I think we are supposed to do this one without the aid of DBI...but I
>>  will double check that.  It can be done without DBI, no?
>
>That I couldn't tell you
>
>>  Those resources would be great.  The problem I am having right now is
>>  in even figuring out where to begin on those subroutines.  Like I
>>  said, once I get started it should come easier...but for whatever
>>  reason I am mentally stuck 100%.

1. Of course it can be done without DBI. From a quick look at your 
assignment, it appears that it's meant to entail use of text files as 
your data tables, and the subroutines emulate SQL database-type calls 
(like "select", "update", "insert", etc.), but you're *not* being 
asked to use a real SQL database.

2. Where to begin? First, relax (I know, it's a final exam, but...), 
and push out of your mind the attempt to "see it all at once." Just 
pick some little bit, isolate it from the rest, and get it to work. 
Do any of those assigned subroutines do things you've done before? 
Your instructor seems to be suggesting that you might have useful 
stuff in lecture notes or past assignments.

3. This is a worthy assignment, and it's not clear from your 
questions how much you've worked with what's involved, so please 
forgive if the following is too basic:

One logical place to start is the "parseRow" sub:

### from the assignment:
# parseRow (10 points)
#
# This subroutine takes a parameter a string that looks like:
# ROW 3::john::foo::John Adams
# It returns a list that looks like:
# ("3","john","foo","John Adams")
sub parseRow {
    my ($rowdata) = @_;
    my @row;
# FILL IN
    return @row;
}
###

So, a 'record' in the table looks like
ROW 3::john::foo::John Adams

Now, turning a string into a list can be done in many ways, and the 
best way depends on the structure of the data within the string. If, 
as here, the structure is:
  - one 'record' per 'row' (line)
  - 'fields' (or 'columns') are separated by double colons ('::')
  - the fields are always in the same order across the row (even if empty)

This is a prime case for Perl's 'split' function, which can divide a 
string into a list if it's given some phrase or pattern to use as the 
boundary between the list elements into which you want the string 
divided.

Concept-code:
LIST = split BOUNDARY, STRING, [NUMCOLUMNS];

NUMCOLUMNS is an optional number limiting how many pieces the string 
is split into, starting from the left. Not needed here if the data is 
all like the sample.

Example:
my @row = split '::', $rowdata;

Idiom:
my @row = split '::' => $rowdata;

Possible Answer:
sub parseRow {
    my ($rowdata) = @_;
    my @row = split '::', $rowdata;
    return @row;
}

Terse Answer:
sub parseRow {
    split '::' => $_[0]
}


4. As you build your module, write a short test script that makes use 
of its capabilities. It might be easier to do it all in one file, 
using package namespaces to separate the module parts from the test 
script. Once you have your subs working, then go back to the module 
outline from your assignment and drop in your working subs. Before 
you've done the latter, call your subs using the module's pacakge 
name prepended to the sub's name, viz. the line below
   TextDB::parseRow($test_string)

###
#!perl -w

use strict;

## Test script

my $test_string = "ROW 3::john::foo::John Adams";

my @row_items = TextDB::parseRow($test_string);

foreach (@row_items) {
   print $_, "\n";
}

## Module-to-be:

package TextDB;

sub parseRow {
    my ($rowdata) = @_;
    my @row = split '::', $rowdata;
    return @row;
}

1;

__END__

HTH

1;




-- 

   - Bruce

__bruce_van_allen__santa_cruz_ca__

Reply via email to