FAQ

David Gilden wrote:
Thanks Dave for the while <FH> loop,

I need to protect against blank lines and spurious white space that
could show up in the data. No doubt the file will edited in Note pad
(on widows 98), and I want to put the data in to the hash in a clean
format. and since the this is basically a price list names of the
product (the keys for the hash) might have spaces: new model , 20 I
do want them quoted: "new model"

also of note Data::Dumper; is built in to OSX! Joy!

Thanks for any refinement on the code below.

Dave
( kora musician / audiophile / web master @ cora connection / Ft.
Worth, TX, USA)
------


#!/usr/bin/perl -w
use Data::Dumper;
use strict;

my %prices;

my $data = "/Users/dowda/Desktop/tmp test/test.txt";

open (FH,$data) || die "could not open $data $!";

while( <FH> ) {
All you need for blank line is:
next if ( /^\s*$/ );
s/^\s*(.+)\s*$/$1/; # clean the line
next if m/^\W+/; # blank lines

my( $k, $v ) = split /,/;
If there can be white space, then just change the split
my ( $k, $v ) = spilt(/\s*,\s*/, $_);
$k =~ s/^\s*(.+)\s*$/$1/; # clean the hash key
$v =~ s/^\s*(.+)\s*$/$1/; # clean the value

$prices{qq($k)} = $v;
Also for processing as keys, you may want to either lc or uc all the data and then when inputting a key, use the same methodology to keep things in sync and under your control.

Wags ;)





**********************************************************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************************************

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedMar 18, '03 at 7:54p
activeMar 18, '03 at 7:54p
posts1
users1
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase