LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-21-2012, 01:29 PM   #16
qrange
Senior Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian stable/testing, amd64
Posts: 1,061

Original Poster
Rep: Reputation: 47

@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.
 
Old 11-21-2012, 01:56 PM   #17
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
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
 
2 members found this post helpful.
Old 11-22-2012, 03:45 AM   #18
qrange
Senior Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian stable/testing, amd64
Posts: 1,061

Original Poster
Rep: Reputation: 47
thanks, but your program doesn't print invalid entry.
I don't understand:
Code:
exists $b{$_} || return 0

Last edited by qrange; 11-22-2012 at 04:11 AM.
 
Old 11-22-2012, 05:55 AM   #19
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Quote:
Originally Posted by qrange View Post
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:
Code:
subset(\@array1, \@array2) || exit "invalid input!";
one doesn't need paranthesis in Perl:
Code:
subset \@array1, \@array2 or exit "invalid input!";
Which can be understood even if one doesn't know Perl.

Markus
 
1 members found this post helpful.
Old 11-22-2012, 09:48 AM   #20
qrange
Senior Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian stable/testing, amd64
Posts: 1,061

Original Poster
Rep: Reputation: 47
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.
 
Old 11-22-2012, 11:12 AM   #21
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by qrange View Post
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.

"yeah, I knew || is 'or'" - no, you didn't. Because if you did, why did you ask ? Do you at all know what Boolean algebra ( https://en.wikipedia.org/wiki/Boolean_algebra ) is ? Do you know what short-circuit evaluation ( http://en.wikipedia.org/wiki/Short-circuit_evaluation ) is ? Perl documentation uses the approach in a whole lot of places.

Again, did you bother to visit http://perldoc.perl.org/ and start from the left column, from http://perldoc.perl.org/index-overview.html and then to go down to http://perldoc.perl.org/index-tutorials.html and http://perldoc.perl.org/index-faq.html ?

And then http://perldoc.perl.org/index-language.html , http://perldoc.perl.org/index-functions.html , http://perldoc.perl.org/perlop.html , http://perldoc.perl.org/perlvar.html , http://perldoc.perl.org/index-pragmas.html , http://perldoc.perl.org/index-utilities.html .
 
Old 11-22-2012, 03:27 PM   #22
qrange
Senior Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian stable/testing, amd64
Posts: 1,061

Original Poster
Rep: Reputation: 47
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl email validation tld check mx check swanny99 Programming 1 06-27-2012 08:42 PM
Perl .check if data are exist in the array before adding new data ufmale Programming 12 07-14-2010 05:26 AM
How to check if an array is empty in Perl (newbie question) resetreset Programming 4 11-28-2008 03:26 AM
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 01:47 AM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration