searching the array

searching the array

am 22.08.2011 08:25:17 von anant mittal

--000e0cd2dbe888eee904ab122426
Content-Type: text/plain; charset=ISO-8859-1

Hi!
I want to search whether a scalar '$a' contains any one of value of an array
'@arr'.
How it may be done?
as its not correct : print "found" if $a =~ /@arr/;

--000e0cd2dbe888eee904ab122426--

Re: searching the array

am 22.08.2011 08:52:56 von Alan Haggai Alavi

Hello Anant,

> I want to search whether a scalar '$a' contains any one of value of an
> array '@arr'.
> How it may be done?

If you are using perl v5.010 or later, you can use the smart match operator
(~~):

use 5.010;

use strict;
use warnings;

my @array = qw( foo bar quux );
my $item = 'bar';

if ( $item ~~ @array ) {
say "Item found: $item";
}

However, if you have an older perl, you can use List::Util's (which is a core
module since perl v5.7.3) `first` subroutine:

use strict;
use warnings;

use List::Util qw( first );

my @array = qw( foo bar quux );
my $item = 'bar';

if ( first { $item eq $_ } @array ) {
print "Item found: $item\n";
}

Regards,
Alan Haggai Alavi.
--
The difference makes the difference.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: searching the array

am 22.08.2011 09:03:38 von Alan Haggai Alavi

Hello Anant,

Please read:

perldoc -q 'certain element is contained in a list or array'

or

http://bit.ly/o9uKat

Regards,
Alan Haggai Alavi.
--
The difference makes the difference.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: searching the array

am 22.08.2011 09:42:05 von timothy adigun

--000e0cd66d60326d6e04ab133788
Content-Type: text/plain; charset=ISO-8859-1

Hi Anant,

>>I want to search whether a scalar '$a' contains any one of value of an
array
>>'@arr'.
>>How it may be done?
>>as its not correct : print "found" if $a =~ /@arr/;

#!/usr/bin/perl
use strict;
use warnings;

my @arr=qw(fry ring apple law);
print "Enter the string you are searching for:";
chomp(my $search=); # you may have to check, if input is not string

grep {print "found: $_" if/\b$search\b/}@arr;

#grep compare your search with each array element
# using $_ and then print out if item is found.
# To make it plain you can ALSO use a foreach loop instead of grep,
# iterate over your array element, comparing them each element
# with your search item.

foreach my $found(@arr){
if($found=~/$search/){
print "found: ",$found,"\n";
}
}

--000e0cd66d60326d6e04ab133788--

Re: searching the array

am 22.08.2011 13:03:31 von AKINLEYE

--000e0cd5cff2939fd504ab16077b
Content-Type: text/plain; charset=ISO-8859-1

Hi anant

On Mon, Aug 22, 2011 at 8:42 AM, timothy adigun <2teezperl@gmail.com> wrote:

> Hi Anant,
>
> >>I want to search whether a scalar '$a' contains any one of value of an
> array
> >>'@arr'.
> >>How it may be done?
> >>as its not correct : print "found" if $a =~ /@arr/;
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my @arr=qw(fry ring apple law);
> print "Enter the string you are searching for:";
> chomp(my $search=); # you may have to check, if input is not string
>
> grep {print "found: $_" if/\b$search\b/}@arr;
>
> #grep compare your search with each array element
> # using $_ and then print out if item is found.
> # To make it plain you can ALSO use a foreach loop instead of grep,
> # iterate over your array element, comparing them each element
> # with your search item.
>
> foreach my $found(@arr){
> if($found=~/$/){
> print "found: ",$found,"\n";
> }
> }
>

Just a modification of the above idea this can be solved is . Still curious
how the above works though

#!/usr/bin/perl
use strict;
use warnings;

my @arr=qw(fry ring apple law);
print "Enter the string you are searching for:";
chomp(my $search=); # you may have to check, if input is not string
my (index) = grep { arr[$_] eq $search }0..$#arr ;
if( defined (index))
{
print " found" ;
}



--
Akinleye Adedamola

--000e0cd5cff2939fd504ab16077b--

Re: searching the array

am 22.08.2011 13:46:33 von Rob Dixon

On 22/08/2011 08:42, timothy adigun wrote:
> Hi Anant,
>
>>> I want to search whether a scalar '$a' contains any one of value of an
> array
>>> '@arr'.
>>> How it may be done?
>>> as its not correct : print "found" if $a =~ /@arr/;
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my @arr=qw(fry ring apple law);
> print "Enter the string you are searching for:";
> chomp(my $search=); # you may have to check, if input is not string
>
> grep {print "found: $_" if/\b$search\b/}@arr;
>
> #grep compare your search with each array element
> # using $_ and then print out if item is found.

