Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Manipulating an array

Hello gurus


I have same information many times in an array which I would like to use only once before printing to a file. My array looks like this:
-------------------BEGIN--------------------------

for (@sorted1[0..199]) {
                @parts = split /\s*;\s*/;
                print FILEOUT  "$parts[5], $parts[12],  $parts[10]\n";

------------------END-----------------------------

This gives me next result:

KAR, Tree White,  TR
KAR, Stone Nice ,  YY
ETC, Karr Happo,  YY
ETC, Renne Artma,  EKD
ETC, Malk Johns,  RTT
122, Veikk Jann,  RTT
WSR, Mikka Routul,  TRR
WSR, Wss Easy,  EKD
KKK, Ar Parkk,  KDE
334, Wer Moert,  GH
334, Tim Paav,  DFF


Etc.

Now the tricky part. I need to change this so that it gives me next result:


KAR
Tree White,  TR
Stone Nice ,  YY

ETC
Karr Happo,  YY
Renne Artma,  EKD
Malk Johns,  RTT

122
Veikk Jann,  RTT

WSR
Mikka Routul,  TRR
Wss Easy,  EKD

KKK
Ar PArkk,  KDE

334
Wer Moert,  GH
Tim Paav,  DFF

Etc.

So I like to have first column to one own line when this character string appear first time and after this all the names which belong to this same string.

How I can make it ??

Thanks advance

-JS
0
salmjuh
Asked:
salmjuh
1 Solution
 
tomcleggCommented:
undef $head;
for (@sorted1[0..199]) {                
  @parts = split /\s*;\s*/;
  if($parts[5] ne $head) {
    $head = $parts[5];
    print FILEOUT "\n$head\n";
  }
  print FILEOUT  "$parts[12], $parts[10]\n";            
}

This also prints an empty line at the top of the file.  Is that OK?
0
 
salmjuhAuthor Commented:
Yes. This is GREAT !

Thanks.


-js
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now