FAQ
Dear Chuck Lawhorn

I am sorry to ask again a small Question.

foreach $Item( @MainArray, @SubArray ) {
$Hash{$Item}++;
}

Does this block put every element of both arrays in to Values of the HASH?.
or
Does this block put every element of one array as keys of the HASH? and
other Array elemnets as Values.If so which is key ,Which is Value.


I am sorry for asking previous Question again.
$Hash{$Key} is a value of a item in HASH,Right?. Then Why do we care
about its value by comparing 1. how this is linked with task
Please clarify this.

Thank you Very much

With Best Regards
Raju
----------
From: Chuck Lawhorn[SMTP:charliel3@yahoo.com]
Sent: Sunday, September 23, 2001 9:56 AM
To: Veeraraju_Mareddi
Subject: RE: Subtracting two arrays

Raju--

When you step through the two arrays, you increase the value of the array
element corresponding to
the value in the arrays. For example, the word 'two' appears in both
arrays, so when we finish
stepping through the two arrays, the value $Hash{'two'} will equa two.
That's how we know the
value 'two' is in both arrays and must be spliced from the main array. If
the value of an array
element is 1, we know it is either only in the main array or only in the
sub array, therefore we
will leave it alone.

The only time this is a problem is if the value 'two' were to appear twice
in the Main array. If
we are assured that each of the array values in the main array os unique,
this will work fine.
Philip's solution, however, was brilliant and much better than mine.

I hope this is clear. Please ask any other questions if I did not explain
this well enough.

--Chuck

--- Veeraraju_Mareddi wrote:
Dear Chuck Lawhorn

Its very pleasure to get a reply from you.

I could not understand this .if( $Hash{$Key} > 1 ). What is this for?.

Thank you so much

Sincerely
Raju


----------
From: Chuck Lawhorn[SMTP:charliel3@yahoo.com]
Sent: Friday, September 21, 2001 8:11 PM
To: Veeraraju_Mareddi; Perl-Win32-Admin@listserv.ActiveState.com
Subject: Re: Subtracting two arrays

This short script should do what you want.

--------------------------------
@MainArray = ( 'one', 'two', 'three', 'four' );
@SubArray = ( 'two', 'three' );

foreach $Item( @MainArray, @SubArray ) {
$Hash{$Item}++;
}

foreach $Key ( keys %Hash ) {
if( $Hash{$Key} > 1 ) {
foreach $i (reverse( 0 .. $#MainArray )) {
splice( @MainArray, 1, $i ) if( $MainArray[$i] eq $Key);
}
}
}

print join( ',', @MainArray ), "\n";

------------------------------

The contents of @MainArray are now ('one', 'four').

Hope this helps,

--Chuck

--- Veeraraju_Mareddi wrote:
Dear Group,

Is there any PERL Function for removing an array of items from
another
array.

I have a main array of elements and one more array of elements
which is
subset of main array. I want to remove those items which are common
in
two
arrays from Main Array.Please tell me is there any of getting it
done
Thanx and regards
Rajuveera



_______________________________________________
Perl-Win32-Admin mailing list
Perl-Win32-Admin@listserv.ActiveState.com
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-admin

__________________________________________________
Terrorist Attacks on U.S. - How can you help?
Donate cash, emergency relief information
http://dailynews.yahoo.com/fc/US/Emergency_Information/
_______________________________________________
Perl-Win32-Admin mailing list
Perl-Win32-Admin@listserv.ActiveState.com
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-admin

__________________________________________________
Do You Yahoo!?
Get email alerts & NEW webcam video instant messaging with Yahoo!
Messenger. http://im.yahoo.com

Search Discussions

  • Randal L. Schwartz at Sep 23, 2001 at 9:10 am
    "Veeraraju" == Veeraraju mareddi writes:
    Veeraraju> Dear Chuck Lawhorn
    Veeraraju> I am sorry to ask again a small Question.

    Veeraraju> foreach $Item( @MainArray, @SubArray ) {
    Veeraraju> $Hash{$Item}++;
    Veeraraju> }

    Veeraraju> Does this block put every element of both arrays in to
    Veeraraju> Values of the HASH?. or Does this block put every element
    Veeraraju> of one array as keys of the HASH? and other Array elemnets
    Veeraraju> as Values.If so which is key ,Which is Value.

    Neither of those. It puts all the items of @MainArray, and all the
    items of @SubArray, as keys in the hash, and the values are how many
    times each item has been seen.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedSep 23, '01 at 4:56a
activeSep 23, '01 at 9:10a
posts2
users2
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase