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.
|