http://qs321.pair.com?node_id=118019


in reply to Reading in data that is

I'm rather confused by your logic. I'm also confused by your problem domain.

You have a file. It will have "EOS" at the last line. (Or, is it between each line? I'll assume the former.) You want to read its data.

Now, what you're doing is creating a string, delimited in a certain fashion, then splitting on that delimitor immediately into a list. Why not just create a list?

Also, your logic in your parser is, well, sketchy.

my @data; while (<DATA>) { chomp; next if /EOS$/; s/"\s?"/\t/g; s/"//g; push @data, [ split /\t/, $_ ]; } foreach (@data) { my %hash; @hash{qw(First Last Addr City State Phone)} = @$_; print "$hash{First}, $hash{Last}\n"; print "$hash{Addr}\n"; print "$hash{City},$hash{State}\n"; print "$hash{Phone}\n"; print "\n"; }
Basically, read a line. Skip it if we're reading an EOS. (We, essentially, don't care about them as all our data is on one line.) Then, convert the "" or " " into tabs, then remove the extra ", then split on tab and push a listref with our data onto our master data list.

Then, when we read, we put the data for that line into a hashslice, then access the hash. :-)

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Replies are listed 'Best First'.
Re: Re: Reading in data that is
by mikeB (Friar) on Oct 10, 2001 at 20:09 UTC
    I'm curious why you used

    s/"//g;

    rather than

    tr/"//d;

    I don't advocate speed optimization at the expense of clarity, but in this case both seem clear to me, and I'd expect tr/// to not use the regex engine and thus be faster.

      *laughs* You know, I've never once used tr in a script that didn't involve cut'n'paste from someone else. TMTOWTDI at its best (worst?)! Yes, tr is faster in this specific instance. No, I'll probably never use it because I very rarely (if ever!) do straight character substitutions. If I'm stripping out characters, I'm doing that as part of a series of substitutions, usually involving s/\s//g and not some specific character(s).

      If you can read either, use the faster one. But, please note that speed of execution is not Perl's strong suit. It's good at that, but speed of development is why most people use Perl. In that vein, s/"//g is just as good.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.