This is a miuse of the grep operator, which is filtering @arr according
to the expression in the braces and returning a new list. But you are
throwing away that list and using only the side effects. It should be
written

/\b$search\b/ and print "found: $_\n" for @arr;

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: searching the array

am 22.08.2011 13:53:44 von Rob Dixon

On 22/08/2011 12:03, AKINLEYE wrote:
> Hi anant
>
> On Mon, Aug 22, 2011 at 8:42 AM, timothy adigun<2teezperl@gmail.com> wrote:
>
>> Hi Anant,
>>
>>>> I want to search whether a scalar '$a' contains any one of value of an
>> array
>>>> '@arr'.
>>>> How it may be done?
>>>> as its not correct : print "found" if $a =~ /@arr/;
>>
>> #!/usr/bin/perl
>> use strict;
>> use warnings;
>>
>> my @arr=qw(fry ring apple law);
>> print "Enter the string you are searching for:";
>> chomp(my $search=); # you may have to check, if input is not string
>>
>> grep {print "found: $_" if/\b$search\b/}@arr;
>>
>> #grep compare your search with each array element
>> # using $_ and then print out if item is found.
>> # To make it plain you can ALSO use a foreach loop instead of grep,
>> # iterate over your array element, comparing them each element
>> # with your search item.
>>
>> foreach my $found(@arr){
>> if($found=~/$/){
>> print "found: ",$found,"\n";
>> }
>> }
>>
>
> Just a modification of the above idea this can be solved is . Still curious
> how the above works though

You have misquoted it, so it doesn't work as it stands. The line

