Active Topics

 


Reply
Thread Tools
Ken-Young's Avatar
Posts: 387 | Thanked: 1,700 times | Joined on Feb 2010 @ Cambridge, MA, USA
#11
Originally Posted by Power View Post
I second buurmas, python is far easier to work with with all the API's. I program a lot on PHP as well on the web, but I am trying my hands on Python for the N900, just wrote a small script to block calls
It's trivial do get python. Just become root and type "apt-get install python".
 
Posts: 78 | Thanked: 32 times | Joined on May 2008
#12
I had a similar experience while learning Python.

I had the textbook opened in one window and fooled around with the terminal at the same time

Gotta luv the '900...
 
Posts: 838 | Thanked: 292 times | Joined on Apr 2010
#13
thanks guys, since I have the book "learning perl" on my kindle, I will try to make it through that. hopefully most of the concepts will apply to python as well. It seems like python is the future (or even the now)...
 
Posts: 89 | Thanked: 48 times | Joined on Dec 2009 @ Glasgow
#14
Originally Posted by extendedping View Post
despite its shortcomings I bought the n900 really only to learn shell scripting and perl.
Great! I hope you don't mind if I offer a couple of helpful comments on your code...

Code:
#!/bin/bash
convert=

case $1 in
group) convert=$(echo 1) ;;
album) convert=$(echo 2) ;;
label) convert=$(echo 3) ;;
year) convert=$(echo 4) ;;
*) echo "please input group, album, label, or year." ; exit 1 ;;
esac 

cut -f$(echo $convert) -d\| albums
You can tidy this slightly:

Code:
#!/bin/bash
case $1 in
	group) convert=1 ;;
	album) convert=2 ;;
	label) convert=3 ;;
	year) convert=4 ;;
	*) echo "please input group, album, label, or year."
	   exit 1 ;;
esac 

cut -f$convert -d\| albums
So the substitution of convert drops straight in place on your 'cut' command.

Another really useful tool to learn is AWK. You could replace your 'cut' command with a simple 'awk' program:
Code:
awk 'BEGIN {FS="|"} {print $'$convert'}' albums
To explain, AWK operates on each line in a file. AWK treats each line as a series of fields ($1 is the first, $2 is second, $0 is the entire line). Here, I've set the field separator (FS) to the pipe character ("|") before any lines of text have been read, and I've told AWK to print out the correct field on each line. It may be a bit more verbose than cut in this example, but AWK is a powerful tool which I use all the time.

Good luck and happy coding!
 
Posts: 838 | Thanked: 292 times | Joined on Apr 2010
#15
Originally Posted by sdstrowes View Post
Great! I hope you don't mind if I offer a couple of helpful comments on your code...

Code:
#!/bin/bash
convert=

case $1 in
group) convert=$(echo 1) ;;
album) convert=$(echo 2) ;;
label) convert=$(echo 3) ;;
year) convert=$(echo 4) ;;
*) echo "please input group, album, label, or year." ; exit 1 ;;
esac 

cut -f$(echo $convert) -d\| albums
You can tidy this slightly:

Code:
#!/bin/bash
case $1 in
	group) convert=1 ;;
	album) convert=2 ;;
	label) convert=3 ;;
	year) convert=4 ;;
	*) echo "please input group, album, label, or year."
	   exit 1 ;;
esac 

cut -f$convert -d\| albums
So the substitution of convert drops straight in place on your 'cut' command.

Another really useful tool to learn is AWK. You could replace your 'cut' command with a simple 'awk' program:
Code:
awk 'BEGIN {FS="|"} {print $'$convert'}' albums
To explain, AWK operates on each line in a file. AWK treats each line as a series of fields ($1 is the first, $2 is second, $0 is the entire line). Here, I've set the field separator (FS) to the pipe character ("|") before any lines of text have been read, and I've told AWK to print out the correct field on each line. It may be a bit more verbose than cut in this example, but AWK is a powerful tool which I use all the time.

Good luck and happy coding!
wow you actually read my code?! thanks I now see cut the echo's in the case statement. I thought any time you needed to get the value from a variable you had to use echo? no I guess not. I will investigate your awk command further, I know a tiny about it but know it is its own programming language and am scared to even look at the chapter on it in my book
 
Posts: 89 | Thanked: 48 times | Joined on Dec 2009 @ Glasgow
#16
Originally Posted by extendedping View Post
I thought any time you needed to get the value from a variable you had to use echo? no I guess not.
No, you don't have to (although, clearly it works). I use echo primarily to view the contents of a variable, or just to print status messages in my scripts. Another more advanced way to use it is to pipe one variable into another. Try this:

Code:
foo="hello cruel world"
echo $foo | awk '{print $1,$3}'
I will investigate your awk command further, I know a tiny about it but know it is its own programming language and am scared to even look at the chapter on it in my book
Take your time with it
 
Posts: 755 | Thanked: 406 times | Joined on Feb 2008 @ UK
#17
Originally Posted by altorn View Post
im not fond of writing shell scripts (i use my pc with an SSH for that, ever so rarely)..

it would be great if we had a mini GCC or C++ compiler for the n900 so we can create even little console programs.
See the threads "Developing on the n900 itself". This can be done using either Easy Debian (installing gcc, make etc on top of this), a chroot of the SDK, or by enabling the SDK repo and installing (very carefully) some tools straight into the device memory.
 
Posts: 838 | Thanked: 292 times | Joined on Apr 2010
#18
thats great but I have to admit I did instead, it seems more appropriate...
foo="hello cruel world"
echo $foo | awk '{print $2,$3}'
 
Posts: 466 | Thanked: 418 times | Joined on Jan 2010
#19
I kind of had the opposite, I bought the N900, then thought "I want to really learn how to program for this thing." I'd always kind of been interested in learning programming (was going to start with Perl, and have a ton of Perl books, but I've seen more and more things (graphical things especially) that are written with Python, so decided to learn that instead.

Nothing like being able to run IDLE right there on your phone

slaapliedje
 
Reply


 
Forum Jump


All times are GMT. The time now is 06:24.