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: Intermediate:
read fields from the textfile without using any loop

 



Sushma
Novice

Jan 19, 2012, 2:11 AM

Post #1 of 10 (2342 views)
read fields from the textfile without using any loop Can't Post

Hi,

i m trying to read the 4th field of each line of a text file without using any loop. is it possible in perl? can anybody help me?

Thanks,

Sushma.


rovf
Veteran

Jan 19, 2012, 5:49 AM

Post #2 of 10 (2336 views)
Re: [Sushma] read fields from the textfile without using any loop [In reply to] Can't Post

This depends on how you define the word "loop", but maybe the functions map and split will do what you want.


Sushma
Novice

Jan 19, 2012, 5:54 AM

Post #3 of 10 (2335 views)
Re: [rovf] read fields from the textfile without using any loop [In reply to] Can't Post

split ll work for only one line not for all the line. i tried using split also.

Thanks,

Sushma.


rovf
Veteran

Jan 19, 2012, 6:22 AM

Post #4 of 10 (2334 views)
Re: [Sushma] read fields from the textfile without using any loop [In reply to] Can't Post

Please read my reply again, and pay attention to the two words before the word "split".


FishMonger
Veteran / Moderator

Jan 19, 2012, 9:10 AM

Post #5 of 10 (2325 views)
Re: [Sushma] read fields from the textfile without using any loop [In reply to] Can't Post

Why don't you want to use a loop?

Here's an equally logical question to yours.

How can I drive my car to the store without driving on any roads?


(This post was edited by FishMonger on Jan 19, 2012, 9:12 AM)


rovf
Veteran

Jan 20, 2012, 2:17 AM

Post #6 of 10 (2270 views)
Re: [FishMonger] read fields from the textfile without using any loop [In reply to] Can't Post


Quote
How can I drive my car to the store without driving on any roads?


I guess there are quite a few SUV addicts who would find this question completely reasonable ;-)

And of course, seeing programming as a sportive activity, we programmers know perfectly well: It is possible to enter any garden without using the garden door, and it is possible to write any program using only recursive functions and "if" (but no "else" please - "else" branches are for sissies).


FishMonger
Veteran / Moderator

Jan 20, 2012, 6:20 AM

Post #7 of 10 (2262 views)
Re: [rovf] read fields from the textfile without using any loop [In reply to] Can't Post


In Reply To
I guess there are quite a few SUV addicts who would find this question completely reasonable ;-)

I thought of that when I was posting, but since we programmers need to be picky and pay attention to minuet details, SUV's aren't cars. They are automobiles. :)


In Reply To
And of course, seeing programming as a sportive activity, we programmers know perfectly well: It is possible to enter any garden without using the garden door, and it is possible to write any program using only recursive functions and "if" (but no "else" please - "else" branches are for sissies).

And by god, NO elsif :-)


BillKSmith
Veteran

Jan 20, 2012, 11:01 AM

Post #8 of 10 (2249 views)
Re: [FishMonger] read fields from the textfile without using any loop [In reply to] Can't Post

Sometimes it is fun to drive off-road.


Code
  

use strict;
use warnings;
$/ = undef;
my $file_content = <DATA>;
my $FIELD = qr/[^,]*/;
my @fours = $file_content =~ /^(?:$FIELD,){3}($FIELD)/msg;
print join( "\n", @fours), "\n";
__DATA__
1,2,3,4,5,6
a,b,c,d,e,f
one,two,three,four,five,six

Good Luck,
Bill


histrung
Novice

Jan 22, 2012, 11:38 AM

Post #9 of 10 (2204 views)
Re: [BillKSmith] read fields from the textfile without using any loop [In reply to] Can't Post

Can you explain the use of ?:, I read the perl docs but not sinking in.


BillKSmith
Veteran

Jan 22, 2012, 5:08 PM

Post #10 of 10 (2186 views)
Re: [histrung] read fields from the textfile without using any loop [In reply to] Can't Post

This is part of the syntax for "non-capturing parenthesis" I need parenthesis to make {3} refer to the right things, but did not want their contents in the results. Refer: (?:pattern) in perldoc perlre.
Good Luck,
Bill

 
 


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

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