Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting a Element in an Array 1

Status
Not open for further replies.

V00D00

Technical User
Joined
Jul 11, 2005
Messages
78
Location
US
I have an array loaded with this sample data:

FNAME, LNAME, ADDR1, PHONE
John Q, Smith, 1313 Mockingbird Ln, 5555551212

I want to split the first name field to separate the first name and middle initial. I need to iterate through the entire array.

Any suggestions?
 
Code up to this point:

Code:
open(IN, "$sourcefile.csv") or die "Failed to open $sourcefile";

open(CSV, ">$camp$mon$mday$year.csv") or die "Failed to write to $sourcefile.temp.csv";
open(TXT, ">$camp$mon$mday$year.dnc.txt") or die "Failed to write to $sourcefile.temp.csv";

while (<IN>) {
      if ($rawcsv->parse($_)) {
           @cutcsv = $rawcsv->fields;
           $cutcsv[16] =~ s/\$//g;
           $cutcsv[16] =~ s/\,//g;
           $cutcsv[14] =~ s/\///g;
           my $comma = join ",", @cutcsv;
           my @tab = ($cutcsv[10],$cutcsv[3],$cutcsv[2],$cutcsv[6],$cutcsv[7],$cutcsv[8],$cutcsv[9],$cutcsv[-1],$cutcsv[-1],$cutcsv[5],$cutcsv[20],$cutcsv[-1],$cutcsv[-1],$cutcsv[15],$cutcsv[0]);
           my $tabout = join "\t", @tab;
           print TXT "$tabout\n";
           print CSV "$comma\n";
           }
    else {
    my $err = $rawcsv->error_input;
    print "parse() failed on argument: ", $err, "\n";
    }
  }
close (IN) or die "Can't close file: $!";
 
Questions.
What array do you want broken up? @cutcsv or @tab?
Why do you use $cutcsv[-1]? Are you trying to force an undef?
What does the "parse" method do and what kind of object is $rawcsv?
Splitting the field is easy. You could "split /\s+/" or use a regex like this: "/(\S+)\s+(\S+)/".



Trojan.
 
I will be breaking up the first element in array @tab.

I am using $cutcsv[-1] to force blank elements. The data is being reformated and needs the blank elements to fit the desired output format.

The parse method is part of Text::CSV, it takes my raw "","" delimited file and reads it in to the array @cutcsv. This was neccessary because of the need to reformat the data that had , in it.

When splitting the element in an existing array, how do I get it back in to the original array?
 
Perhaps if I post the whole code and not just a snippit will help:

Code:
#!/usr/bin/perl
use strict;
use diagnostics;
use Text::CSV;

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$isdst)= localtime;
$year += 1900;
$mon += 1;

my $rawcsv = Text::CSV->new();              # create a new object
my @cutcsv = "";

print "\nEnter Lead List File Name: ";
chomp (my $sourcefile = <STDIN>);

print "\nWhat campaign are these for? ";
chomp (my $camp = <STDIN>);

open(IN, "$sourcefile.csv") or die "Failed to open $sourcefile";

open(CSV, ">$camp$mon$mday$year.csv") or die "Failed to write to $sourcefile.temp.csv";
open(TXT, ">$camp$mon$mday$year.dnc.txt") or die "Failed to write to $sourcefile.temp.csv";

while (<IN>) {
      if ($rawcsv->parse($_)) {
           @cutcsv = $rawcsv->fields;
           $cutcsv[16] =~ s/\$//g;
           $cutcsv[16] =~ s/\,//g;
           $cutcsv[14] =~ s/\///g;
           my $comma = join ",", @cutcsv;
           my @tab = ($cutcsv[10],$cutcsv[3],$cutcsv[2],$cutcsv[6],$cutcsv[7],$cutcsv[8],$cutcsv[9],$cutcsv[-1],$cutcsv[-1],$cutcsv[5],$cutcsv[20],$cutcsv[-1],$cutcsv[-1],$cutcsv[15],$cutcsv[0]);
           my $tabout = join "\t", @tab;
           print TXT "$tabout\n";
           print CSV "$comma\n";
           }
    else {
    my $err = $rawcsv->error_input;
    print "parse() failed on argument: ", $err, "\n";
    }
  }
close (IN) or die "Can't close file: $!";
 
You have only 4 fields in your sample input and yet you are processing over 20 fields in your code.
Can you explain?
If you tell us which field positions you need the firstname and middle initial in then we can do that.
For example:
Code:
($tab[0],$tab[1]) = split /\s+/, $cutcsv[0], 2;
You should not really use -1 indexes to arrays to get undefs, you might get a surprise.
Your @tab allocation could be:
Code:
@tab = (@cutcsv[10,3,2,6,7,8,9],undef,undef,@cutcsv[5,20],undef,undef,@cutcsv[15,0]);



Trojan.
 
I only listed a couple of fields in the example to try and simplify the question. Plus it allows for me to take a simple example and modify it, so I am not just asking for someone to code for me.

currently the the fname field sits in the 2 element. I would it by space and put the first part of the split back in to element 2 and the second half of the split in element 15.

 
Assign @tab as I showed you and then:
Code:
$tab[2] =~ s/(\s+\S+)// and $tab[15] = $1;
Try it and see if it gives you what you want.


Trojan.
 
Lets complicate things.

In an example where element 0 has fname mname lname, what would be the code to separate eache of those in to there own elements in the array.
 
Assuming that they were space separated:
Code:
my @name_parts = split /\s+/,$array[0],3/;
That leaves fname in $name_parts[0], mname in $name_parts[1] and lname in $name_parts[2].
You can then assign them to what you want.


Trojan.
 
Here is the code I have:

Code:
#!/usr/bin/perl -w
use strict;
use diagnostics;


my ($sec,$min,$hour,$mday,$mon,$year,$wday,$isdst)= localtime;
$year += 1900;
$mon += 1;


print "\nEnter Lead List File Name: ";
chomp (my $sourcefile = <STDIN>);

open(IN, "$sourcefile.txt") or die "Failed to open $sourcefile";
open(OUT, ">$mon$mday$year.dncpdate.txt") or die "Failed to write to $sourcefile.temp.csv";

while (<IN>) {
        my @dncprep = split /\|/, $_;
        my @name_parts = split (/\s+/,$dncprep[0],3);
        my $phonenumber = $dncprep[5].$dncprep[6];
        my @dncflop = ($phonenumber,$name_parts[2],$name_parts[0],$dncprep[1],$dncprep[2],$dncprep[3],$dncprep[4]);
        my $outfile = join "\t", @dncflop;
        print OUT "$outfile\n" ;
        }
close (IN) or die "Can't close source: $!";
close (OUT) or die "Can't close file: $!";

The code works, but on execution it displays:

Use of uninitialized value in join or string at dncprep.pl line 22, <IN> line 1859 (#1)

It displays this for every line that is in my source file.

Any suggestions?
 
One of the fields must be blank in your input.
Check your input for two pipes together ("||").
You could temporarily disable the warning around the "join" if you like.


Trojan.
 
As it turns out, it is not an empty field, but the fname mname lname split that is causing the error. It seems to get quite upset if there is no middle name.

Any suggestions as to how to handle that?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top