JarJarJoe has asked for the wisdom of the Perl Monks concerning the following question:

@array1=qw(jane 23 teacher);
@array2=qw(james 20 tailor);
@array3=qw(jason 19 manager);
@array4=qw(jimmy 23 teacher);
@array5=qw(jenny 23 teacher);
@array6=qw(kim 19 manager);
@array7=qw(larry 19 manager);
@array8=qw(wall 23 teacher);

Hi monks. I have these sets of array ,and i want to:-


1)store all arrays in array of array


2)find the matching item , occupation and age.store the matched name inside a hash


3)loopback all the unmatched set of data to look for another set of match


4)display all the data in the table


please help me monks, i am stuck in a limbo for a long time ,need to complete this project

Replies are listed 'Best First'.
Re: Data processing from array
by davido (Cardinal) on Mar 06, 2014 at 08:25 UTC

    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

Re: Data processing from array
by Anonymous Monk on Mar 06, 2014 at 13:41 UTC
    A better thing to do is to ask your teacher for help with your homework, because this amount and sort of data is unquestionably a homework assignment.