Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re^2: use English; and performance

by asz (Pilgrim)
on Mar 02, 2006 at 16:56 UTC ( #533977=note: print w/replies, xml ) Need Help??

in reply to Re: use English; and performance
in thread use English; and performance

i'm trying to create a string tokenizer for a config file parser and the best that i've managed to think of is this:
#!/usr/bin/perl use strict; use Data::Dumper; my $line = q[keyword1 value keyword2 "value with spaces" keyword3 valu +e]; print Dumper tokenize_line($line); sub tokenize_line { my $line = shift; my @tokens; while ($line =~ /(\S+)/g) { # every non-space match is a token push @tokens, $1; # anything in double-quotes is a single token if ($line =~ /\G\s*"(.+?)"/) { push @tokens, $1; # continue from this last match $line = $'; } } return \@tokens; }
wich outputs this:
$VAR1 = [ 'keyword1', 'value', 'keyword2', 'value with spaces', 'keyword3', 'value' ];
i know it's an ugly hack, trying to substitute the original string with the rest of the matched pattern ($line = $';), but in my previous attempts i would use split and substr to achieve the same results... and it was very ugly :)
what would be a better way to write this? i will be parsing some hundred lines from a config file, so i don't think i want a performance penalty. thank you all for your time and advice!


Replies are listed 'Best First'.
Re^3: use English; and performance
by Aristotle (Chancellor) on Mar 03, 2006 at 16:09 UTC

    Use /gc in your speculative match. /c prevents pos() from being reset on match failure.

    Makeshifts last the longest.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2022-01-25 06:15 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (65 votes). Check out past polls.