in reply to Getting info with a small flatfile db.

The simple command for the second step is split. Of course, depending on how complex the data is (whether the pipe character can be escaped and included in a field), you might have better luck with Text::CSV, Text::xSV, or even DBD::CSV. They should be ordered there by relative complexity.

Counting lines in a file is pretty easy, too. You can slurp them all into an array (if the file is small) and evaluate the array in scalar context. Otherwise, you can loop through the entire file, line by line, and use the special variable $.. The modules named above may have their own mechanisms for doing this.

Update: Stuff the results into an array, that way you won't have to worry about it. Otherwise, you can count the number of pipes in a line with the simple: my $numpipes = $line =~ tr/\|//;

Replies are listed 'Best First'.
Re: Re: Getting info with a small flatfile db.
by bladx (Chaplain) on Aug 03, 2001 at 09:42 UTC
    Thank you, but I already am using the split command, and what I really need is for it to be able to figure out how many variables it needs to put data into per line. any help?
    Andy

      There are a bunch of different ways: count the number of pipes in the line, and add one. You can use tr/|// for this (returns the number of |s).

      Second way store the results from split in an array:

      my @parts = split /\|/, $line;

      Perl will grab all the bits there are to grab. As chromatic mentioned, you can evaluate that array in scalar context to find out how many parts there are. You can also use a list assignment to nab, say, only the first five:

      my ($first, $second, $third, $fourth, $fifth, undef) = split /\|/, $st +ring,6;

      HTH

      perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'