Perl: Search for a string in an array

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

Perl: Search for a string in an array

Postposted on Fri Jul 31, 2009 9:23 pm

So, I'm working on writing a forum in Perl. (This is an assignment. I'm well aware of the multitude of better language and already feature complete forums out there.)

For one portion of the forum, I want to automatically adjust a subject line if it matches another one. (Kind of like how this forum can have multiple threads with the same title.) I can't quite figure out what I'm doing wrong.

Here's what I have right now:
Code: Select all
#!/usr/bin/perl
print "Content-type: text/html\n\n";

$string = "This is a test";
@subjects = ("This is a test","This is a test (2)");
$postCount = 1;

# This is a test
print "$string<br />";
while (grep /$string/, @subjects) {
    $postCount++;
    if ($string !~ /\(\d+\)$/) {
   $string = $string . " ($postCount)";
#Skipped
   print "Skipped!<br />";
    }

    $string =~ s/\d+\)$/$postCount\)/;
#This is a test (2)
    print "$string<br />";
}
#This is a test (3)
print $string;
exit;


The output I get from this script is:
Code: Select all
This is a test
Skipped!
This is a test (2)
This is a test (2)


This is the only thing holding me back. The rest of the forum script is nearly finished. I've tried all sorts of things including different loops and conditions. This is the closest I've gotten. I know it's something apparent that I'm not seeing.
The best things in life are free.
http://www.gentoo.org
Guy 1: Surely, you will fold with me.
Guy 2: Alright, but don't call me Shirley.
titan
Grand Gerbil Poohbah
 
Posts: 3276
Joined: Mon Feb 18, 2002 7:00 pm
Location: Great Smoky Mountains

Re: Perl: Search for a string in an array

Postposted on Sat Aug 01, 2009 12:09 am

The below seems to work, you could still use a while loop and an index to go through the array instead.

In your test code I think the problem is if you use while(grep /$string/, @subjects) the results of grep /$string/, @subjects after the first time through the loops is null because $ string is updated to the new value $string =~ s/\d+\)$/$postCount\)/;. At least that is what I saw from putting the grep within the loop and just storing the output. Hopefully this helps.

Code: Select all
#!/usr/bin/perl
print "Content-type: text/html\n\n";

$string = "This is a test";
@subjects = ("This is a test","This is a test (2)");
$postCount = 1;
@subjects2 = grep /$string/, @subjects;
# This is a test
print "$string\n";

foreach $subjects2 (@subjects2)
{
    $postCount++;
    if ($string !~ /\(\d+\)$/)
    {
   $string = $string . " ($postCount)";
#Skipped
   print "Skipped!\n";
    }
    $string =~ s/\d+\)$/$postCount\)/;
#This is a test (2)
    print "$string\n";
}
#This is a test (3)
print $string;
exit;


D:\>D:\test.pl
Content-type: text/html

This is a test
Skipped!
This is a test (2)
This is a test (3)
This is a test (3)
tfp
Grand Gerbil Poohbah
 
Posts: 3068
Joined: Wed Sep 24, 2003 11:09 am

Re: Perl: Search for a string in an array

Postposted on Sat Aug 01, 2009 9:03 am

I think your foreach statement is just running the code for each entry.

Now, I don't ecpect to have thousands of threads on my forum, but that doesn't make sense.
The best things in life are free.
http://www.gentoo.org
Guy 1: Surely, you will fold with me.
Guy 2: Alright, but don't call me Shirley.
titan
Grand Gerbil Poohbah
 
Posts: 3276
Joined: Mon Feb 18, 2002 7:00 pm
Location: Great Smoky Mountains

Re: Perl: Search for a string in an array

Postposted on Sat Aug 01, 2009 10:23 am

yeah it will run for each entry from the grep result, however there shouldn't be to many matching strings for the forum.

I guess I'm not exactly sure of what you want to do.
tfp
Grand Gerbil Poohbah
 
Posts: 3068
Joined: Wed Sep 24, 2003 11:09 am

Re: Perl: Search for a string in an array

Postposted on Sat Aug 01, 2009 4:07 pm

tfp wrote:yeah it will run for each entry from the grep result, however there shouldn't be to many matching strings for the forum.

I guess I'm not exactly sure of what you want to do.

I just re-read the script, I think it'll do what I'm looking to do. Let me give it a shot.

Why wouldn't having grep in the condition of a while loop work anyway? Wouldn't it evaluate the grep again?
The best things in life are free.
http://www.gentoo.org
Guy 1: Surely, you will fold with me.
Guy 2: Alright, but don't call me Shirley.
titan
Grand Gerbil Poohbah
 
Posts: 3276
Joined: Mon Feb 18, 2002 7:00 pm
Location: Great Smoky Mountains

Re: Perl: Search for a string in an array

Postposted on Sat Aug 01, 2009 4:16 pm

Well, the script is close. Very close.

When I adjust the array to hold these values:
Code: Select all
@subjects = ("This is a test","This is a test...not","This is a test (2)");

I get a final result of:
Code: Select all
This is a test (4)


Obviously, "This is a test...not" and "This is a test" are not the same. I think I can adjust it a bit with a RegEx. Does grep use RegEx?
The best things in life are free.
http://www.gentoo.org
Guy 1: Surely, you will fold with me.
Guy 2: Alright, but don't call me Shirley.
titan
Grand Gerbil Poohbah
 
Posts: 3276
Joined: Mon Feb 18, 2002 7:00 pm
Location: Great Smoky Mountains

Re: Perl: Search for a string in an array

Postposted on Sat Aug 01, 2009 4:31 pm

Trala!

Code: Select all
@subjects2 = grep /^$string( \(\d+\)|)$/, @subjects;

Now it works precisely as it should.

In fact, I found a better way to find the number of elements in an array after the grep.
Here's my adjusted script with no loops necessary:
Code: Select all
#!/usr/bin/perl
print "Content-type: text/html\n\n";

$string = "This is a test";
@subjects = ("This is a test","This is a test...not","This is a test (2)");
@subjects2 = grep /^$string( \(\d+\)|)$/, @subjects;
$postCount = scalar @subjects2;
$postCount++;
if ($postCount > 1) {
    if ($string !~ /\(\d+\)$/) {
   $string = $string . " ($postCount)";
    }
    else {
   $string =~ s/\d+\)$/$postCount\)/;
    }
}
print $string;

exit;


This script now returns "This is a test (3)".

Thanks for the nudge in the right direction, tfp. :D
The best things in life are free.
http://www.gentoo.org
Guy 1: Surely, you will fold with me.
Guy 2: Alright, but don't call me Shirley.
titan
Grand Gerbil Poohbah
 
Posts: 3276
Joined: Mon Feb 18, 2002 7:00 pm
Location: Great Smoky Mountains

Re: Perl: Search for a string in an array

Postposted on Sat Aug 01, 2009 9:42 pm

titan wrote:
tfp wrote:yeah it will run for each entry from the grep result, however there shouldn't be to many matching strings for the forum.

I guess I'm not exactly sure of what you want to do.

I just re-read the script, I think it'll do what I'm looking to do. Let me give it a shot.

Why wouldn't having grep in the condition of a while loop work anyway? Wouldn't it evaluate the grep again?

I guess it's probably because grep retuns an array and the while loop goes through the grep array rather than researching the original array.
The best things in life are free.
http://www.gentoo.org
Guy 1: Surely, you will fold with me.
Guy 2: Alright, but don't call me Shirley.
titan
Grand Gerbil Poohbah
 
Posts: 3276
Joined: Mon Feb 18, 2002 7:00 pm
Location: Great Smoky Mountains

Re: Perl: Search for a string in an array

Postposted on Sun Aug 02, 2009 9:11 am

Happy to hear you were able to get it to do what you need it to do. I don't use regular expressions very often so I was kind of flying with one eye closed. Finding away to pull the loop should be a big help in your forum over time.
tfp
Grand Gerbil Poohbah
 
Posts: 3068
Joined: Wed Sep 24, 2003 11:09 am


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 8 guests