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

Im trying to find the text of a variable occurring in each line in an array.
Example:
Array "@full-list" contains ...
"Bob-smith#anthropology"
"Sandy-smith#sociology"
"Tom-sanders#english"
"Tina-lacy#socialwork"
"Anthony-sanders#english"
The value of the variable I'm searching for changes from smith to sanders to lacy.
@namelist-search= smith, sanders, lacy.

How do I find each occurrence in the array?
Here's what I have. Don't know how to changes the grep.....

For each (@namelist-search)
{
$Hold it=$_;
Chomp($holdit);
@grepnames= grep($holdit, @full-list);
}
Open result, ">matched-in-array.txt"
Print result "@grepnames\n\n";
  • Comment on How can I find the occurrence of a variable in each line of an array

Replies are listed 'Best First'.
Re: How can I find the occurrence of a variable in each line of an array
by LanX (Saint) on Sep 23, 2015 at 23:17 UTC
    Well it's not clear if you want multiple matches to be listed separately, like "lacy smith" twice.

    If not I'd just construct an "or"ed regex with all search terms.

    DB<127> @search=qw/smith sanders/ => ("smith", "sanders") DB<128> $regex= join "|", @search => "smith|sanders" DB<129> @matches = grep { /($regex)/ } @full => ( "Bob-smith#anthropology", "Sandy-smith#sociology", "Tom-sanders#english", "Anthony-sanders#english", ) DB<130> @full => ( "Bob-smith#anthropology", "Sandy-smith#sociology", "Tom-sanders#english", "Tina-lacy#socialwork", "Anthony-sanders#english", )

    update

    improved code

    BTW we appreciate if you post real code not just multiply broken pseudo stuff. :)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re: How can I find the occurrence of a variable in each line of an array
by 1nickt (Canon) on Sep 24, 2015 at 06:53 UTC

    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.
Re: How can I find the occurrence of a variable in each line of an array
by james28909 (Deacon) on Sep 24, 2015 at 01:33 UTC
    You have a typo in the code you posted. "$Hold it" is not a valid variable. need to change it to $hold_it or $holdit, as seen further down in your code.

      There are many "typo"'s... "For each", "Chomp". It's pretty clear this is pseudo code. What should be pointed out is that all of it should be put into <code></code> tags.

        There are many "typo"'s... "For each", "Chomp". It's pretty clear this is pseudo code.

        Probably auto correct from some phone or some such ... seriously who is going to write "Chomp" in pseudocode :D

Re: How can I find the occurrence of a variable in each line of an array
by Anonymous Monk on Sep 23, 2015 at 23:26 UTC

    Try

    my $fullre = join '|', map { "\Q$_\E" } @full_list; ... @grepnames = $holdit =~ m{($fullre)}g;
Re: How can I find the occurrence of a variable in each line of an array
by GotToBTru (Prior) on Sep 24, 2015 at 13:11 UTC

    You asked essentially the same question 4 years ago. I see you incorporated some of what you were told into this.

    Imprecise wording is getting in the way here, perhaps because English is not your first language? The variable is not changing but the value in the variable is.

    Dum Spiro Spero

      it's not the same question. This time, I'm not trying to find an exact match for each line. I'm seeking a match anywhere that it's found. And I do speak English .