Reputation: 109
I need some hindsight on how to print to get the expected outcome.How can I make automatically iterate for the letter for just the data I have, like i++
, and how can I reset the value of i
for each HOME
?
Expected Outcome:
RESIDENCE LIST
a) HOME 1 1. Albert Einstein
2. Adriana
3. Anna
LOCATION USA
b) HOME 2 1. Blaine Pascal
2. Caroline Herschel
3. Cecilia Payne-Gaposchkin
LOCATION GERMANY
c) HOME 3 1. Dorothy Hodgkin
2. Edmond Halley
3. Edwin Powell Hubble
LOCATION INDIA
use strict;
use warnings;
my i=1;
my @alphabets=("a".."z");
my @homes=qw(
HOME1
HOME2
HOME3
);
my @residences=qw(
HOME1 Albert Einstein
HOME1 Adriana
HOME1 Anna
HOME2 Blaine Pascal
HOME2 Caroline Herschel
HOME2 Cecilia Payne-Gaposchkin
HOME3 Dorothy Hodgkin
HOME3 Edmond Halley
HOME3 Edwin Powell Hubble
);
my @location=qw(
USA
GERMANY
INDIA
);
print "RESIDENCE LIST \n\n";
foreach my $alphabet(@alphabets)
{
print "$alphabet)";
foreach my $home(@homes)
{
foreach my $location(@location)
{
foreach my $residence (@residences)
{
if ($home=~ /^residence(.*)/)
{
print "\thome\t";
print $i++,")$1\n";
}
}
print "\t $location\n";
}
}
New Script
foreach my $home(@homes)
{
my $i=1;
foreach my $location(@location)
{
foreach my $residence (@residences)
{
if ($home=~ /^residence(.*)/)
{
print $alphabet++."\n";
print "\thome\t";
print $i++,")$1\n";
}
else
{
next:
}
}
print "\t $location\n";
}
}
Outcome that I get
a) HOME 1 1. Albert Einstein
b) HOME 1 2. Adriana
c) HOME 1 3. Anna
d) LOCATION USA
e) HOME 2 1. Blaine Pascal
f) HOME 2 2. Caroline Herschel
g) HOME 2 3. Cecilia Payne-Gaposchkin
h) LOCATION GERMANY
i) HOME 3 1. Dorothy Hodgkin
j) HOME 3 2. Edmond Halley
k) HOME 3 3. Edwin Powell Hubble
l) LOCATION INDIA
Upvotes: 0
Views: 232
Reputation: 3925
Instead of having $alphabet
, initialize it like you do with $i
, but with a
. Perls ++
operator knows how to work on letters:
my $alphabet = 'a';
...
$alphabet++;
Instead of having $i
as factually global variable, declare it one loop level above where you want it to be reset:
...
foreach my $home (@homes) {
my $i = 1;
foreach my $location (@location) {
...
$i++
....
};
}
Upvotes: 1