if($found=~/$/){

should read

if ($found =~ /$search/) {

Even so, it finds elements elements of @arr that contain $search as a
substring, whereas what is probably wanted is simply

if ($found eq $search) {

> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my @arr=qw(fry ring apple law);
> print "Enter the string you are searching for:";
> chomp(my $search=); # you may have to check, if input is not string

I don't understand your comment. The input from stdin has to be a
string - it cannot be anything else.

> my (index) = grep { arr[$_] eq $search }0..$#arr ;
> if( defined (index))
> {
> print " found" ;
> }

This code will not compile. It should read

my ($index) = grep { $arr[$_] eq $search } 0..$#arr ;
if (defined ($index)) {
print " found at $index" ;
}

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: searching the array

am 22.08.2011 14:10:40 von timothy adigun

--0016e6d63ff1b53b6804ab16f744
Content-Type: text/plain; charset=ISO-8859-1

Hi Rob,

my @arr=qw(fry ring apple law);
print "Enter the string you are searching for:";
chomp(my $search=); # you may have to check, if input is not string
>>I don't understand your comment. The input from stdin has to be a
>>string - it cannot be anything else.

the input from stdin could be anything, but I was only making it clear for
the user to know that since what we are searching for is a string, he can
take the code further, to ensure that a string is inputted in the search
scalar element.

Regards,
Timothy.

--0016e6d63ff1b53b6804ab16f744--

Re: searching the array

am 22.08.2011 14:11:26 von timothy adigun

--0016e6dd8a2e7c65ac04ab16fa79
Content-Type: text/plain; charset=ISO-8859-1

Hi Rob,


my @arr=qw(fry ring apple law);
print "Enter the string you are searching for:";
chomp(my $search=); # you may have to check, if input is not string
>>I don't understand your comment. The input from stdin has to be a
>>string - it cannot be anything else.

the input from stdin could be anything, but I was only making it clear for
the user to know that since what we are searching for is a string, he can
take the code further, to ensure that a string is inputted in the search
scalar element.

Regards,
Timothy.

--0016e6dd8a2e7c65ac04ab16fa79--

Re: searching the array

am 22.08.2011 14:21:31 von Shawn H Corey

On 11-08-22 02:25 AM, anant mittal wrote:
> Hi!
> I want to search whether a scalar '$a' contains any one of value of an array
> '@arr'.
> How it may be done?
> as its not correct : print "found" if $a =~ /@arr/;
>

First of all, don't use $a or $b as a variable name. `sort` uses them
and although `perl` won't get confuse about them, you will.

Try:

#!/usr/bin/env perl

use strict;
use warnings;

use List::Util;

# find first item, undef means nothing found
my $found_item = first { $item =~ m{ \Q$_\E }msx } @list;

__END__

See:
perldoc List::Util
perldoc perlretut
perldoc perlre


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

"Make something worthwhile." -- Dear Hunter

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: searching the array

am 22.08.2011 14:23:21 von Uri Guttman

>>>>> "ta" == timothy adigun <2teezperl@gmail.com> writes:

ta> Hi Rob,
ta> my @arr=qw(fry ring apple law);
ta> print "Enter the string you are searching for:";
ta> chomp(my $search=); # you may have to check, if input is not string
>>> I don't understand your comment. The input from stdin has to be a
>>> string - it cannot be anything else.

ta> the input from stdin could be anything, but I was only making it
ta> clear for the user to know that since what we are searching for is
ta> a string, he can take the code further, to ensure that a string is
ta> inputted in the search scalar element.

it can only be a string as it comes from stdin. what else do you think
it could be?

uri

--
Uri Guttman -- uri AT perlhunter DOT com --- http://www.perlhunter.com --
------------ Perl Developer Recruiting and Placement Services -------------
----- Perl Code Review, Architecture, Development, Training, Support -------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: searching the array

am 22.08.2011 14:26:36 von Shawn H Corey

On 11-08-22 08:23 AM, Uri Guttman wrote:
> it can only be a string as it comes from stdin. what else do you think
> it could be?

1. The empty string (not really a string)

2. eof( *STDIN );


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

"Make something worthwhile." -- Dear Hunter

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: searching the array

am 22.08.2011 15:19:15 von Uri Guttman

>>>>> "SHC" == Shawn H Corey writes:

SHC> On 11-08-22 08:23 AM, Uri Guttman wrote:
>> it can only be a string as it comes from stdin. what else do you think
>> it could be?

SHC> 1. The empty string (not really a string)

it is a string that also called the null string. and it is hard to get
from reading stdin from a keyboard. typing ^D will get you an eof
first. anything else will not get you a null string (at least a newline
will be read if in normal line buffered mode or a single char if not).

SHC> 2. eof( *STDIN );

you get undef in the scalar. the comment was still off as he seems to
imply data that isn't a string. eof isn't data.

uri

--
Uri Guttman -- uri AT perlhunter DOT com --- http://www.perlhunter.com --
------------ Perl Developer Recruiting and Placement Services -------------
----- Perl Code Review, Architecture, Development, Training, Support -------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: searching the array

am 22.08.2011 15:22:58 von Shawn H Corey

On 11-08-22 09:19 AM, Uri Guttman wrote:
> the comment was still off as he seems to
> imply data that isn't a string. eof isn't data.
>

It's meta-data. :)


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

"Make something worthwhile." -- Dear Hunter

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: searching the array

am 22.08.2011 17:12:18 von AKINLEYE

--90e6ba2121a746d67704ab198135
Content-Type: text/plain; charset=ISO-8859-1

Hi Rob ,
Sorry my mistake didn't see that.

Thanxs

if ($found eq $search) {


#!/usr/bin/perl
> use strict;
> use warnings;
>
> my @arr=qw(fry ring apple law);
> print "Enter the string you are searching for:";
> chomp(my $search=); # you may have to check, if input is not string
>

I don't understand your comment. The input from stdin has to be a
string - it cannot be anything else.


my (index) = grep { arr[$_] eq $search }0..$#arr ;
> if( defined (index))
> {
> print " found" ;
> }
>

This code will not compile. It should read


my ($index) = grep { $arr[$_] eq $search } 0..$#arr ;
if (defined ($index)) {
print " found at $index" ;
}

--
Akinleye Adedamola

--90e6ba2121a746d67704ab198135--

perl version 5.12

am 22.08.2011 22:12:16 von dannwong

Hi All,
I'm moving from perl version 5.8 to 5.12. In 5.8 code, I use the
dbmopen function to read a perl db, but that function doesn't seem to
work with version 5.12. any ideas if the function name changed or I need
to use something else? Thanks.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: perl version 5.12

am 22.08.2011 22:30:30 von jwkrahn

Danny Wong (dannwong) wrote:
> Hi All,

Hello,

> I'm moving from perl version 5.8 to 5.12. In 5.8 code, I use the
> dbmopen function to read a perl db, but that function doesn't seem to
> work with version 5.12. any ideas if the function name changed or I need
> to use something else? Thanks.

dbmopen (and its corresponding functions) have been deprecated for a
while now.

perldoc DB_File
perldoc GDBM_File
perldoc NDBM_File
perldoc ODBM_File
perldoc SDBM_File



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/