Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Good afternoon! I am new to this forum. In fact this is my first post. I am querying a database and then trying to join the data onto a single line that has the same id in the first field. Here is an example of what the beginning date looks like:

9885 Room 1105A 9885 Jack 1105A--05D 9885 org_code B703 9885 Building 1268A 114948 nasa_nets off 114948 blockSecName test name 114948 blockTechName brian test 114948 blockAdminName test admin 114949 blockSecName John G. Smooth 114949 blockTechPhone 222-555-1212 114949 blockAdminName Lucy P. Wallice 114949 blockAdminId 8878787 114949 block_name unknown 114949 blockSecId 787878 114949 blockAdminEmail 114949 blockTechName TEST LAN 114949 blockSecPhone 222-555-3232 114949 blockTechEmail 114949 blockSecEmail 114949 nasa_nets off

Note that the first field has an ID. the second, third, and fourth may be different. I don't need to keep the first field at all. Here is what I would like the end result to look like:|Room=1105A|Jack=1105A--05D|org_code=B703|Building=1268A|nasa_nets=off|blockSecName=test name|blockTechName=brian t +est|blockA dminName=test admin|blockSecName=John G. Smooth|blockTechPhone=222-555 +-1212|bloc kAdminName=Lucy P. Wallice|blockAdminId=8878787|block_name=unknown|blo +ckSecId=78 7878||blockTechName=TEST LAN| +blockSecPhon e=222-555-3232||blockSecEmail=Jo +hn.good|nasa_nets=off

Notice that every line that had the same first field was put on the same line. In fact, I suppose we can just ignore the first field and just use the second field. So now that you know I am least trying, here is what I have tried:

$out_file="/tmp/brian_test.$datestring"; open(IN,"<$TMPCONTLIST") or die "Can't open $TMPCONTLIST!\n"; open(OUT,">$out_file") or die "Can't open $out_file!\n"; while ($line=<IN>) { @fields = split /\t/,$line; $line = join "|", @fields[1,2,3,4]; print OUT $line; print OUT "\n"; } close IN; close OUT;

It is giving me this....just a sample|User_POC_Phone|222-555-2322 ||Room|1105A ||Jack|1105A--05D ||org_code|B703 ||Building|1268A

Can you please help? Thanks in advance!!!

In reply to split then join based on common value in field by brianjb

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (2)
    As of 2018-05-26 01:12 GMT
    Find Nodes?
      Voting Booth?