ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
@markush
how do you see if array is subset of another?
my program takes input from user in form : '2,3,10...'
it then uses csv to extract elements and store it in array. I need to check if user didn't accidentally type in wrong number. hash has all valid values.
Well, if one array has about 10 elements and the other about 3 elements I can see if the second is a subset of the first.
I'm not sure if I want to see the rest of your program....
Perl is an interesting language and worth learning it. What I want to say is: you should really try to understand the concepts. Hashes and regular expressions are the most fundamental concepts of Perl and one should (at least) these try to understand before writing programs.
Here is how I would solve your problem
Code:
# check if a is subset of b
sub subset {
my ( $a, $b ) = @_ ;
my %b ;
foreach ( @$b ) {
$b{$_} = 1 ;
}
foreach ( @$a ) {
exists $b{$_} || return 0 ;
}
return 1 ;
}
here the arrays are passed as references to the function subset, one would call it this way
Code:
my @array1 = qw(2 3 10 7 11 15 16 19 20 21 22 25 26) ;
my @array2 = qw(2 4 7) ;
print "yes\n" if subset(\@array2, \@array1) ;
In the function subset the first foreach loop populates the hash %b and the second loop checks if the elements of the other array are stored as key in the hash. I'd recommend to understand this code. This code will also work when the arrays have thousands of elements.
Markus
Last edited by markush; 11-22-2012 at 09:59 AM.
Reason: typo
thanks, but your program doesn't print invalid entry.
you're kidding, right? why don't you write yourself a print-statement as you need it, for example:
Code:
exit "Invalid input!" if !subset(\@array1, \@array2);
Quote:
I don't understand:
Code:
exists $b{$_} || return 0
it's a shortcut, || means "or". If the statement left from the || is true, the complete statement is true and the next entry of the loop is checked. But if the first statement is wrong which means $_ is not in the hash, then the statement right from || is executed, the function returns 0 and exits. This means that the function returns 0 immediately when it found a number which isn't in the hash.
You can uses this also for the above printstatement:
thanks.
I expected it to print bad number, (if some are ok), not a generic "Invalid input!". but, nevermind, I'll modify it.
yeah, I knew || is 'or'; nice shortcut.
thanks.
I expected it to print bad number, (if some are ok), not a generic "Invalid input!". but, nevermind, I'll modify it.
yeah, I knew || is 'or'; nice shortcut.
well I didn't know about 'short-circuit evaluation'. don't recall seeing it before in other languages. I've used BASIC and a bit of asm on ZX, then some PASCAL, C and some other in college. but all that was many years ago.
again, sorry for being ignorant.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.