473,499 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Removing Duplicate Headings

1 New Member
Im somewhat stuck on a project im working on and im pretty new to Perl, so i hope some perl heads can help me out here:)

I have an array that looks somewhat like this:

heading 1
data2keep
data2keep
data2keep
heading 1
data2keep
data2keep
heading 2
data2keep
data2keep
data2keep
data2keep
heading 2
data2keep
data2keep
heading 3
data2keep
data2keep
data2keep
.
.
Now as you see, headings occur several times even tho they are identical. (This due to page shifts in a SAP file export)
I want only one ocurance of each heading.

So, the result should look like:

heading 1
data2keep
data2keep
data2keep
data2keep
data2keep
heading 2
data2keep
data2keep
data2keep
data2keep
data2keep
data2keep
heading 3
data2keep
data2keep
data2keep
So far i have this code, but it just identifies the linenumbers of where a new "block" starts:
Expand|Select|Wrap|Line Numbers
  1. foreach (@data) {
  2.     if ($_ =~ m/heading/) {
  3.         $_{$_}++;
  4.         unless($_{$_} > 1) {
  5.             push(@block,"\t$i\t $_\n");
  6.         }
  7.     }
  8.     $i++;
  9. }
  10.  
So, i need some function to do splice on those headers i dont want.
Hope anyone can help.
Aug 23 '07 #1
3 1146
kevin24
4 New Member
Hi

Welcome to perl scripts!

If your particularly looking to splice a function, then see perl slice function in "tizag.com" which i suggest a good learning online site...

See if that helps you..

Best regards,

Kevin..
Aug 24 '07 #2
numberwhun
3,509 Recognized Expert Moderator Specialist
Not to be nit-picky, but please do not top post (post on top of others postings).

there?
is
now
backwards,
discussion
a
read
to
trying
like
nothing
is
there
Because

Regards,

Jeff
Aug 24 '07 #3
KevinADC
4,059 Recognized Expert Specialist
One of probably many ways to do what you want using a hash of arrays to store the data and another array to maintain heading order:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. chomp(my @data = <DATA>);
  4. my @order = ();
  5. my %HoA = ();
  6. my $heading;
  7. foreach my $line (@data) {
  8.    if ($line =~ m/^heading/) {
  9.       $heading = $line;
  10.       if (!exists $HoA{$line}) {
  11.          push @order,$line;
  12.       }
  13.    }
  14.    else {
  15.       push @{$HoA{$heading}},$line;
  16.    }
  17. }
  18.  
  19. foreach my $line (@order){
  20.    print $line,"\n",join("\n",@{$HoA{$line}}),"\n";
  21. }
  22.  
  23. __DATA__
  24. heading 1
  25. data2keep-1
  26. data2keep-1
  27. data2keep-1
  28. heading 1
  29. data2keep-1
  30. data2keep-1
  31. heading 2
  32. data2keep-2
  33. data2keep-2
  34. data2keep-2
  35. data2keep-2
  36. heading 2
  37. data2keep-2
  38. data2keep-2
  39. heading 3
  40. data2keep-3
  41. data2keep-3
  42. data2keep-3
Aug 24 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
17930
by: Rad | last post by:
I have a table . It has a nullable column called AccountNumber, which is of varchar type. The AccountNumber is alpha-numeric. I want to take data from this table and process it for my application....
7
3554
by: Ellen Manning | last post by:
I've got an A2K report showing students and their costs. Student info is in the main report and costs are in a subreport for each student. The user inputs the program desired then only those...
9
5076
by: vbportal | last post by:
Hi, I would like to add BitArrays to an ArrayList and then remove any duplicates - can someone please help me forward. I seem to have (at leaset ;-) )2 problems/lack of understanding (see test...
1
3457
by: gaikokujinkyofusho | last post by:
Hi, I have been enjoying being able to subscribe to RSS (http://kinja.com/user/thedigestibleaggie) for awhile and have come up with a fairly nice list of feeds but I have run into an annoying...
12
20770
by: joestevens232 | last post by:
Hello Im having problems figuring out how to remove the duplicate entries in an array...Write a program that accepts a sequence of integers (some of which may repeat) as input into an array. Write...
1
2241
by: PerumalSamy | last post by:
Hi I am having table with more 13 lakhs records. I am having duplicate records in it. i need to remove that. I wrote the following query SELECT *
7
2798
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
3
7487
by: Clownboat | last post by:
I've just written a CSS/HTML flyout menu as an addition to an existing system (EQdkp Plus, to be specific). The menu is generated by a couple PHP functions. The output is an unordered list as in...
7
3586
by: Anthony97 | last post by:
I'm working on removing duplicate records from a table in sql. I've tried using select distinct, and I've grouped by but I'm still running into some issues. Attached is a sample of the output table.
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.