473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

perl regex

89
I have a data file and 4th column looks like below: Some examples

34899939-34899967
34899939-34899967:34905554-34905559
34899939-34899967:34905554-34905559:34905560-34905574


I have to extract like below:
For the first line:
$start = 34899939
$end = 34899967
$block_size = 1

For the 2nd line:
$start = 34899939
$end = 34905559
$block_size = 2
$n1=34899939
$n2=34899967
$n3=34905554
$n4=34905559

For the 3rd line:
$start = 34899939
$end = 34905574
$block_size = 3
$n1=34899939
$n2=34899967
$n3=34905554
$n4=34905559
$n5=34905560
$n6=34905574

I am able to differentiate 1 block and 2 block depending upon : character and able to find the solution for the 3rd line as below:
Expand|Select|Wrap|Line Numbers
  1. sub special {
  2.  
  3.         chomp $_;
  4.         my @v = split(/\s+/,$_);        
  5.         if($v[3] =~ /\:/) {
  6.         $num1 = $`;
  7.         $num2 = $';
  8.                 if($num1 =~ /\-/) {
  9.                         $n1 = $`;
  10.                         $n2 = $';
  11.                 }
  12.                 if($num2 =~ /\-/) {
  13.                         $n3 = $`;
  14.                         $n4 = $';
  15.                 }
  16.         }
  17.         $start = $n1;
  18.         $end = $n4;
  19.         print "$n1 \t $n2 \t $n3 \t $n4 \n";
  20.  
  21. }
  22.  
But how do I generalise the numbers with : to generate the $n(i)? Thanks.
Feb 19 '09 #1
4 2163
KevinADC
4,059 Expert 2GB
Your data is confusing. What do you mean "the 4th column looks like this"? You have posted three seperate lines of data. Are they part of a larger line of data? Why in your sub special() are you first splitting on spaces when there is no spaces in the data you posted?
Feb 19 '09 #2
lilly07
89
Hi Kevin, sorry for the confusion. Let me try to explain.

My 4th column can contain data as given in my previous post. It can contain without : or with one or two or three set of numbers separated by :

Hence they are different kinds of data available in 4th column of each data.

Every line I parse it to get the 4th column hence I split using space to get my 4th column data. And still parse with a special character : and then further split the example.

If my 4th column is like 1st example, then it is easy for me to split the numbers and put them into $n1 and $n2.

If my 4th column is like 2nd line example with one :, then my subroutine special does the job and assign $n1,$n2,$n3 and $n4.

But I want to write a generalised routine which can handle like line 3 or even with more number of:

I hope I have explained clearly.
Regards
Feb 19 '09 #3
KevinADC
4,059 Expert 2GB
That helped clear it up. I hope I am not doing your school work for you.

Expand|Select|Wrap|Line Numbers
  1. while(<DATA>) {
  2.    special($_);
  3. }
  4.  
  5. sub special {
  6.    local ($_) = @_; 
  7.    chomp $_;
  8.    my $col4 = (split(/\s+/))[3];
  9.    my @blocks = split(/:/,$col4);
  10.    my @temp;
  11.    for (@blocks) {
  12.       push @temp, split(/-/);
  13.    }
  14.    print "start = $temp[0]\n";
  15.    print "end = $temp[-1]\n";
  16.    print 'blocks = ', scalar @blocks,"\n";
  17.    for (@temp) {
  18.       print "\t$_\n";
  19.    }
  20.    print "\n";
  21. }
  22. __DATA__
  23. dummy dummy dummy 34899939-34899967 dummy
  24. dummy dummy dummy 34899939-34899967:34905554-34905559 dummy 
  25. dummy dummy dummy 34899939-34899967:34905554-34905559:34905560-34905574 dummy 
  26.  
Apply your own file I/O inplace of DATA
Feb 19 '09 #4
KevinADC
4,059 Expert 2GB
output is:

Expand|Select|Wrap|Line Numbers
  1. start = 34899939
  2. end = 34899967
  3. blocks = 1
  4.     34899939
  5.     34899967
  6.  
  7. start = 34899939
  8. end = 34905559
  9. blocks = 2
  10.     34899939
  11.     34899967
  12.     34905554
  13.     34905559
  14.  
  15. start = 34899939
  16. end = 34905574
  17. blocks = 3
  18.     34899939
  19.     34899967
  20.     34905554
  21.     34905559
  22.     34905560
  23.     34905574
  24.  
edit the output for your needs to display it how you need to. I added "start", "end" and "blocks" just to make it easier to read.
Feb 19 '09 #5

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

Similar topics

7
by: el_roachmeister | last post by:
Is there a good article that explains why php does not support =~ like perl for regex? I am confused by all the different ways one can do regex in php but it would seem supporting =~ would...
6
by: John Smith | last post by:
Hello, I have a rather odd question. My company is an all java/oracle shop. We do everything is Java... no matter what it is... parsing of text files, messaging, gui you name it. My question...
77
by: Hunn E. Balsiche | last post by:
in term of its OO features, syntax consistencies, ease of use, and their development progress. I have not use python but heard about it quite often; and ruby, is it mature enough to be use for...
17
by: Michael McGarry | last post by:
Hi, I am just starting to use Python. Does Python have all the regular expression features of Perl? Is Python missing any features available in Perl? Thanks, Michael
1
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
9
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # Matching string patterns # # Sometimes you want to know if a string is of # particular pattern. Let's say in your website # you have converted all images...
75
by: Xah Lee | last post by:
http://python.org/doc/2.4.1/lib/module-re.html http://python.org/doc/2.4.1/lib/node114.html --------- QUOTE The module defines several functions, constants, and an exception. Some of the...
3
by: Pat Deegan | last post by:
Greetings, I've been having issues while debugging programs. The problems only appear when using the '-d' switch and I get the impression it has to do with UTF8 and regex matching but I don't...
1
by: George Orwell | last post by:
Would I be missing much if I stopped trying to learn Perl well enough to use for spidering, screen scraping etc. and converted over to PHP ? I am looking to do all, or at least most of the hacks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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,...

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.