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:
HELP!! retrieving individual elements from an "array_of_arrays"

 



scooper
Deleted

Sep 18, 2000, 2:53 AM

Post #1 of 4 (769 views)
HELP!! retrieving individual elements from an "array_of_arrays" Can't Post

HELP!!!

Is it possible to retrieve the individual elements from an "array_of_arrays"?
Is there something that I'm missing here???

Thanks!!!

I have:

SOMEWORD_THAT_MATCHES, 23456784584568:
SOME_OTHER_WORD_THAT_MATCHES, 23456784584568:
AND MATCH

(BTW - If anyone has any ideas in the blank space before "SOME_OTHER_WORD" I'd appreciate hearing them :-))

sub search_file{
$matches = 0;
$input_string = "SOMEWORD_THAT_MATCHES SOME_OTHER_WORD_THAT_MATCHES";
$choice="GLORCH";
if ($choice =~ /GLORCH/i) {
@individual_keywords = split(/\s+/, $input_string); #split the input up by whitespace
$number_individual_keywords = $#individual_keywords+1;
@search_string = $individual_keywords;
open (READ_UPDATEFILE, $updatesfile) or die "Error opening updatefile. $updatesfile: $!\n";
while (<READ_UPDATEFILE> ){
chop;
$updatefile_text= $_;
@updatefields = split(/\t/, $updatefile_text);
$record = "$updatefields[1]\t$updatefields[3]\t$updatefields[12]";
@update_keys = split(/\,/,$updatefields[11]); # dynamically split $update_keys into an array
%seen = ();
@keylist_uniq = ();


# search through $record, iterating dynamically through as many keywords as were given
foreach $individual_keywords (@individual_keywords){

if ($record =~ m/$individual_keywords/i){

# put the matches into keylist_uniq array SOMEWORD_THAT_MATCHES, 23456784584568

$pushkeys="$individual_keywords,$updatefields[0]:\n";
push(@keylist_uniq,$pushkeys) unless $seen{$pushkeys};
} else {
# found zilch
}

} continue {

# compare each keyword to individual words in the [11]th element of this $record

foreach $update_key (@update_keys){
if ($update_key =~ m/$individual_keywords/i){

# put the matches into the keylist_uniq array SOME_OTHER_WORD_THAT_MATCHES, 23456784584568

$pushkeys="$individual_keywords,$updatefields[0]:\n";
push(@keylist_uniq,$pushkeys) unless $seen{$pushkeys};
} else {
# got nothin'
}
}
}


#now join and filter the keylist_uniq arrays to get unique results

foreach $pushkeys (@keylist_uniq) {
push(@temp_array,$pushkeys) unless $seen{$pushkeys}++;
}

# retrieve the values in each

@filtered = sort keys %seen;
$foo = \@filtered;

#I'd like to retrieve the values as an string.
# @$foo = "SOMEWORD_THAT_MATCHES, 23456784584568:\nSOME_OTHER_WORD_THAT_MATCHES, 23456784584568:\n"
#split them up by ":"
# $bar = split(/:/, @$foo);
#split those up by ","
# $bim = split(/,/, $bar);
#push the first element $bim[1] to the results array for later retrieval
# push(@results,$bim[1]);


if (eval($#temp_array+1) ge $number_individual_keywords) {
print "@$foo\n"; #
print "AND MATCH\n";

#push(@results,$bim[1]);
#$number_of_results++;


} else {

print "@$foo\n";
print "NO MATCH\n";
}
}
}
close(READ_UPDATEFILE);
}


japhy
Enthusiast

Sep 18, 2000, 5:11 AM

Post #2 of 4 (769 views)
Re: HELP!! retrieving individual elements from an "array_of_arrays" [In reply to] Can't Post

It would have been FAR nicer for you to have given a link to your program as a text file online somewhere.

As for the spaces in front of the matches, that's because you're doing "@array", which is like saying join($", @array), and $" defaults to a single space. So "@array" is a string of the elements with spaces between them.

As for your array-of-arrays business, if $foo is a reference to @bar, then $foo->[$I] is the same as $bar[$I]. If $bar[$I] is a reference to ANOTHER array, then doing $bar[$I][$J] accesses element $J of the referenced array.

Please read one (or more) of the following:
perlreftut
perlref
http://www.pobox.com/~japhy/documentation/using_refs

The first two are standard documentation for Perl 5.6 (the first one can also be gotten from my web site if you don't have Perl 5.6 yet -- the url is http://www.pobox.com/~japhy/perl/5.6/ ), and the second is an email I was asked to write (by perl.com staff) for someone starting in the realm of references in Perl.

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher



scooper
Deleted

Sep 18, 2000, 3:43 PM

Post #3 of 4 (769 views)
Re: HELP!! retrieving individual elements from an "array_of_arrays" [In reply to] Can't Post

OH! I had a feeling that's where I should be looking ... but I really wasn't too sure.

Thanks for pointing me in the right direction!

BTW -- how do I post code so that my indent maintain? sorry 'bout the ugly post, it's my first and I by the time I posted, it was too late to change it!!


Kanji
User

Sep 18, 2000, 4:37 PM

Post #4 of 4 (769 views)
Re: HELP!! retrieving individual elements from an "array_of_arrays" [In reply to] Can't Post

You need to use the [CODE] ... [/CODE] construct. It's documented via the *UBB Code is ON link next to the post entry box.

 
 


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

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