LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2005, 05:08 PM   #1
Ateo
Member
 
Registered: Sep 2004
Location: Long Beach, CA
Distribution: FreeBSD,Ubuntu,Gentoo,MacOS
Posts: 139

Rep: Reputation: 15
The perl equivalent...


In php, when I set an array with multiple values, I can compare a string to that array for a result. Example:
Code:
<?php

$value =  array("value1", "value2", "value3", "value4", "value5");
$string = "value3";

if ( in_array($string, $value) )
{
     echo "$string exists in array";
}
else
{
     echo "$string does not exist in array";
}

?>
For the life of me, I cannot figure out the perl equivalent. I have the O'Reilly Programming Perl book in my hands but.... still can't find the equivalent. Currently, I am using for loops as I was not able to get a foreach loop running correctly, but that's a different issue.

Anyone able to assist?

Thanks

Last edited by Ateo; 04-08-2005 at 05:12 PM.
 
Old 04-08-2005, 05:29 PM   #2
auximini
Member
 
Registered: Dec 2003
Location: Calgary, AB
Distribution: Any!
Posts: 146

Rep: Reputation: 18
You could do something like

Code:
if (grep {/value/}, @array) {
    print "OK";
}
or

Code:
foreach $element (@array) {
    if ($element eq $value) {
        print "OK";
    }
}
hope that helps
 
Old 04-08-2005, 06:26 PM   #3
Ateo
Member
 
Registered: Sep 2004
Location: Long Beach, CA
Distribution: FreeBSD,Ubuntu,Gentoo,MacOS
Posts: 139

Original Poster
Rep: Reputation: 15
I ended up using:
Code:
if ( grep $var1, @array )
{
     print "yay\n";
}
Thanks !
 
Old 04-08-2005, 06:33 PM   #4
auximini
Member
 
Registered: Dec 2003
Location: Calgary, AB
Distribution: Any!
Posts: 146

Rep: Reputation: 18
Be careful with the grep, though. I would add an ^ to the beginning and $ to the end to anchor the variable down. Otherwise you might end up with this:

Code:
grep 'hi', 'this'
when you are only looking for hi

so you would need to do

Code:
grep '^hi$', 'this'
and it will return false.

(My syntax is probably wrong.. it's been a while since I used grep in perl..)
 
Old 04-09-2005, 02:23 AM   #5
Ateo
Member
 
Registered: Sep 2004
Location: Long Beach, CA
Distribution: FreeBSD,Ubuntu,Gentoo,MacOS
Posts: 139

Original Poster
Rep: Reputation: 15
I was wondering why I kept getting mixed results. Sometimes it would work, sometimes it wouldn't. Here's my final code:
Code:
foreach $folder ($imap->folders)
{
	if ( !grep /^$folder$/, @excludes)
	{
		$new = $new + $imap->unseen_count($folder);
	}
}
It's a script for my superkaramba theme for displaying total unread messages in all my folder EXCEPT those in the @excludes array.... Adding the ^ and $ seemed to do the trick.

Thanks again!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Require Linux/Perl equivalent command for windows Command alix123 Programming 7 08-19-2005 02:23 AM
Problem with perl module for w3c validator to work on my local Apache+PHP+perl instal tbamt Linux - Software 0 12-16-2004 05:37 PM
Hiding code in PERL, perl gui question randomx Programming 1 06-26-2004 03:22 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
chrooting apache v2 (php, ssl, perl support) ; perl configuration markus1982 Linux - Security 3 01-26-2003 06:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration