Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Yet another question about reading in data from a file for use in another file

by wruehl (Acolyte)
on Dec 14, 2007 at 15:38 UTC ( #657058=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks, I am in need of a method to scan in a file formatted exactly like this:
Data Collected for SA120:Prct Dirty Cache Pages = 0 Data Collected for SA121:Prct Dirty Cache Pages = 1 Data Collected for SA122:Prct Dirty Cache Pages = 0 Data Collected for SA220:Prct Dirty Cache Pages = 0 Data Collected for SA221:Prct Dirty Cache Pages = 0 Data Collected for SA222:Prct Dirty Cache Pages = 17
and then be able to assign numerical values to variables for checking in another script. IE I'd like to be able to read in the file and then assign the numerical value after SA120 to $SA120 or some other value name. If I use something like the below, what would I need to alter in that code to get it to work? I'm not quite sure what is being done in the code below, but I think it searches for a text match with SA120, then does something else, but I'm not quite sure what as it isn't my source code.
foreach (<F_INFILE_3>) { chomp $_; $_ =~ s /^\s//g; $_ =~ s /\./_/g; if ( $_ =~ m/SA120/g ) { @TEMP_ARRAY_1 = split("=",$_); @TEMP_ARRAY_2 = split(":",$TEMP_ARRAY_1[1]); $SA120 = $TEMP_ARRAY_2[1]; }
Thanks,
-Bill

Replies are listed 'Best First'.
Re: Yet another question about reading in data from a file for use in another file
by RaduH (Scribe) on Dec 14, 2007 at 16:02 UTC
    Here's what happens in your script:

    The input file is read line by line by the foreach loop. Each iteration through the loop reads and processes the current line, which is stored in the $_variable.

    chomp makes sure there's no newline character at the end of your $_; The following line removes leading whitespace (^ means match at the beginning and \s is a special character that stands for 1 white space and // before g is 'nothing' so -replace white space with nothing). I'm not sure I understand why the g at the end as it stands for 'global' but your regexp specifically says 'beginning of $_' (by the use of ^)

    The next line replaces the occurrence of a dot with _. You don't seem to have any dots in your input so I'm guessing that must be used on other input files or must have been copied from elsewhere without any adjustment.

    Next, your 'if' tries to find if your current line $_ contains the string 'SA120'. If it does you go inside the block if not I'm guessing you move on to the next iteration of your foreach loop (the end of that loop is missing in your published code)

    If you find the string, you split it where the = symbol shows up, which means you get the numeric value at the right in the second "half" of the string and the text to the left of = in the first "half". Then you split the first half again, where a colon is. This is where your problem occurs. your $TEMP_ARRAY_21 is something like "Prct Dirty Cache Pages". You don't need that you need:

    $SA120 = $TEMP_ARRAY_1[1];
    You already got your numeric value after the first split so remove the second split (does nothing (good) for you) and update the assignment so your $SA120 gets the second half of the FIRST (and not only) split.

    Hope this helps!

      Thanks, that's exactly what I needed.
      -Bill
Re: Yet another question about reading in data from a file for use in another file
by davies (Prior) on Dec 14, 2007 at 16:03 UTC
    I'm sure there are better ways of doing the 'split', but the following works for me:
    use strict; use warnings; use diagnostics; my %forchecking; while (<DATA>) { chomp; my @sFields = split /\s+/; $forchecking{substr($sFields[3],0,5)} = $sFields[8]; } __DATA__ Data Collected for SA120:Prct Dirty Cache Pages = 0 Data Collected for SA121:Prct Dirty Cache Pages = 1 Data Collected for SA122:Prct Dirty Cache Pages = 0 Data Collected for SA220:Prct Dirty Cache Pages = 0 Data Collected for SA221:Prct Dirty Cache Pages = 0 Data Collected for SA222:Prct Dirty Cache Pages = 17
    Regards,

    John Davies
Re: Yet another question about reading in data from a file for use in another file
by caelifer (Scribe) on Dec 14, 2007 at 16:23 UTC
    This might be a little Perl-golfish, but it offers you named parameters for further processing
    use strict; use Data::Dumper; $/ = ""; my %stats = map { m/(\w+?): .* = \s* (\d+)/xm; $1 => $2; } <DATA>; print Dumper(\%stats); __DATA__ Data Collected for SA120:Prct Dirty Cache Pages = 0 Data Collected for SA121:Prct Dirty Cache Pages = 1 Data Collected for SA122:Prct Dirty Cache Pages = 0 Data Collected for SA220:Prct Dirty Cache Pages = 0 Data Collected for SA221:Prct Dirty Cache Pages = 0 Data Collected for SA222:Prct Dirty Cache Pages = 17
    Regards,

    - caelifer

Re: Yet another question about reading in data from a file for use in another file
by poolpi (Hermit) on Dec 15, 2007 at 16:05 UTC
    TIMTOW...
    use strict; use warnings; use Data::Dumper; local $/; my %data = (map{unpack 'x19A5x25A*', $_}<DATA>); print Dumper(\%data); __DATA__ Data Collected for SA120:Prct Dirty Cache Pages = 0 Data Collected for SA121:Prct Dirty Cache Pages = 1 Data Collected for SA122:Prct Dirty Cache Pages = 0 Data Collected for SA220:Prct Dirty Cache Pages = 0 Data Collected for SA221:Prct Dirty Cache Pages = 0 Data Collected for SA222:Prct Dirty Cache Pages = 17

    OUTPUT:
    $VAR1 = { ' SA220' => ' 0', ' SA221' => ' 0', ' SA122' => ' 0', ' SA222' => ' 17', ' SA121' => ' 1', ' SA120' => ' 0' };

    update :
    /.+(SA\d{3}).+?(\d+)(?{$data{$1}=$2})/ for <DATA>

    HTH,
    PooLpi

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2022-07-06 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?