Hi,

We've a requirement wherein we're provided with a csv file, within the file there is a name field, and some of names are very long and are causing issues while importing. What we want is pull out these kind of rows containing longer names and send it back to the provider.

We want name character to be a variable so that we can flex the number value and get all the desired rows. Like name having more 7 character or 10 character, and each time by change the number we can pull out all the rows.

The entire requirement is complex, but i want help in the first part, rest i will take care of.

Thanks, Prince.

I borrowed sample data (but not the program) for the following from a Parsing CSV tutorial. The tutorial recommends using the Text::CSV module, which you may want to do if the "name" field is not the first field of your records.

#!/usr/bin/perl
#Names2Long.pl
use strict;
use warnings;

$_ = <DATA>; #Read and skip the header record
my $max_length = 20;
while (<DATA>) {
    chomp;
    m/"([^"]*)"/; #Captures contents of first field (if name is not first, change this)
    my $name = $1;
    if (length($name) > $max_length) {
        print "$_ <<=========== Name exceeds $max_length characters.\n";
    }
    else {
        print "$_\n";
    }
}


__DATA__
"Name","Address","Floors","Donated last year","Contact"
"Charlotte French Cakes","1179 Glenhuntly Rd",1,"Y","John"
"Glenhuntly Pharmacy","1181 Glenhuntly Rd",1,"Y","Paul"
"Dick Wicks Magnetic Pain Relief","1183-1185 Glenhuntly Rd",1,"Y","George"
"Gilmour's Shoes","1187 Glenhuntly Rd",1,"Y","Ringo

Running this gives the following output:

"Charlotte French Cakes","1179 Glenhuntly Rd",1,"Y","John" <<=========== Name exceeds 20 characters.
"Glenhuntly Pharmacy","1181 Glenhuntly Rd",1,"Y","Paul"
"Dick Wicks Magnetic Pain Relief","1183-1185 Glenhuntly Rd",1,"Y","George" <<=========== Name exceeds 20 characters.
"Gilmour's Shoes","1187 Glenhuntly Rd",1,"Y","Ringo

Thanks for the reply.

I'm trying to run "Perl Names2Long.pl data.csv" , but it is failing with following errors.

readline() on unopened filehandle DATA at Names2Long.pl line 6.
readline() on unopened filehandle DATA at Names2Long.pl line 8.

Regards, Prince

See next post...

I'm trying to run "Perl Names2Long.pl data.csv"

I didn't realise you wanted to run the script with the data file as a run-time parameter. If you want to run it that way, try the following modified version of the script:

#!/usr/bin/perl
#Names2Long02.pl
use strict;
use warnings;

$_ = <>; #Read and skip the header record
my $max_length = 20;

while (<>) {
    chomp;
    m/"([^"]*)"/; #Captures contents of first set of double quotes (if name is not in first field, change this)
    my $name = $1;
    my $length_of_name = length($name);
    if ($length_of_name > $max_length) {
        print "$_ <<===== Length of Name is $length_of_name characters (exceeds $max_length.)\n";
    }
    else {
        print "$_\n";
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.