2 files-array->1files

Discussion in 'Perl Misc' started by kristoff, Aug 15, 2005.

  1. kristoff

    kristoff Guest

    Hi,
    I'm new in perl.
    I'm facing to a problem which will appear for most of you not really
    tricky, but I'm not comfortable with array stuff, so prefer asking
    question is case of someone made this in the past.

    I have 2 set of files, text files.
    1st one with this format ( systemname;message )


    bingo;ERROR Can't be reach port is closed
    pppp;system OK
    typo;System OK
    Ray;ERROR Can't be reach port is closed
    blush;ERROR Can't be reach port is closed
    bingo;system OK


    2nd FILE ( domain;system name ):
    HP;ppp
    HP;bingo
    IBM;typo
    COMPAQ;blush
    DELL;ray


    EXPECTED RESULT :
    I would like to match data, to a final txt files ( preferably an html
    output ) sorted or not :
    HP
    ppp System OK
    bingo System OK
    IBM
    typo System OK
    COMPAQ
    blush ERROR Can't be reach port is closed
    DELL
    ray ERROR Can't be reach port is closed


    Does someone have an idea or already realised this kind of stuff


    Many thanks in advance for you help
     
    kristoff, Aug 15, 2005
    #1
    1. Advertising

  2. kristoff wrote:
    > I have 2 set of files, text files.
    > 1st one with this format ( systemname;message )
    >
    > bingo;ERROR Can't be reach port is closed
    > pppp;system OK
    > typo;System OK
    > Ray;ERROR Can't be reach port is closed
    > blush;ERROR Can't be reach port is closed
    > bingo;system OK
    >
    > 2nd FILE ( domain;system name ):
    > HP;ppp
    > HP;bingo
    > IBM;typo
    > COMPAQ;blush
    > DELL;ray
    >
    > EXPECTED RESULT :
    > I would like to match data, to a final txt files ( preferably an html
    > output ) sorted or not :
    > HP
    > ppp System OK
    > bingo System OK
    > IBM
    > typo System OK
    > COMPAQ
    > blush ERROR Can't be reach port is closed
    > DELL
    > ray ERROR Can't be reach port is closed


    What have you tried? Which part of the problem do you have difficulties
    with? Where is the code you have written so far?

    Please study the posting guidelines for this group:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Aug 15, 2005
    #2
    1. Advertising

  3. kristoff wrote:
    > Hi,
    > I'm new in perl.
    > I'm facing to a problem which will appear for most of you not really
    > tricky, but I'm not comfortable with array stuff, so prefer asking
    > question is case of someone made this in the past.
    >
    > I have 2 set of files, text files.
    > 1st one with this format ( systemname;message )
    >
    >
    > bingo;ERROR Can't be reach port is closed
    > pppp;system OK
    > typo;System OK
    > Ray;ERROR Can't be reach port is closed
    > blush;ERROR Can't be reach port is closed
    > bingo;system OK
    >
    >
    > 2nd FILE ( domain;system name ):
    > HP;ppp
    > HP;bingo
    > IBM;typo
    > COMPAQ;blush
    > DELL;ray
    >
    >
    > EXPECTED RESULT :
    > I would like to match data, to a final txt files ( preferably an html
    > output ) sorted or not :
    > HP
    > ppp System OK
    > bingo System OK
    > IBM
    > typo System OK
    > COMPAQ
    > blush ERROR Can't be reach port is closed
    > DELL
    > ray ERROR Can't be reach port is closed
    >
    >
    > Does someone have an idea or already realised this kind of stuff


    Take a look at hashes and hashes of hashes.

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Aug 15, 2005
    #3
  4. Josef Moellers wrote:
    > kristoff wrote:
    >> I'm not comfortable with array stuff,

    >
    > Take a look at hashes and hashes of hashes.


    Yeah, that's _much_ easier.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Aug 15, 2005
    #4
  5. kristoff

    Joe Smith Guest

    kristoff wrote:

    > 1st one with this format ( systemname;message )
    > bingo;ERROR Can't be reach port is closed
    > pppp;system OK
    > typo;System OK
    > Ray;ERROR Can't be reach port is closed
    > blush;ERROR Can't be reach port is closed
    > bingo;system OK
    >
    > 2nd FILE ( domain;system name ):
    > HP;ppp
    > HP;bingo
    > IBM;typo
    > COMPAQ;blush
    > DELL;ray


    I would put all the info from the first file into one
    hash (key=systemname, val=message), assuming only one
    message per systemname.

    /(\w+);(.*)/ and $message{$1} = $2;

    Put the info from the second file into a second hash
    (key=domain, val=ref to anon array containing system names).

    /(\w+);(\w+)/ and push @{$systems{$1}},$2;

    Then process each key in %systems.

    foreach my $domain (sort keys %systems) {
    print "something with $domain ...\n";
    foreach my $system ( @{$systems{$domain}} ) {
    print "something with $system and $message{$system}\n";
    }
    }

    Those should be enough hints to get you going.
    -Joe
     
    Joe Smith, Aug 16, 2005
    #5
  6. kristoff wrote:

    > I have 2 set of files, text files.
    > 1st one with this format ( systemname;message )
    >
    >
    > bingo;ERROR Can't be reach port is closed
    > pppp;system OK
    > typo;System OK
    > Ray;ERROR Can't be reach port is closed
    > blush;ERROR Can't be reach port is closed
    > bingo;system OK
    >
    >
    > 2nd FILE ( domain;system name ):
    > HP;ppp
    > HP;bingo
    > IBM;typo
    > COMPAQ;blush
    > DELL;ray
    >
    >
    > EXPECTED RESULT :
    > I would like to match data, to a final txt files ( preferably an html
    > output ) sorted or not :
    > HP
    > ppp System OK
    > bingo System OK
    > IBM
    > typo System OK
    > COMPAQ
    > blush ERROR Can't be reach port is closed
    > DELL
    > ray ERROR Can't be reach port is closed


    A simple solution in Awk:

    BEGIN { FS = ";" }
    ARGV[1]==FILENAME { a[$1] = $2; next }
    { if (d != $1)
    { d = $1
    print " " d
    }
    print $2, a[$2]
    }

    Run with
    awk -f 2to1.awk file1 file2

    file1:
    bingo;ERROR Can't be reach port is closed
    ppp;system OK
    typo;System OK
    ray;ERROR Can't be reach port is closed
    blush;ERROR Can't be reach port is closed
    bingo;system OK

    file2:
    HP;ppp
    HP;bingo
    IBM;typo
    COMPAQ;blush
    DELL;ray

    output:
    HP
    ppp system OK
    bingo system OK
    IBM
    typo System OK
    COMPAQ
    blush ERROR Can't be reach port is closed
    DELL
    ray ERROR Can't be reach port is closed
     
    William James, Aug 16, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. crazyprakash
    Replies:
    4
    Views:
    3,377
    adrian
    Oct 30, 2005
  2. Replies:
    4
    Views:
    957
    M.E.Farmer
    Feb 13, 2005
  3. Replies:
    3
    Views:
    1,466
    Rolf Magnus
    Jan 18, 2009
  4. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    496
    David RF
    Sep 3, 2009
  5. Terry Michaels
    Replies:
    4
    Views:
    171
    w_a_x_man
    Oct 16, 2010
Loading...

Share This Page