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: Beginner:
string manipulations help

 



saravana
User

Jun 29, 2011, 1:24 AM

Post #1 of 11 (1460 views)
string manipulations help Can't Post

hi there,
i have a two arrays which has strings.
@arr=" the the the the the the the";
@arr1=" the cat is on the mat";

Want i want to do is, i want to compare the string in 1st array are present in 2nd array. if so i want to print the count of them..

in this case. "the" is the repeated string in first array which is their in second array also.. so it should print the matching count as 7..

any suggestions.


rovf
Veteran

Jun 29, 2011, 2:16 AM

Post #2 of 11 (1459 views)
Re: [saravana] string manipulations help [In reply to] Can't Post


Quote
i want to compare the string in 1st array are present in 2nd array


Your example is not clear. Both arrays in your example have only 1 element (which is a long string), and later you talk about substrings ("the" is a substring of both strings you mention).
You seem to mix up "array" and "string" in your explanation.

I suggest that you first formulate the task in a clear way.


saravana
User

Jun 29, 2011, 2:50 AM

Post #3 of 11 (1456 views)
Re: [rovf] string manipulations help [In reply to] Can't Post

thanks for the reply,
let me try to be clear,

I want to compare two sentences.

Sentence1=" the the the the the the"
sentence2=" the cat is on the mat"

when s1 is compared with s2 all the words in s1 matches. so the count is 7.

i did this by splitting sentence into words and then compared using "eq". but it printed result as 12(i.e 12 times "the" instead of 7 times..

plz leave ur suggestion.


rovf
Veteran

Jun 29, 2011, 3:20 AM

Post #4 of 11 (1453 views)
Re: [saravana] string manipulations help [In reply to] Can't Post


Quote
it printed result as 12(i.e 12 times "the" instead of 7 times..
plz leave ur suggestion.


The first idea which comes to my mind would be that there is a bug in your program, so my suggestion would be to fix this bug.

(Note:

Although I'm well known for my paranormal abilities and, under normal circumstances, I can immediately point out a bug in any program without actually seeing the program, in your case my inner eye can't see your code clear enough, so I can't tell you where the mistake in your program is.)

Oh, wait ... I think you made a typo in line 11 of your program. Please fix it!

;-)


saravana
User

Jun 29, 2011, 3:25 AM

Post #5 of 11 (1452 views)
Re: [rovf] string manipulations help [In reply to] Can't Post

sorry for not adding the code:


<code>
my @arr=('the','the','the','the','the','the');
my @arr1=('the','cat','is','on','the','mat');

foreach $a(@arr)
{
foreach $b(@arr1)
{
if($a eq $b){
push(@c,"$b\12")
}
}
}print "@c\n";
<code>

output:
the
the
the
the
the
the
the
the
the
the
the
the


rovf
Veteran

Jun 29, 2011, 5:01 AM

Post #6 of 11 (1443 views)
Re: [saravana] string manipulations help [In reply to] Can't Post

I can't see anywhere in your code, that you are counting something. If $a equals $b, you add some string to the array @c.

You need a mapping of all those strings in @arr1, which also occur in @arr2, and this mapping should map the string to the number of occurances.

Mappings are usually implemented by hashes in Perl, so I would set up a hash, mapping strings to numbers.


saravana
User

Jun 29, 2011, 7:50 AM

Post #7 of 11 (1435 views)
Re: [rovf] string manipulations help [In reply to] Can't Post

thanks for that.

And i do have a query.

Instead of performing single word matching as i did previously. I want to perform matching over multiple words

i.e i want to take first three words from array1 and try to find match in array2. then leaving first word and taking next three words from array1 and comparing with array2 to find match.. like that i want to do until all end of the word in array1.


BillKSmith
Veteran

Jun 29, 2011, 7:51 AM

Post #8 of 11 (1435 views)
Re: [saravana] string manipulations help [In reply to] Can't Post

Use grep to select the desired words from @arr for printing. Use a hash for the test.

Initialize the keys of the hash with the words from @arr1. The following code puts the details of the test into a pair of subroutines. The first initializes the hash. The other test for the presence of the word as a hash key.


Code
  

use strict;
use warnings;
my $STRING = 'the ' x 7;
my $STRING1 = 'The cat is on the mat';
init_test($STRING1);
$, = "\n";
print grep {is_present_in_1($_)} split( /\s/, $STRING );

{
my %hash1;

sub init_test {
my ($string1) = @_;
%hash1 = map {$_,1} split /\s/, $string1;
}

sub is_present_in_1{
my ($word) = @_;
return exists $hash1{$word};
}
}

Good Luck,
Bill


rovf
Veteran

Jun 29, 2011, 8:09 AM

Post #9 of 11 (1431 views)
Re: [saravana] string manipulations help [In reply to] Can't Post

At least in your post #5, you did NOT much individual words, but whole strings (which just happened to be individual words). I think you mean that you want to combine several strings to a single entitiy for your comparision, right?

Your task definition is still a little bit foggy, but if you want to combine several array elements, you have several possibilities, for example

- array slices (however this could be a bit difficult for a beginner),

- combining the array elements in question to a single string and using this string for comparision (see the 'join' function). The character which you use for catenating the array elements must be one which you know does not exist inside the strings (I sometimes use chr(0) for this)

- Leave the array as it is, but build an auxiliary data structure containing the index into the array (from where to start the comparision) and the number of array elements to compare.


saravana
User

Jun 29, 2011, 9:33 AM

Post #10 of 11 (1429 views)
Re: [BillKSmith] string manipulations help [In reply to] Can't Post

Hi bill,
Thanks for the reply

And one more doubt. in the place of comparing the single element "the". i want to compare three elements.. is it possible..

let assume the values of $String and $String1 as follows.

I.e if the $String ='the ' ,'cat','is','on','the','mat'

and $String1 is same as $String.

wat i want to do is, i need to take the 1st three elements of string
i.e
(the cat is)cmp($string1) then,
(cat is on)cmp($string1)
(is on the)cmp($string1)
(on the mat)cmp($string1)

in this order i want to compare instead of looking for single string comparison. if these combination present in the string1 i will print them.

Since in this eg both sentence are same i can find all the four matches..

Can you tell me how to do accomplish this with perl.


BillKSmith
Veteran

Jun 29, 2011, 10:29 AM

Post #11 of 11 (1427 views)
Re: [saravana] string manipulations help [In reply to] Can't Post

The short answer is to use regular expressions rather than split to parse the the input strings. Everything else is the same. As rovf pointed out, you still do not seem to understand the difference between arrays and strings (Refer: perldoc perldata). In "C", a string is a special case of an array. In perl, they are unrelated concepts. You are unlikely to understand a longer answer until you resolve this issue.
Good Luck,
Bill

 
 


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

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