develooper Front page | perl.beginners | Postings from December 2002

Re: two questions

Thread Previous | Thread Next
December 31, 2002 10:17
Re: two questions
Message ID:


> I don't like who look for a complete and correct script here, but I 
> cannot solve my problem: this is my input-type:
> Dante	 Alighieri 
> Cecco	 Angiolieri 
> Brunetto	 Latini 
> Eugenio	 Montale 
> Giacomo	 Leopardi 
> Niccolò	 Tommaseo 
> Guido	 Gozzano 
> (and so on...)
> all I want in my output are two arrays: the first column of the input 
> (all the first names) in the first array and the second column (all the 
> surnames) in the second array.

Assuming an array of lines with the names in it:



my (@firstnames,@secondnames,@badnames);
foreach my $name (@names) {
  if ($name =~ /(.*)\s+(.*)/) {
     push @firstnames, $1;
     push @secondnames, $2;
  else {
     # Invalid formatted name
     push @badnames;

Depending on what you are doing with the two arrays it might be easier/better to use an array of hashrefs, where you can store the names by key, something like:

foreach my $name (@names) {
  if ($name =~ /(.*)\s+(.*)/) {
     push @fullnames = { 'firstname'=>$1,'lastname'=>$2 };


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About