Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Transpose the entries

by iqbala (Initiate)
on Jan 08, 2004 at 15:02 UTC ( [id://319815]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks

I am trying to get the following input in transpose form
--------------- Name: Tim Brown username: txbro27 cuid: twbrown --------------- Name: Dean Walker username: dxwalk3 --------------- username: jxrizzo Name: John Rizzo --------------- Name: Doreen Mirlass username: mirlasd --------------- cuid: chamblem Name: Mike Chambless username: mblem --------------- name: Tim McCarthy username: txmccar cuid: tmmccar ---------------
There are three things to notice here

- The order of the Name/cuid/username not always same.
- They need to considered as case insensitive
- sometime the cuid is missing
The output should be in this format
username:fname lname:cuid
If the cuid is missing the value should be EMPTY. For
example the above input will give the following output
txbro27:Tim Brown:twbrown dxwalk3:Dean Walker:EMPTY jxrizzo:John Rizzo:EMPTY mirlasd:Doreen Mirlass:EMPTY mblem:Mike Chambless:chamblem txmccar:Tim McCarthy:tmmccar
Any help with this would really help me a lot
Thanks

Replies are listed 'Best First'.
Re: Transpose the entries
by boo_radley (Parson) on Jan 08, 2004 at 15:14 UTC
    First off, I'd set the record separator to that big line of dashes, then push each 'line' into an array. I'd do this in a block, so that the record separator would automatically return to it's regular value of "\n" after the block. Then I'd go through the array I created in step one, and create a hash by splitting on ": " and newlines, taking care to make sure that a 'cuid' key exists. I'd push this hash into a different array. At that point, I'd be ready to loop through this second array to create output in whatever format I'd need.
    update What abigail-II says.
Re: Transpose the entries
by borisz (Canon) on Jan 08, 2004 at 15:21 UTC
    Like this?
    #!/usr/bin/perl local $/ = "---------------"; while( <DATA> ) { next if /^\S*$/; my ( $n, $u, $c ); ( $n ) = /Name:\s+(.*)$/mi; ( $u ) = /username:\s+(.*)$/mi; ( $c ) = /cuid:\s+(.*)$/mi; $n ||= 'EMPTY'; $u ||= 'EMPTY'; $c ||= 'EMPTY'; print "$u:$n:$c\n"; } __DATA__ --------------- Name: Tim Brown username: txbro27 cuid: twbrown --------------- Name: Dean Walker username: dxwalk3 --------------- username: jxrizzo Name: John Rizzo --------------- Name: Doreen Mirlass username: mirlasd --------------- cuid: chamblem Name: Mike Chambless username: mblem --------------- name: Tim McCarthy username: txmccar cuid: tmmccar ---------------
Re: Transpose the entries
by Abigail-II (Bishop) on Jan 08, 2004 at 15:14 UTC
    Here's a general outline of the solution:
    1. Open the file.
    2. Read in a record.
    3. Process the record.
    4. Print the processed record.
    5. Finish if you have read in the last record.
    6. Go to step 2.

    Abigail

Re: Transpose the entries
by Fletch (Bishop) on Jan 08, 2004 at 15:11 UTC

    Fish.

    my %rec = ( cuid => "EMPTY" ); scalar <>; # eat first seperator line. while( <> ) { chomp; if( /^-+$/ ) { print "$rec{username}:$rec{name}:$rec{cuid}\n"; %rec = ( cuid => "EMPTY" ); } else { my( $k, $v ) = split( /:\s*/, $_, 2 ); $rec{ lc $k } = $v; } }

    Update: Missed the part about the default.

Re: Transpose the entries
by Theo (Priest) on Jan 08, 2004 at 15:11 UTC
    What have you tried so far?

    -Theo-
    (so many nodes and so little time ... )

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-19 05:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found