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


In reply to Re: Data processing from array by davido
in thread Data processing from array by JarJarJoe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.