andrewr has asked for the wisdom of the Perl Monks concerning the following question:

I'm having a bit of trouble as I cannot access perldoc.perl.org etc (basically anything that resolves to x4.develooper.com) therefore cannot get to any of the docs or someof the online books... anyways....

I have a variable that contains a single line of text eg:

Margin dB : 20.0 19.8

I wish to get the two numeric data values out as invidual scalar variables ( $value1 and $value2 for example).

What would be the easiest way to achive this?

Many thanks in advance.

Andrew

Replies are listed 'Best First'.
Re: data extraction from a variable
by ikegami (Patriarch) on Jan 29, 2010 at 03:08 UTC
    The docs are available via the perldoc command line tool.
    my ($value1, $value2) = /:\s*(\S+)\s+(\S+)/;
Re: data extraction from a variable
by roboticus (Chancellor) on Jan 29, 2010 at 03:13 UTC

    andrewr:

    I'd just do something like:

    my ($variable, $first, $second); $variable = some_data_provider(); if ($variable =~ /: ([0-9.]+) ([0-9.]+)/) { ($first, $second) = ($1, $2); print "Found the values $first and $second!\n"; }

    ...roboticus

    Amusing note: I accidentally left off the / in the </code> tag and got one of those horrible-looking newbie posts. I wonder if the newbies are enclosing code in a pair of <code> tags?

      Yes, sometimes they screw up the closing tags. But most of the times, they don't use any tags at all.

      thanks robiticus !!

      I used your suggestion with a minor change as I realised that my example line wasn't a 100% accurate example (oops)

      if ($variable =~ /:\W{1,15}([0-9.]+)\W{1,15}([0-9.]+)/) { ($first, $second) = ($1, $2);

      worked out how to effectively ignore the spaces between everything.

      again many thanks for helping out a PERL newbie.

Re: data extraction from a variable
by Anonymous Monk on Jan 29, 2010 at 05:14 UTC