Solved

Perl help

Posted on 2015-02-10
10
240 Views
Last Modified: 2015-02-16
Hi,

I attended an interview today and they asked me the below questions. Please help on them.

1. How to sort an array without using built in?
2. How to sort an hash with values ?
3. Tell the login for permutation in Perl?
4. Regex for email address, web url , state-city-zipcode format
5. what is the difference between ./test.pl and perl test.pl ?


Thanks
0
Comment
Question by:magento
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 250 total points
ID: 40601189
1 you might use a custom module, depending on why you want to sort an array without using built in
2 sort{$hash{$a}<=>$hash{$b}, or use Sort::Hash::Values;
3 I don't understand what you are asking.
4 use Regexp::Common qw[Email::Address zip];
5 depends on the #! line and mode of test.pl
0
 
LVL 26

Assisted Solution

by:wilcoxon
wilcoxon earned 250 total points
ID: 40601467
1) I suspect this was a pointless interview question.  I'm guessing they want to make sure you understand what sort does.  In perl, there is never a reason not to use sort (though you do sometimes want to give it a custom coderef to do the sorting).
2) ozo answered
3) I don't understand what you are asking either
4) it is very difficult to create a regex to match email addresses - Regexp::Common is by far the best bet
quick-and-dirty URL regex: ($method, $site, $path, $query) = m{(\w+)://([^/]*)/([^&]*)(&.*)?$}
5) ./test.pl will run the command using whatever is specified on #! while perl test.pl ignores the #! line and runs the code

For instance:
test.pl starts with #!/usr/local/bin/perl -w
you also have perl at /usr/bin/perl and 'which perl' gives you that one
./test.pl will effectively run /usr/local/bin/perl -w test.pl
perl test.pl will run /usr/bin/perl test.pl
0
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 40601488
perl test.pl doesn't ignore the #! line
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:wilcoxon
ID: 40601610
Interesting.  I never noticed that before.  It's definitely contrary to what I would expect (and, iirc, normal shell processing mechanics (but it's been a long time since I've done much shell scripting so I may be misremembering)).
0
 
LVL 84

Expert Comment

by:ozo
ID: 40601671
from `perldoc perlrun`
       The "#!" line is always examined for switches as the line is being parsed.
...
       If the "#!" line does not contain the word "perl" nor the word "indir"
       the program named after the "#!" is executed instead of the Perl
       interpreter.  This is slightly bizarre, but it helps people on machines
       that don't do "#!", because they can tell a program that their SHELL is
       /usr/bin/perl, and Perl will then dispatch the program to the correct
       interpreter for them.


But I would not consider it reasonable to expect knowledge of such details on an interview question unless the interviewee is given access to the man pages
0
 
LVL 5

Author Comment

by:magento
ID: 40602020
Dear Experts,

Thank you so much for your time . I am going thru the answers.
The question 3 is , they asked me to show logic in perl to do the combination ...

Thanks
0
 
LVL 26

Assisted Solution

by:wilcoxon
wilcoxon earned 250 total points
ID: 40602286
Ah.  I get it now - the logic of permutation.  That's another general algorithm question.  You should be able to Google for most basic algorithm questions:

1) bubble sort (you don't specify the type of sort) - http://www.perlmonks.org/?node_id=113259
3) permutation - http://www.perlmonks.org/?node_id=24270
The correct way to do permutations would be Algorithm::Permute (but I doubt that's the answer they wanted).
0
 
LVL 84

Expert Comment

by:ozo
ID: 40604891
3) I'm still not sure I understand.  Do you want permutation or combination?  And would you want a singular one (if so, which) or plural all (if so, in what order)?   And which logic?
0
 
LVL 5

Author Comment

by:magento
ID: 40607106
Hi Ozo ,

Sorry i really dont know this . This was asked to me in an internal interview. So i thought of learning the answers from experts.

I think they asked me how you will do permutation logic in perl .

Thanks
0
 
LVL 84

Expert Comment

by:ozo
ID: 40607402
You might do a particular permutation like
@a=@a[1,4,3,2,0];
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Renaming with batch file 9 108
cscript to activate Windows and Office? 2 150
Shell Script- gzip 5 85
Convert Variable to Array 4 24
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question