Home: Perl Programming Help: Intermediate:
read fields from the textfile without using any loop



Sushma
Novice

Jan 19, 2012, 2:11 AM


Views: 5013
read fields from the textfile without using any loop

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


Views: 5007
Re: [Sushma] read fields from the textfile without using any loop

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


Views: 5006
Re: [rovf] read fields from the textfile without using any loop

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


Views: 5005
Re: [Sushma] read fields from the textfile without using any loop

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


Views: 4996
Re: [Sushma] read fields from the textfile without using any loop

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


Views: 4941
Re: [FishMonger] read fields from the textfile without using any loop


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


Views: 4933
Re: [rovf] read fields from the textfile without using any loop


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


Views: 4920
Re: [FishMonger] read fields from the textfile without using any loop

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


Views: 4875
Re: [BillKSmith] read fields from the textfile without using any loop

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


BillKSmith
Veteran

Jan 22, 2012, 5:08 PM


Views: 4857
Re: [histrung] read fields from the textfile without using any loop

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