Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

I need to insert spaces and get the values for all the variables

by rahulruns (Beadle)
on Sep 04, 2013 at 07:52 UTC ( #1052241=perlquestion: print w/replies, xml ) Need Help??
rahulruns has asked for the wisdom of the Perl Monks concerning the following question:

I need to insert the spaces in the line and separate out all the variable with there respective values. After that I need to store all the values for one variable so that I can build a graph representation from it

The INPUT FILE LINES dvgqu="0"vgrq="0"w="0"devce="db"elped_me="0"r="0"rmb="0"rrqm="0"vcm="0 +"ul="0"w="0"wmb="0"wrqm="0" dvgqu="0"vgrq="0"w="0"devce="d"elped_me="0"r="0"rmb="0"rrqm="0"vcm="0" +ul="0"w="0"wmb="0"wrqm="0" dvgqu="0.00"vgrq="0.00"w="0.00"devce="db"elped_me="1"r="0.00"rmb="0.00 +"rrqm="0.00"vcm="0.00"ul="0.00"w="0.00"wmb="0.00"wrqm="0.00" dvgqu="0.00"vgrq="58.91"w="0.09"devce="d"elped_me="1"r="0.00"rmb="0.00 +"rrqm="0.00"vcm="0.09"ul="0.05"w="5.50"wmb="324.00"wrqm="35.00"

Replies are listed 'Best First'.
Re: I need to insert spaces and get the values for all the variables
by Random_Walk (Prior) on Sep 04, 2013 at 08:01 UTC

    Then you will need to plan out the steps to achieve this. I am guessing inserting spaces is not your true aim, but graphing or outputting a graphable format is.

    # open the file # Read a line at a time probably in a while loop # split the line on appropriate markers and assign the results # to a suitable data structure # look at split and hash data structures. # store the above result (print to a csv perhaps) # or plot the graph points, continue to the next line # close the file when done.


    Pereant, qui ante nos nostra dixerunt!

      Hi the point I am not able to get is to how to separate the data for the variables If the line is dvgqu="0.00"vgrq="0.00"w="0.00"devce="d"elped_me="174" how do I read it like dvgqu="0.00" vgrq="0.00" w="0.00" devce="d" elped_me="174" This would help in reading all the variables and their values. Without a proper separation I am not able to make out a pattern to match or to use for split

        my %vars = split /=?"/, $line;

        This splits on any " optionally preceded by the = sign.


        use strict; use warnings; use Data::Dumper; my @store; # put the results in here while (<DATA>) { chomp; next unless /\S+/; # Ignore empty lines my %vars = split /=?"/; # Do the splits push @store, \%vars; # record these samples } print Dumper \@store; # Look what we got __DATA__ dvgqu="0"vgrq="0"w="0"devce="db"elped_me="0"r="0"rmb="0"rrqm="0"vcm="0 +"ul="0"w="0"wmb="0"wrqm="0" dvgqu="0"vgrq="0"w="0"devce="d"elped_me="0"r="0"rmb="0"rrqm="0"vcm="0" +ul="0"w="0"wmb="0"wrqm="0" dvgqu="0.00"vgrq="0.00"w="0.00"devce="db"elped_me="1"r="0.00"rmb="0.00 +"rrqm="0.00"vcm="0.00"ul="0.00"w="0.00"wmb="0.00"wrqm="0.00" dvgqu="0.00"vgrq="58.91"w="0.09"devce="d"elped_me="1"r="0.00"rmb="0.00 +"rrqm="0.00"vcm="0.09"ul="0.05"w="5.50"wmb="324.00"wrqm="35.00"

        Update 2

        I just saw the variable name 'w' appears twice in the lines. In this case my code only keeps the last value. You may need to do something a little more complicated


        Pereant, qui ante nos nostra dixerunt!

        Try something like this

        while(<DATA>){ print join " ", /([^"=]+="[^"]+")/g; }
Re: I need to insert spaces and get the values for all the variables
by Jer2911 (Acolyte) on Sep 04, 2013 at 17:48 UTC
    You could load it up into a hash of array references:
    use strict; use warnings; use Data::Dumper; my %stuff_from_file; open (IN, "<input.txt"); while (<IN>) { my @array = split (/(?<!=)"/, $_); # Split each line by the " whic +h is not preceeded by = foreach my $ele (@array) { $ele =~ s/"//; # Remove the other " my ($key, $val) = split ("=", $ele); push @{$stuff_from_file{$key}},$val; } } print Dumper \%stuff_from_file; close IN;

      Thank You Jer2911. I am now able to extract the data in a better way and the information you have provided helped me to build a hash

        Great! Happy coding!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1052241]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2017-07-24 18:18 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (356 votes). Check out past polls.