CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Extract certain words from lines

 



mhadidi2002
New User

Jun 9, 2009, 7:14 PM

Post #1 of 7 (4097 views)
Extract certain words from lines Can't Post

 
Dear all,

I am a beginner using perl; scripting, so i wish you cna help me...

I have a file containing a data like this:

>CHA 2
#419150.000000
[4639567,4639674] [4825157,4825264] 7196
[4639511,4639565] [4825101,4825155] 8416
#1346.000000
[4612526,4612558] [859583,859615] 1843
[4612489,4612510] [859547,859568] 17296
#1263.000000
[4612623,4612644] [3296095,3296116] 18439
[4612483,4612515] [3296057,3296089] 4517

this data is transfered to an array, so each line in 1 element of this array (@data)

I want a code that extract the first no. in the first square bracket in each line in @arr1(4639567,4639511,... for example).

Thank you in advance :)


KevinR
Veteran


Jun 9, 2009, 10:23 PM

Post #2 of 7 (4093 views)
Re: [mhadidi2002] Extract certain words from lines [In reply to] Can't Post

Please post whatever code you have already written and tried, even if it did not work.

Edit:

http://forums.devshed.com/perl-programming-6/help-in-perl-script-617005.html
-------------------------------------------------


(This post was edited by KevinR on Jun 9, 2009, 10:25 PM)


vikas.deep
User

Jun 9, 2009, 10:27 PM

Post #3 of 7 (4091 views)
Re: [mhadidi2002] Extract certain words from lines [In reply to] Can't Post

didi

Before posting anything or in addition to posting anything you are advised to read or roam a bit

As recently as 5 june, 2009 Fishmonger(Re: [mikkin] scope of File handle June5,2009)has written the following in a thread in Beginners section. I quote


Quote
6) Slurping the file into an array is rarely the best or proper approach. I most cases you want to loop over the data "line-by-line" or "record-by-record".


This and other golden tips are not only for the Original Poster (OP) but any body who is interested.

Why do you slurp the file contents in an array.

Please check if the following code helps you


Code
my @arr1; 
open (my $FH, "in.txt") or die "cannot open the file $!";
while (<$FH>)
{if ($_ =~ /\>/g||$_ =~ /\#/g){next;}else
{ my($num1,$num2,$num3) = unpack("xA7A*");

push @arr1, $num1,",";

}
}

print @arr1

The output is

Code
C:\ActivePerl>perl try.pl 
4639567,4639511,4612526,4612489,4612623,4612483,

and the in.txt had the following

Code
>CHA 2  
#419150.000000
12345678901234567890123456789012345678901234567890
[4639567,4639674] [4825157,4825264] 7196
[4639511,4639565] [4825101,4825155] 8416
#1346.000000
[4612526,4612558] [859583,859615] 1843
[4612489,4612510] [859547,859568] 17296
#1263.000000
[4612623,4612644] [3296095,3296116] 18439
[4612483,4612515] [3296057,3296089] 4517

-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


vikas.deep
User

Jun 9, 2009, 10:29 PM

Post #4 of 7 (4090 views)
Re: [KevinR] Extract certain words from lines [In reply to] Can't Post

Kevin

(Dear) Sir

I never knew that you were (also) working here Only when I completed my reply to this post and viewed my reply post I came to know about your presence.

Why do some people post same question in different forums It means they are not confident that somebody will reply them and this devshed keeps appearing again and again Next time I will check out devshed first and then only post any reply here

Thanks fo rthe hyperlink
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

(This post was edited by vikas.deep on Jun 9, 2009, 10:35 PM)


KevinR
Veteran


Jun 9, 2009, 10:48 PM

Post #5 of 7 (4084 views)
Re: [vikas.deep] Extract certain words from lines [In reply to] Can't Post


In Reply To
Why do some people post same question in different forums It means they are not confident that somebody will reply them and this devshed keeps appearing again and again Next time I will check out devshed first and then only post any reply here

Thanks fo rthe hyperlink


I can't tell you why people post on more than one forum. I can guess though. They just want an answer and they don't see anything wrong or inappropriate with asking on more than one forum.

I alert people to this activity when I see it so you are aware they might already have an answer so you don't waste your time helping them or so that you can decide if you want to help them at all.
-------------------------------------------------


mhadidi2002
New User

Jun 10, 2009, 4:58 PM

Post #6 of 7 (4070 views)
Re: [vikas.deep] Extract certain words from lines [In reply to] Can't Post

 
Dear Eng vikas.deep,

First of all, I'd like to thank you for your help, really it works well.
secondly, I would like to inform you that is the first time to use forum to ask question, so I may have no idea about its regualtion or policies.

really i posted in more than forum just to have more replies, to learn different ideas from many people ( THE AIM IS TO LEARN MORE) not because i am not confident that no one will reply me...

please tell be if it isn't favorable in the world of forums, i am not aware of it...


Thanks again...


vikas.deep
User

Jun 10, 2009, 9:17 PM

Post #7 of 7 (4065 views)
Re: [mhadidi2002] Extract certain words from lines [In reply to] Can't Post

didi

Firstly what do you mean by "Eng vikas.deep"

My name is Vikas Deep and "vikas-dot-deep" is my log-in id here. What is that "Eng" that you have prefixed to my name; I do not know. You or anybody else reserves the right to Learn as much as you want.

"No-body can teach you anything if you do not want but you can learn anything/everything if you want".

But posting same question in different forums duplicates and wastes a lot of effort and time.

My advise to you is to at-least wait for 24- 36 hrs and then only post the same question in another forum.

One basic decency is not to use a section for anything other than it is meant for So all this stuff should have been ideally posted in "General-discussion".

Even replying to your ("Non- Regular Expression or Non-Perl") question here I stand a strong chance of receiving admonition by some or other regular-visitor.
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives