perl sorting variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl sorting variables
# 1  
Old 04-16-2010
perl sorting variables

Good morning!!

Im trying to practice withe Perl and sorting variables is not going good at all!

Code:
#!/usr/bin/perl

$username = $ENV {'LOGNAME'};
print "Hello, $username\n";

I want to add sort and 'mail' and 'home'. This below is what I have came up with,but of course its not working.

Code:
#!/usr/bin/perl

$username = $ENV (sort('LOGNAME'('HOME'('MAIL'))));
print "Hello, $username\n";

Any help is greatly appreciated!!

Ben

Last edited by pludi; 04-16-2010 at 02:19 AM.. Reason: closed tags
# 2  
Old 04-16-2010
What do you mean by "I want to add sort and 'mail' and 'home'"? What should be the result of your operation? The three environment variables, concatenated in sorted order? Or something different?
# 3  
Old 04-16-2010
Pludi,

Yes....The three environment variables, concatenated in sorted order?

I want the output to be:

Hello, (username)with MAIL, LOGNAME, HOME printed out on the screen.

Make sense?

Raven

**It doesnt have to be those 3 variables I chose, it can be any in the environment.
# 4  
Old 04-16-2010
Sorted by the variable name:
Code:
$environ = join " ", map { $ENV{$_} } sort qw/LESS MAIL HOME/;

Sorted by the variable contents:
Code:
$environ = join " ", map { $ENV{$_} } sort { $ENV{$a} cmp $ENV{$b} } qw/LESS MAIL HOME/;

# 5  
Old 04-16-2010
Pludi,

Im going to read some more about the $_, and " ". After I read more, if I have questions about your response.....Ill reply to this thread.

Thanks so much for your help!
Ben
# 6  
Old 04-16-2010
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

2. Shell Programming and Scripting

Perl Sorting Help

Hey guys, I have started to learn perl recently because of a position I took. They want me to master perl and I've been reading books and practicing myself. Basically I,m having my perl script run through a text pad and give the output in a special way e.g input deviceconfig { ... (5 Replies)
Discussion started by: zee3b
5 Replies

3. Shell Programming and Scripting

Sorting dates in Perl

I have a directory of backup files. named like this: ldap.data.04-06-2012.tar ldap.data.03-06-2012.tar ldap.data.02-06-2012.tar ldap.data.01-06-2012.tar ldap.data.31-05-2012.tar ldap.data.30-05-2012.tar ldap.data.29-05-2012.tar ldap.data.28-05-2012.tar ldap.data.27-05-2012.tar... (6 Replies)
Discussion started by: robsonde
6 Replies

4. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

5. Shell Programming and Scripting

Sorting a particular column in PERL

I have a file abc.txt which contains data in th following format abc,23 hgfh,87 tweg,89 jdfjn,74 I want to sort on the basis of column (the second one). It should be numerical sort. output should be abc,23 jdfjn,74 hgfh,87 tweg,89 I know how to do it in unix. I need a PERL code (1 Reply)
Discussion started by: centurion_13
1 Replies

6. Shell Programming and Scripting

Perl sorting

Hi, I have a file in this format: a b c d e a b c d e a b c d e i need perl script to sort 2nd column in alphabatical order The script i use is #!/usr/bin/perl my @words=<>; foreach(sort mysort @words) { print; (4 Replies)
Discussion started by: Learnerabc
4 Replies

7. Shell Programming and Scripting

Perl: sorting by string

I have an array full of string values that need to be sorted, but if a value starts with (regex) 0^ it should be at the beginning of the array. Otherwise the array should be sorted normally using ascii sort. Please help me create the sub to pass to the sort function. (7 Replies)
Discussion started by: dangral
7 Replies

8. Shell Programming and Scripting

PERL data - sorting

Hello, I have a page where multiple fields and their values are displayed. But I am able to sort only a few fields. When I looked into the issue, it is seen that the for each row of info , an unique id is generated and id.txt is generated and saved. Only those fields which are inside that id.txt... (3 Replies)
Discussion started by: eagercyber
3 Replies

9. Shell Programming and Scripting

shell script help: sorting, incrementing environment variables and stuff

First: me == noob. Whats a good resource for shell script info cause I'm having trouble finding good info. I'm writing a shell script to automate the setup of a flash 'page flip'. My current code is below. the page flip takes an xml file of format <content> <pages... (1 Reply)
Discussion started by: secoif
1 Replies

10. Shell Programming and Scripting

perl sorting

I have many files that I need to sort each week. I know how to do in Unix, but for this task it appears best to do native inside an existing perl program. So, simplified, I have a file similar to the following: Joe_________12_Main_St__A001________LX Benny_______5_Spring____A002________LX... (5 Replies)
Discussion started by: joeyg
5 Replies
Login or Register to Ask a Question