Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Writing to file

martin23martin23 Posts: 6Member
hi, can anyone help, I can append to a file without any problems but am having difficulty getting output in this format.

name monday tuesday weds
server1 10 10.5 12
server2 30 35 40
server3 20 29 32

the numbers represent disk space utilised at a set time on any day, I can extract the information in the following way :-

server1 10
server2 30
server3 20
server1 10.5
server2 35
server3 29

i.e. appending to a file but would like to output to a file in the above format, any help would be greatly appreciated.

Comments

  • JonathanJonathan Posts: 2,914Member
    : hi, can anyone help, I can append to a file without any problems but
    : am having difficulty getting output in this format.
    :
    : name monday tuesday weds
    : server1 10 10.5 12
    : server2 30 35 40
    : server3 20 29 32
    :
    : the numbers represent disk space utilised at a set time on any day,
    : I can extract the information in the following way :-
    :
    : server1 10
    : server2 30
    : server3 20
    : server1 10.5
    : server2 35
    : server3 29
    :
    : i.e. appending to a file but would like to output to a file in the
    : above format, any help would be greatly appreciated.
    :
    Me thinks a hash is the right tool for the job here. You can use the server name as the hash key, then concatenate the data onto the appropriate hash element. By way of example, here's how you'd read the existing file in:-

    [code]open FILE, ") {
    chomp; # Gets rid of newline character.
    /^(w+)(.*)$/; # Finds server name and current data.
    $serverSpace{$1} = $2; # Drops it in the hash.
    }
    close FILE;[/code]

    Then suppose we have the new data in a hash called %newData, we can insert it into the main %serverSpace one something like:-

    [code]for (keys %newData) {
    $serverSpace{$_} .= " " . $newData{$_};
    }[/code]

    Note the is to sort out spacing and I'm assuming that isn't in the %newData values - I'll leave you to work out just what you want.

    Then it's just writing the file back to disk. If you want them sorted by server name, leave sort in, otherwise drop it.

    [code]open FILE, ">thefile.txt";
    for (sort keys %serverSpace) {
    print FILE $_ . $serverSapce{$_};
    }
    close FILE;[/code]

    There may be the odd thing to fix or neaten up in there, but hopefully this gives you some ideas.

    Jonathan


    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.