On Apr 1, 2005 12:45 PM, Brett Williams wrote: > Im aware of regular > expressions now, but find them too confusing at this stage (im an > extreme newbie to any programming).
Read "perldoc perlrequick" (type that into your terminal), that will get you up to speed quickly regarding regular expressions. You can also read it online: http://perldoc.perl.org/perlrequick.html > > I want to print to screen all text between ":" on a new line. I > thought this would be easy :) > The final code i came up with is, > First comment - always start your code with: use strict; use warnings; It will help you tremendously, especially as a beginner. > open (INPUT, "file.txt") or die "Error, can't find file\n"; Good job, however the error message is a little slim. See my code, below. > $line = readline(INPUT); > while($line) There's no real reason for you to use "readline" here - much better (less typing :-)) to use the diamond operator. See my code below. > { > chomp($line); "chomp" removes the "\n" from the input line. Why are you doing this here? Since you are already splitting the input line, that will get rid of the "\n" - chomp is redundant here. > @pricelist = split (/\:/, $line); No need to escape the ":", it is not a metacharacter. > > For some reason this code prints out the entire contents of the text > file (and fails to display every entry on a new line). Im completely > lost, can anyone help? > Here's some code that works. I explain it below: ######### begin code use strict; use warnings; my $infile = "file.txt"; open (INPUT, $infile) or die "$0 error, can't open $infile for reading: $!\n"; while(defined (my $line = <INPUT>)) { my @pricelist = split (/:/, $line); print "$pricelist[1]\n" if @pricelist != 1; } close(INPUT); ######### end code As you can see, I started with "use strict" and "use warnings". The "open" line is essentially the same as yours, I simply beefed-up the error message (read "perldoc perlvar" for more info about $0 and $!). There is no need to assign to $line outside the while loop - you can do so inside the while itself. The code I wrote uses a common idiom, that checks if the value of the assignment is defined. The value will not be defined at the EOF, at which point the while will be exited. Just what we need :-) 'print "$pricelist[1]\n" if @pricelist != 1;' says that if the number of elements in the array @pricelist is not 1, print something. Why is that? Well, looking at the input, I saw that unwanted lines will have no ":" in them at all. The result of "split" from an unwanted line wil have just 1 result (@pricelist==1), since split on /:/ will basically return the entire line - there is nothing to split. So lines for which @pricelist != 1 must be wanted lines. $pricelist[1] is the second element of the array @pricelist. If you need to know more, read "perldoc perlintro", it is a very good introduction to Perl's syntax, variables, etc. The reason the number we want is in the second and not first element has to do with the way split works. You can read about split at "perldoc -f split", just as you can read about any Perl function using "perldoc -f <function_name>". I'll let you figure out exactly what "split" is doing here be yourself :-) Hope this helps, -- Offer Kaye -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>