in reply to Data processing from array
Hey, welcome back. Do you remember the formatting advice you got here? Re: Submit button increment value. Please read Writeup Formatting Tips to learn how to format your posts.
Anyway, your example code is a set of loosely related arrays. They actually are only related in that they store similar types of data, and have some similarity in their identifiers. I'm not going to go down the "variable as a variable name" road, since that would just be doing you a disservice.
You probably want to organize the data like this:
my @array = ( [ qw( jane 23 teacher ) ], [ qw( james 20 tailor ) ], [ qw( jason 19 manager ) ], [ qw( jimmy 23 teacher ) ], [ qw( jenny 23 teacher ) ], [ qw( kim 19 manager ) ], [ qw( larry 19 manager ) ], [ qw( wall 23 teacher ) ], );
You could accomplish that as you read the data in from a file (for example), like this:
my @array; while( <DATA> ) { chomp; push @array, [ split '' ]; } __DATA__ jane 23 teacher james 20 tailor ...
For your second, third, and fourth questions, I'm not exactly sure. Your description of what you want to do is unclear to me. Maybe it's just too late in the day. Perhaps something like this:
use constant NAME => 0; use constant AGE => 1; use constant JOB => 2; my %hash; foreach my $aref ( @array ) { if( $aref->[NAME] eq 'kim' && $aref->[AGE] == 19 && $aref->[JOB] eq 'manager' ){ $hash{$aref->[NAME]} = $aref; } }
That builds a crossreference where each hash element is indexed by name, and refers back to an array element. Then you could:
foreach my $name ( sort keys %hash ) { local $, = q{, }; say $name, $hash{$name}[AGE], $hash{$name}[JOB]; }
By the way, being stuck in limbo for a long time is fine, but certainly during that long time you had time to try a few things. Posting what you've tried, and describing how it fails to meet your needs goes a long way toward getting better answers. But more importantly, it helps you to think through your problem so that you have a better chance of understanding the answers.
For this particular topic you probably should have a look at perlref and perlreftut. Those documents will help you to understand working with references, which I'm guessing is where you've been uncomfortably stuck.
Dave
|
|---|