Front page | perl.beginners |
Postings from December 2002
Re: two questions
Thread Previous
|
Thread Next
From:
wiggins
Date:
December 31, 2002 10:17
Subject:
Re: two questions
Message ID:
200212311817.MAA03082@crows.siteprotect.com
<snip>
> 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:
@names
Then:
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 };
}
}
etc...
http://danconia.org
Thread Previous
|
Thread Next