LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 02-02-2012, 04:28 AM   #1
frater
Member
 
Registered: Jul 2008
Posts: 106

Rep: Reputation: 23
add piping as parameter to a simple perl script


I need median in one of my bash scripts.
A friend of mine is encouraging to leave bash alone and start with perl.

On the inet I found this little perl implementation which I now want to use within my bash scripts. A little executable file in /usr/local/sbin/median

Code:
#!/usr/bin/perl
@nums = sort{$a <=> $b} @ARGV;
$med = $nums[($#nums / 2)];

print $med . "\n";
The problem now is that I don't want to pass the parameters on the command line, but I want to pipe them like this.

Code:
# echo '3
> 1
> 3
> 4
> 5
> 2' | median

#
should give the same output as:
Code:
# median 3 1 3 4 5 2
3
#
I would like to have the command-line to take precedence.
If no command line is given it should take STDIN as the parameter.

This could be a (very belated) start with perl for me...
I would really appreciate any input

Last edited by frater; 02-02-2012 at 04:50 AM.
 
Old 02-02-2012, 04:54 AM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 640

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Hi.

Code:
#!/usr/bin/perl
@nums = sort{$a <=> $b} <STDIN>;
$med = $nums[($#nums / 2)];

print $med . "\n";
Code:
$ echo  3 1 3 4 5 2 | tr ' ' '\n'| ./median.pl 
3
Take a look a this link.
 
Old 02-02-2012, 05:25 AM   #3
frater
Member
 
Registered: Jul 2008
Posts: 106

Original Poster
Rep: Reputation: 23
Thanks, but do you know how to elegantly support both ways and preferably without needing a "tr ' ' '\n'" ??
The command-line should take precedence
 
Old 02-02-2012, 06:25 AM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Code:
#!/usr/bin/perl

my @nums;

do {
    while(<>) {
        chomp;
        push @nums, split;
    }
} unless $#ARGV > -1;

@nums = @ARGV if $#ARGV > -1;
@nums =  sort {$a <=> $b} @nums;
my $med = $nums[($#nums / 2)];

print $med . "\n";
Code:
echo "1 5 9 82 15" | ./median.pl
9
Code:
echo "
1
5
9
82
15
" | ./median.pl
9
Code:
echo "1 5 9
82 15" | ./median.pl
9
Code:
./median.pl 1 5 9 82 15
9

Last edited by Cedrik; 02-02-2012 at 06:26 AM.
 
Old 02-03-2012, 05:32 AM   #5
frater
Member
 
Registered: Jul 2008
Posts: 106

Original Poster
Rep: Reputation: 23
Thanks, thanks, thanks...

Hoping I don't overask, but how would it look if it needs to work like this...
Code:
cat <file> median
median <file>
median 35 3 37 23 12 30
Looking at it now, only the 2 first would be enough.
That's how other functions like sed and grep work:
Code:
cat <file> median
median <file>

Last edited by frater; 02-03-2012 at 06:13 AM.
 
Old 02-03-2012, 06:26 AM   #6
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 640

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Hi.

Quote:
Looking at it now, only the 2 first would be enough.
That's how other functions like sed and grep work:
That's simpler -- perl has a syntax sugar "<>" for this (read e.g. here)
Code:
#!/usr/bin/perl

my @nums;

while(<>) {
	chomp;
	push @nums, split;
}


@nums =  sort {$a <=> $b} @nums;
my $med = $nums[($#nums / 2)];

print $med . "\n";

Last edited by firstfire; 02-03-2012 at 06:28 AM.
 
  


Reply

Tags
perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
simple perl script not working vinaytp Linux - Newbie 1 06-19-2009 01:13 AM
how to pass in a parameter to a perl script new_2_unix Linux - Newbie 2 12-09-2007 12:48 AM
Testing parameter input to perl script merana Programming 1 03-17-2006 09:54 AM
Very simple question, installing script for Perl lexington Linux - Newbie 1 06-07-2004 04:55 AM
how to add -h parameter in script yulitao Programming 4 03-23-2004 07:30 PM


All times are GMT -5. The time now is 11:44 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration