On Sat, Feb 7, 2009 at 07:10, John W. Krahn <jwkr...@shaw.ca> wrote: > Rob Dixon wrote: >> >> Greg wrote: >>> >>> I have an input file that I have to evaluate whether to include lines >>> based upon their last character. >>> Is there a simple regex that would allow me to >>> assign the very last character to a variable? > > ^^^^^^^^^^^^^^^^^^^^^^^ > ^^^^^^^^^^^^^^^^^^^^^^^ > >>> In my case it will either be A or I (capital i) if that helps? >>> >>> Thanks very much from a perl newb. >> >> my $line = "1234567890A\n"; >> my ($lastchar) = $line =~ /(.)$/; > > That assigns the second to last character. snip
Sort of, it only prints the second to last if there is a newline at the end of the line, and this is the commonly the desired behaviour. Many people do not count the newline as a character when talking about "the last character". Earlier in the spec he said "I have to evaluate whether to include lines". Lines means that they have a line ending character (or characters). If we were to read this spec as you have we could easily conclude that there is no need to do any processing. The last character will always be "\n" (or the line ending character(s)), so no lines need be looked at to determine if they should be included. This is obviously nonsense, so we can conclude that by "very last character" he meant "the last character on the line before the newline". If, however, he had mentioned that he was working with fixed length records, well, then you might have a point (but only if the data file didn't use newlines at the end of the records to make life easier for humans). -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/