On Fri, Feb 17, 2012 at 11:49 PM, Rob Coops <rco...@gmail.com> wrote: > > > On Fri, Feb 17, 2012 at 4:32 PM, lina <lina.lastn...@gmail.com> wrote: >> >> Hi, >> >> I have a file, >> >> cat try.xpm >> a 1 >> b 2 >> c 3 >> d 4 >> >> abbbcaaaadddb >> >> >> I wish to use perl to translate the last line into the numerical value. >> >> #!/usr/bin/perl >> >> use warnings; >> use strict; >> >> open FILE, "<try.xpm" or die $!; >> >> my @line = <FILE>; >> >> while (<FILE>) { >> print $_; >> } >> >> strangely it print me nothing out, >> >> Thanks for any suggestions, >> >> Best regards, >> >> -- >> To unsubscribe, e-mail: beginners-unsubscr...@perl.org >> For additional commands, e-mail: beginners-h...@perl.org >> http://learn.perl.org/ >> >> > > Hi thats not that strange look at what you are doing... > > #!/usr/bin/perl > > use warnings; > use strict; > # So far so good > open FILE, "<try.xpm" or die $!; # Open the file no problem there though I > would use a variable instead of a handle like that but I'll leave that up to > others to complain about ;-) > > my @line = <FILE>; # Read the entire file to @line > > while (<FILE>) { # Read the FILE handle till the end of the file is reached > (you already got there in the previous step so this will instantly return) > print $_; # Yeah that should work providing that you get into the > loop > } > > Of course you could put the cursor back at the beginning of the file after > you shove it all into @line or you could remove that step. Another option > could be to loop over @line rather then over the file. Now there is one > thing to mention here and that is if you as it is called slurp a file into > memory as you do with the my @line = <FILE>; statement you might depending > on the size of the file get in problem with the memory available on your > system. Now a days most computers have GB's of memory but a lot > of operating systems limit the amount of memory they will normally allocate > to a process to a certain amount given that other programs also like to use > some of that memory so it is usually not a bad idea to have a good think > about if you really need to shove it all into memory or if you could live > with processing the data one line at a time storing only that information > that you really need rather then all the information available to you.
Thanks Rob, What's the $_ and $@ I can guess about its usage, but don't get why use _ or @ here?. BTW, what's the significant advantage perl exceed the other languages (it shares lots of similarity with bash and sed), Just curious, such as for which problems or under which situation you would consider trying perl. sorry, I only did something very light in my life, never involved in some serious project. Thanks with best regards, > > Regards, > > Rob -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/