Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: Unpack Many Fields

by ikegami (Patriarch)
on Feb 16, 2010 at 01:53 UTC ( [id://823380]=note: print w/replies, xml ) Need Help??


in reply to Re: Unpack Many Fields
in thread Unpack Many Fields

Using # in qw causes a warning. The following removes warnings and allows spaces to be used in the comment:
my $fmt = q[ x[10] a5 # code x[22] a10 # thingummy x[4] a3 a2 # doodah & whatsit ]; $fmt =~ s/#.*//g;

But pack allows for comments in patterns, so one doesn't even need that last line.

Update: Originally, the /g was missing, which led me to discover that one doesn't need to remove the comments.

Replies are listed 'Best First'.
Re^3: Unpack Many Fields
by shoness (Friar) on Feb 16, 2010 at 02:23 UTC
    I think it has to be something like
    $fmt =~ s/(?:#.*)|\n|\s//g;
    Thanks to both of you!
    My code is already looking much better.
    Cheers!

      Not at all. For starters,

      s/(?:#.*)|\n|\s//g

      is the same as

      s/#.*|\s//g

      Secondly, spaces are allowed in the pattern, so your change is unwarranted.

      Finally, comments are allowed in the pattern, so you don't need the substitution at all.

        Spaces AND NEWLINES are accepted in the pattern! Cool.
        That's why you're the boss! ++

        Cheers!

Re^3: Unpack Many Fields
by BrowserUk (Patriarch) on Feb 16, 2010 at 03:01 UTC

    Whaddayaknow! That definitely deserves a mench in perlfunc.


    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.

      It does. The first sentence of the following is present since at least 5.6.0:

      A comment in a TEMPLATE starts with # and goes to the end of line. White space may be used to separate pack codes from each other, but modifiers and a repeat count must follow immediately.

      It would be nice if WS and # were mentioned in the table at the top, though.

        So it does. I scrolled up and down both the pack and unpack entires several times before I posted; and again just now and didn't pick it out. I had to use a search to find it--which is far easier once you know what search term to use.

        Seconded on the addition to the table at the top.


        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://823380]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-03-29 05:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found