I have a data file and 4th column looks like below: Some examples
34899939-34899967
34899939-34899967:349055 54-34905559
34899939-34899967:349055 54-34905559:349055 60-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: -
sub special {
-
-
chomp $_;
-
my @v = split(/\s+/,$_);
-
if($v[3] =~ /\:/) {
-
$num1 = $`;
-
$num2 = $';
-
if($num1 =~ /\-/) {
-
$n1 = $`;
-
$n2 = $';
-
}
-
if($num2 =~ /\-/) {
-
$n3 = $`;
-
$n4 = $';
-
}
-
}
-
$start = $n1;
-
$end = $n4;
-
print "$n1 \t $n2 \t $n3 \t $n4 \n";
-
-
}
-
But how do I generalise the numbers with : to generate the $n(i)? Thanks.
4 2168 KevinADC 4,059
Recognized Expert Specialist
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?
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
KevinADC 4,059
Recognized Expert Specialist
That helped clear it up. I hope I am not doing your school work for you. -
while(<DATA>) {
-
special($_);
-
}
-
-
sub special {
-
local ($_) = @_;
-
chomp $_;
-
my $col4 = (split(/\s+/))[3];
-
my @blocks = split(/:/,$col4);
-
my @temp;
-
for (@blocks) {
-
push @temp, split(/-/);
-
}
-
print "start = $temp[0]\n";
-
print "end = $temp[-1]\n";
-
print 'blocks = ', scalar @blocks,"\n";
-
for (@temp) {
-
print "\t$_\n";
-
}
-
print "\n";
-
}
-
__DATA__
-
dummy dummy dummy 34899939-34899967 dummy
-
dummy dummy dummy 34899939-34899967:34905554-34905559 dummy
-
dummy dummy dummy 34899939-34899967:34905554-34905559:34905560-34905574 dummy
-
Apply your own file I/O inplace of DATA
KevinADC 4,059
Recognized Expert Specialist
output is: -
start = 34899939
-
end = 34899967
-
blocks = 1
-
34899939
-
34899967
-
-
start = 34899939
-
end = 34905559
-
blocks = 2
-
34899939
-
34899967
-
34905554
-
34905559
-
-
start = 34899939
-
end = 34905574
-
blocks = 3
-
34899939
-
34899967
-
34905554
-
34905559
-
34905560
-
34905574
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 eliminate alot of
redundancy. What am i missing here?
|
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 is this... is Perl so
much better at parsing text files and outputing that we would see a
substantial speed increase? We process about 10 million records in
flat files a day for reformatting before putting them in a DB.
Also, when it comes to...
|
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 developing serious application,
e.g web application as it has not many features in it yet.
I've given up on Perl for its ugly syntax and it is not the easiest language
to learn. How about PHP?
Thanks
|
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
|
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'
| |
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 files from gif
# format to png format. Now you need to change the
# html code to use the .png files. So, essentially
|
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 functions are simplified versions of the full featured methods
for compiled regular expressions. Most non-trivial applications always
use the compiled form
UNQUOTE
|
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 know how to deal with it.
One particular example is a program which uses the DBI to connect to a
database, which will run fine normally but will hang during the call to
DBI->connect() when running under the debugger.
|
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 decribed in the books "Spidering Hacks" and
"Perl & LWP". I am familiar with the book "Webbots, Spiders, and Screen Scrapers: A
Guide to Developing Internet Agents with PHP/CURL" Would anyone know of any other
sources of info related to this kind...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |