Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Parse variables from @output

by roboticus (Chancellor)
on Nov 14, 2019 at 20:55 UTC ( #11108700=note: print w/replies, xml ) Need Help??

in reply to Parse variables from @output


You don't specify what's wrong, so I can only guess: I'm thinking that line 15 should be:

$details{'Main Memory'} = $1 if (/^\S+ Intel (\S+)/i);

You have only one capture group (i.e., part of the regular expression inside parenthesis), so the captured value can only be in $1. For example:

$ cat use strict; use warnings; my $t = "Now is the time for all good men to come to the aid"; if ($t =~ /^(\w+) (\w+) (\w+) (\w+) (\w+) (\w+)/) { print "Words: 2='$2', 4='$4', 1='$1'\n"; } else { print "*** no match! ***\n"; }

when you run it, you should get:

$ perl Words: 2='is', 4='time', 1='Now'

In order to get better answers, it's helpful to include a bit more information. Especially if you can make a simple bit of code that can demonstrate the problem as I just did here. I hope I guessed correctly, but if not, update the code in your original question to make it clear. You could do so by putting something like:

use strict; use warnings; my @output = ( "a few lines of data that I cut\n", "and pasted from a screen capture that I want to\n", "process.\n", );

at the front, and something like:

print "Chassis: <$details{'Chassis'}>\n"; print "Main Memory: <$details{'Main Memory'}>\n";

at the end to show what you actually get. Then specify what you actually wanted instead, so we can clearly see the problem. Note the angle brackets I put in the output: whitespace handling errors (especially newlines and carriage returns) are so common that it's helpful to put something before and after the string so you can see if there are unwanted characters in your output.


When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: Parse variables from @output
by Cisco_Dave (Novice) on Nov 14, 2019 at 22:41 UTC
    Thanks for the quick reply. Will fully read and understand fully tomorrow. Have edited original post to help clarify a little.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2021-10-26 14:10 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (90 votes). Check out past polls.