You shouldn't have put your data into an array like that to begin with. The technique of concatenating discrete fields into a string for storage and pushing them onto an array is not advisable, mostly because it makes the data very hard to work with, as you are discovering.

Your data should be in a hash, like this:

my %people = ( 1 => { first_name => 'Bob', last_name => 'Smith', topic => 'An +thropology' }, 2 => { first_name => 'Sandy', last_name => 'Smith', topic => 'So +ciology' }, 3 => { first_name => 'Tom', last_name => 'Sanders', topic => 'En +glish' }, 4 => { first_name => 'Tina', last_name => 'Lacy', topic => 'So +cial Work' }, 5 => { first_name => 'Anthony', last_name => 'Sanders', topic => 'En +glish' }, );
Then you can get at the data any way you like, access just part of it, sort it by fields, etc etc.

Edit: I see that most of my brethren have interpreted your question another way. I figured by "The variable I'm searching for changes from smith to sanders to lacy" you were indicating which "field" you wanted to extract. What you meant was "The value I'm searching for changes from ...".

Nevertheless the advice to put the data in a hash is still applicable.

#!/usr/bin/perl; use strict; use warnings; use feature qw/ say /; my %people = ( 1 => { first_name => 'Bob', last_name => 'Smith', topic => 'An +thropology' }, 2 => { first_name => 'Sandy', last_name => 'Smith', topic => 'So +ciology' }, 3 => { first_name => 'Tom', last_name => 'Sanders', topic => 'En +glish' }, 4 => { first_name => 'Tina', last_name => 'Lacy', topic => 'So +cial Work' }, 5 => { first_name => 'Anthony', last_name => 'Sanders', topic => 'En +glish' }, ); my @last_names = map { $_->{'last_name'} } values %people; say for sort @last_names; __END__
Output:
$ perl 1142844.pl Lacy Sanders Sanders Smith Smith

Hope this helps! If you need advice on how to build the hash, post a follow-up question.

The way forward always starts with a minimal test.

In reply to Re: How can I find the occurrence of a variable in each line of an array by 1nickt
in thread How can I find the occurrence of a variable in each line of an array by alicatserver

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.