Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array reformat 2

Status
Not open for further replies.

3inen

Technical User
May 26, 2005
51
US
Hi!
i have multiple accouts with account number (right now upto 3 digits) in coulmn 1 and different entries separated by single space and ending with the .ca1

the number of entries differ for each account and i dont know before hand how many there would be per account.

i want to change the array format as shown below. so far, i have tried 2 different ways, one using regular expression and the other using hash. but both scripts needs improvement.

can any body help.

thanks in advance


Input
Account10 L6_VI_TI0S000007796.ca1 L5_VI_TI0S0000077912.ca1 L4_VI_TI0S00000779.ca1
Account1 L6_VI_TI0S000007793.ca1 VIT_S0_S0_0000779.ca1
Account120 VITIS0S00000779234.ca1 VITIS0S000007792.ca1


expected Output
Account10 L6_VI_TI0S000007796.ca1
Account10 L5_VI_TI0S0000077912.ca1
Account10 L4_VI_TI0S00000779.ca1
Account1 L6_VI_TI0S000007793.ca1
Account1 VIT_S0_S0_0000779.ca1

so far.....

#!/usr/bin/perl-w


open(DATA, "<into.txt");
while (<DATA>){
chomp;
/(Account\d{1,3})\s(.*.ca1)/;
print "$1\n$2\n\n";
}

and.......

#!/usr/bin/perl-w


open(DATA, "<into.txt");
while (<DATA>){
chomp;
($Account, $grade) = /(Account\d{1,3})\s(.*.ca1)/;

$entries{$Account} .= $grade . ab1; }

foreach $Account (sort keys %entries) {



print "$Account\n$entries\n\n";
}

 
Use the split function to split each line into pieces.
Code:
while(<DATA>) {
   chomp;
   my @pieces = split;
   for my $i ( 1 .. $#pieces ) {
      print "$pieces[0] $pieces[ $i ]\n";
   }
}
 
wow ishnid it works like a beauty
Thanks


 
Code:
while (<DATA>) {
    chomp;
    my ($account, @info) = split;
    foreach (@info) {
        print "$account $_\n";
    }
}
...but Ishnid's looks cooler
 
both solutions above are perfect for what you asked, but if you wanted something more flexible, where you could do something with the data besides print it in the same order as the data file, like sort it by Account or the data associated with each account, you could use a data set:

Code:
#!perl 
use strict; 
use warnings; 
my %HofA = ();
while(<DATA>) {
   chomp;
   my @pieces = split;
   push @{$HofA{shift(@pieces)}},@pieces; 
}
foreach my $keys (sort keys %HofA){
   print "$keys :\n";
   for (sort @{$HofA{$keys}}) {
      print "   $_\n";
   }
   print "\n";
}
__DATA__
Account10    L6_VI_TI0S000007796.ca1    L5_VI_TI0S0000077912.ca1  L4_VI_TI0S00000779.ca1
Account1    L6_VI_TI0S000007793.ca1    VIT_S0_S0_0000779.ca1
Account120    VITIS0S00000779234.ca1    VITIS0S000007792.ca1

which will print:

Code:
Account1 :
   L6_VI_TI0S000007793.ca1
   VIT_S0_S0_0000779.ca1

Account10 :
   L4_VI_TI0S00000779.ca1
   L5_VI_TI0S0000077912.ca1
   L6_VI_TI0S000007796.ca1

Account120 :
   VITIS0S000007792.ca1
   VITIS0S00000779234.ca1

but if that is not something you do not need the other solutions will be perfect.
 
hehehe... I made a double-negative:

but if that is not something you do not need the other solutions will be perfect.

should be:

but if that is not something you need the other solutions will be perfect.


 
KevinADC

You are a star.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top