LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 03-03-2007, 05:56 AM   #1
noir911
Member
 
Registered: Apr 2004
Posts: 682

Rep: Reputation: Disabled
Perl vs. bash code - number of lines


I wrote this code in Perl and then in sh and found that sh code is
smaller than the Perl one. All this does is convert bytes
to kb, mb and gb. Does anyone know how to make the Perl code smaller?

here's the sh version: (6 lines)

Code:
#!/usr/local/bin/bash
value=$1
((kilo=value/1024))
((mega=kilo/1024))
((giga=mega/1024))
echo $value bytes = $kilo kb, $mega mb, and $giga Gb
here's the Perl version: (11 lines)

Code:
#!/usr/bin/perl -w
my $number = shift;
my $byte = ($number);
my $kb = ($byte/1024);
my $mb = ($kb/1024);
my $gb = ($mb/1024);
print "number is $number\n";
print "byte is $byte byte\n";
print "kb is $kb kb\n";
print "mb is $mb\n";
print "gb is $gb\n";
 
Old 03-03-2007, 06:31 AM   #2
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Very funny. This might work:
Code:
#!/usr/bin/perl -w
my $number = shift;
my $byte = ($number);
my $kb = ($byte/1024);
my $mb = ($kb/1024);
my $gb = ($mb/1024);
print "number is $number\n";
print "byte is $byte byte\n";
print "kb is $kb kb\n";
print "mb is $mb\n";
print "gb is $gb\n";
Perl code ist much smaller now, you see?
 
Old 03-03-2007, 07:23 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,

Although I don't believe this is a serious question (the output from both scripts doesn't remotely look alike....).

Anyway, just in case:
Code:
#!/usr/bin/perl -w
my $byte = shift;
my $kb = ($byte/1024);
my $mb = ($kb/1024);
my $gb = ($mb/1024);
print "$byte bytes = $kb kb, $mb mb, and $gb gb\n";
Now the perl code is also 6 lines, the output is the same (although perl is much more accurate).

Hope this helps.
 
Old 03-03-2007, 08:19 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
You should use the IEC standard binary prefixes, instead of mis-using SI prefixes for binary sizes. 1024 of a unit is not a kilounit, it is a kibiunit.

See here for more info.
 
Old 03-03-2007, 08:38 AM   #5
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Code:
#!/usr/bin/env perl
my $number = shift;
my ($byte, $kb, $mb, $gb) = ($number, $number/1024, $number/(1024**2), $number/(1024**3));
print "number is $number\nbyte is $byte byte\nkb is $kb kb\nmb is $mb\ngb is $gb\n";
The number of lines is really pretty meaningless when you start talking about stuff like this. Just exploit a few features of the language you've got the exact same amount of code in a few less lines. Now, if you were comparing 10,000 lines with 1,000 lines, that would be meaningful (maybe).
 
Old 03-03-2007, 11:08 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
3 lines
Code:
my $x = shift;
	@a = map $x/=1024, (1..4);
	print "@a\n";
 
Old 03-03-2007, 11:13 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
nope TWO lines
Code:
	my $x = shift;
	map {print "$x $_\n" and $x/=1024} qw(bytes Kb Mb Gb);
oops

Last edited by bigearsbilly; 03-03-2007 at 11:42 AM.
 
Old 03-03-2007, 01:16 PM   #8
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
One-Upsmanship

Quote:
Originally Posted by bigearsbilly
nope TWO lines
That's just a hop, skip, and a jump from one line:
Code:
perl -e 'map {print "$ARGV[0] $_\n" and $ARGV[0]/=1024} qw(bytes Kb Mb Gb);' 98234987398
You can do it in Haskell too:
Code:
import System.Environment
main = do args <- getArgs
          sequence_ (map putStrLn [show ((read ((args !! 0))::Float) / (1024 ^ power))
                                            ++ " " ++ meaning | (power, meaning) <-
                                                                  zip [0..3] ["bytes","kb","mb","gb"]])
Yippie!
 
Old 03-03-2007, 03:34 PM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
that's cheating! but as you are a lisp fan I'll forgive you.
 
Old 10-07-2011, 11:12 AM   #10
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Rep: Reputation: 57
hello

in SH, what would be the code to convert Bytes to MB, GB, ... ?
 
0 members found this post helpful.
Old 10-07-2011, 11:23 AM   #11
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,250

Rep: Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432
Quote:
Originally Posted by frenchn00b View Post
hello
in SH, what would be the code to convert Bytes to MB, GB, ... ?
It's posted above. And why reopen a four year old thread, when the answer is IN IT already, or easily found via Google?
 
Old 10-07-2011, 01:35 PM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Perhaps he's refering to a posix-compliant version, since ((..)) is a bash-specific feature?

Of course the answer to that is simply to use $((..)) instead.

Code:
kilo=$(( value/1024 ))
For more on converting various bash forms to posix:
http://mywiki.wooledge.org/Bashism
 
1 members found this post helpful.
  


Reply


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
cat: output specific number of lines mikeshn Linux - Software 8 01-24-2017 12:08 PM
counting number of lines inside a directory structure. vl@d Linux - General 4 11-20-2006 01:50 PM
Reading the number of Lines in a File Mistro116@yahoo.com Programming 31 11-24-2005 01:36 AM
count total number of lines in several files xushi Programming 5 11-12-2005 05:42 PM
output number of blank lines tjgadu Linux - Newbie 7 06-09-2005 05:01 PM

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

All times are GMT -5. The time now is 08:37 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration