Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Printing Last Element of a line using perl memory?

by 7stud (Deacon)
on Feb 15, 2013 at 01:03 UTC ( #1018822=note: print w/replies, xml ) Need Help??

in reply to Printing Last Element of a line using perl memory?

When you are having trouble with regex matching, and you are certain your regex is correct, then it's time to check exactly what's in the string you're matching. One trick is to do this:

say "-->$string<--";

Any trailing whitespace will be apparent:

-->hello world <---

And if there is a newline at the end of the string, you'll see something like this:

-->hello world <--

Or if you want to reveal every character's ascii code, you can do this:

printf "%vd \n", $string; --output:--
Edit: the printf applies ord() to the characters, so the printf is not limited to ascii characters.

Replies are listed 'Best First'.
Re^2: Printing Last Element of a line using perl memory?
by BrowserUk (Pope) on Feb 15, 2013 at 01:27 UTC
    Or if you want to reveal every character in your string's ascii code, you can do this: printf "%vd \n", $string;

    Thanks for that. I had no idea it existed.

    And (IMO) even better is that is led me to just try: printf "%vx\n", $s;. And whaddayaknow. It worked!

    I'll be making much use of that in future.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      You can even get a little fancier if you want:

      my $string = "\x15hello"; printf "%#*vx \n", " ", $string; --output:-- 0x15 0x68 0x65 0x6c 0x6c 0x6f

      And the printf() actually reveals every character's ord(), so it's not confined to ascii characters.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1018822]
[haukex]: not the most efficient, but then again, I don't have that many modules on CPAN :-)
[Corion]: haukex: Yes, but if it's only supposed to run on my machine, I can be far more liberal with how I extract the code etc.
[Corion]: haukex: Yes - I see the benefit of using Dist::Zilla for people with 150+ modules on CPAN, but I don't see it for myself, and I'm always put off from contributing to such modules because they require a lot of toolchain setup that I don't want to ...
[Corion]: ... spend time on if I only want to provide a short patch
[haukex]: Corion: Yes exactly, in the author tests I don't worry about portability as much, I also don't list the author tests' dependencies in Makefile.PL
[haukex]: I figure someone who wants to contribute will know how to install the missing modules ;-) Not the nicest way to go but I don't think many people are using my modules yet

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (11)
As of 2017-02-27 12:37 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (385 votes). Check out past polls.