PDA

View Full Version : regex within a fetchrow_array, loop


bazz
06-08-2008, 12:12 AM
Hi,

Having uploaded a csv file to one table, I am moving various parts of it to others as part of the normalisation process.

I need to remove some '_' and to capitalise each word in the array returned.
The following code works until I add the regex's as highlighted. Can the regex not be used here or have I forgotten something else.


while (my @fields = $sth->fetchrow_array) {
#print qq( <p>fields = @fields</p> );
# push (@full_list, @fields);
@fields =~ s/_/ /g;
@fields =~ s/(\w+)/ucfirst($1)/eg;
my $in = $dbhconnect->prepare ("INSERT into tbl_address
(address_id,
customer_id,
name_number,
address_1,
address_2,
address_3,
townland,
main_town,
county,
country)
values ('', ?, ?, ?, ?, ?, ?, ?, ?, ? )
") or die "prepare statement failed: $DBI::errstr\n";


bazz

oesxyl
06-08-2008, 01:50 AM
@fields is a array, you must apply the reagex to each item.
for example you can use map:

my @putput = map { $_ =~ s/_/ /g; $_; } @input;


foreach, while, or any other way.

regards

bazz
06-08-2008, 11:53 AM
Thanks oesxyl,

It's a few years since I used map and I had foregotten about it. Need to read up on it again and look back to wherever I used it.

bazz

oesxyl
06-08-2008, 03:15 PM
Thanks oesxyl,

It's a few years since I used map and I had foregotten about it. Need to read up on it again and look back to wherever I used it.

bazz
you can use foreach or while loop as I said, the idea is to walk thru array some way and apply regex.

regards

bazz
06-08-2008, 04:03 PM
Thanks, yep I got it working.

Now I just have a session problem. :(

I'll post about it separately.

bazz