CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Distinct in a array

 



S_Shrum
User

Apr 23, 2001, 10:49 AM

Post #1 of 4 (402 views)
Distinct in a array Can't Post

I have a script that searches for user specified text in a multidim array and passes the row # into @match.

If the script finds one occurance of the user specified text in a row, one entry is made into @match; this works fine. The problem is that if the script finds more than 1 occurance of the search specification in a row, multiple entries pointing to the same record are passed to @match (1 for each match found).

What I need to know is the syntax that will only add an # if the # does not already exist in @match. Sorta like <pseudocode>:

If # not in @match, add #

TIA

Sean Shrum
sean@shrum.net
www.shrum.net


Jean
User


Apr 23, 2001, 11:43 PM

Post #2 of 4 (391 views)
Re: Distinct in a array [In reply to] Can't Post

I would advise to use a hash.
In this case yiu can use the # as an identifier.

If I understood your post correctly, the following example may be of help:
@arr is an existing array
%hash is a new hash that is going to include all the
positions in the array that include word 'found'.

Code
my $userinput = 'found'; 
for ($i = 0; $i <= $#arr; $i++) { # Loops through the array and keeps track of the array index in $i
if ( $arr[$i] =~ /$userinput/ ) { # Checks for user input matching
if ( !exists $match{$i} ) { # Ignores previously found indexes (irrelevant in one-dim array)
$match{$item} = $arr[$i]; # Saves the string in the %match hash under $i name
}
}
}

Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com


BigRich
Novice

Apr 25, 2001, 4:51 AM

Post #3 of 4 (382 views)
Re: Distinct in a array [In reply to] Can't Post

It sounds like a loop control problem more than anything else.

if you only want to count one occurance, stop searching after you've found that one occurrance.

#!/usr/bin/perl -w

use strict; # always use strict!

my @mulidimensional = (
[qw(this that foo)],
[qw(one two two)],
[qw(foo bar foo)],
[qw(123 foo 123)],
);

my $input = 'foo';

# wrong

Code
my @multiple_entries; 
my $row_number = 0;
foreach my $record (@mulidimensional) {
foreach my $item (@{$record}) {
if ($item eq lc($input)) {
push @multiple_entries, $row_number; #found it but were not quitting
}
}
$row_number++;
}

##
## @multiple_entries contains ("0", "2", "2", "3") because it found
## 2 occurances of 'foo' in record number 2 (3rd row)
##


# right

Code
my @entries; 
my $count = 0;
foreach my $record (@mulidimensional) {
foreach my $item (@{$record}) {
if ($item eq lc($input)) {
push @entries, $count; last; #found it, go to the next record
}
}
$count++;
}

##
## @entries contains ("0", "2", "3") because it quit searching
## as soon as it found 'foo' in record number 2
##


Hope this helped,

BigRich



S_Shrum
User

May 3, 2001, 4:38 PM

Post #4 of 4 (361 views)
Re: Distinct in a array [In reply to] Can't Post

Maybe I wasn't clear.

I have a multidim array called @contents that contains the data being searched for. I have another singledim array called @match that just holds ROW NUMBERS to @contents.

The searching part works fine. What I am looking to do:

if ROW# not exist in @match, then push ROW#

That's all. I can't seem to get the syntax correct myself. I just need to know how to set up the syntax. It shouldn't be more than a one-liner thing.

Thanks for the help so far.

Sean Shrum
sean@shrum.net
www.shrum.net

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives