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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Beginner with question about declarations 1

Status
Not open for further replies.

Saiyanate

Programmer
Aug 21, 2007
2
US
I want to create arrays to hold data for a series of people, say @person1, @person2, etc. but the total number of people varies each time I run the program. How do I create arrays (or strings, for that matter) for person1 through personN? I've tried variations on concatenating as seen below, all of which failed.

At its simplest, my question looks like this:
Code:
my $i='2';
my $str.$i;  # this does not create a string named $str2
Thanks.

Code:
#/usr/bin/perl
use strict;
use warnings;
#create arrays for 5 people
for (my $i=1; $i<5; ++$i) {
	my @array.$i;
	}
#use 'print' to see if it worked
print "@array1\n";
print "@array2\n";
print "@array3\n";
print "@array4\n";
print "@array5\n\n";
#rest of prog would go here
exit;
 
You might look at using a 'Hash', where the keys would be 'person1', 'person2', etc and their values would the data.

I hope that helps.

Mike
 
You could always use a while loop, but you would still need to provide some initial information of how many people you are going to use...

i.e.

my (@arraya, @arrayb);
my $numberofpeople = 4;
my $counter = 1;
while ($counter <= 4) {
@arraya = @arrayb[$counter];
$counter = $counter+1;
}

Im not sure if this line of code is correct:
@arraya = @arrayb[$counter];

But you can see what i'm getting at
 
Ahh, I also meant...

while ($counter <= $numberofpeople) {
 
I would use hash of hashs.

You could do somethink like
$info{person1}{firstname} = "George";
$info{person1}{lastname} = "Bush";
$info{person2}{firstname} = "Bill";
$info{person2}{lastname} = "Clinton";

and so one

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Let's just say I used a simple hash.

Code:
#/usr/bin/perl
use strict;
use warnings;
srand (time ^ $$ ^ unpack "%L*", `ps axww | gzip`); # from perldoc.perl.org
my %person_data; #declare hash
my $number_people=10; # or =ARGV[0], STDIN, whatever
my @traits = ('ugly','stupid','smelly','asymmetric','unkempt','sloppy','nasty');
for (my $a=1; $a<=$number_people; ++$a) { # HERES THE IMPORTANT BIT
	$person_data{"person$a"} = $traits[rand @traits];
	}
#say all I care about are persons 3, 5, and 9
print "Of interest...\n";
print "Person 3:  $person_data{'person3'}\n";
print "Person 5:  $person_data{'person5'}\n";
print "Person 9:  $person_data{'person9'}\n";
#or print out them all using something I found at [URL unfurl="true"]www.cs.mcgill.ca/~abatko[/URL]
print "\nHere is everyone...\n";
while( my ($k, $v) = each %person_data ) {
	print "$k is $v.\n";
	}
exit;

To solve my actual programming problem, I may end up using a hash of hashes, or if possible, a hash in which the values are arrays.

Thanks everyone!
 
You can do a hash of arrays also.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
this really just complicates things (in bold):

$person_data{"person$a"} = $traits[rand @traits];

adding the word "person" is serving no useful purpose, you can use a simple numbering system:

Code:
#/usr/bin/perl
use strict;
use warnings;

my %person_data;
my $number_people=10;
my @traits = ('ugly','stupid','smelly','asymmetric','unkempt','sloppy','nasty');
for my $i (1 .. $number_people) {
   $person_data{$i} = $traits[rand @traits];
}

print "Of interest...\n";
print "Person 3:  $person_data{3}\n";
print "Person 5:  $person_data{5}\n";
print "Person 9:  $person_data{9}\n";

print "\nHere is everyone...\n";
while( my ($k, $v) = each %person_data ) {
    print "$k is $v.\n";
    }
exit;



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin's right, but if you're using a numbering system, there's no reason to use a hash at all. Arrays are the ideal data structure for this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top