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: Beginner:
question about split

 



makals
Novice

Dec 31, 2010, 5:00 AM

Post #1 of 4 (2042 views)
question about split Can't Post

hello

i have this script :

#!/usr/bin/perl

# summary.plx

use warnings;

use strict;

$/ = "";

my $counter = 1;

while (<>) {

print $counter++, ":";

print ((split /\n/, $_)[0]);

print "\n";



}


-------------

can someone exlpain these two lines:

print ((split /\n/, $_)[0]);

print "\n";

and forwhat is the 0




FishMonger
Veteran / Moderator

Dec 31, 2010, 5:24 AM

Post #2 of 4 (2039 views)
Re: [makals] question about split [In reply to] Can't Post

The starting point to understand those 2 lines is to read the Perl documentation for the 2 functions in the lines.
http://perldoc.perl.org/functions/print.html
http://perldoc.perl.org/functions/split.html

To answer your question about the 0 in the statement, you need to read up on slices.
http://perldoc.perl.org/perldata.html#Slices

If after reading those docs you still don't understand those lines, then post back with a more refined question based on what you read.


(This post was edited by FishMonger on Dec 31, 2010, 5:25 AM)


makals
Novice

Dec 31, 2010, 6:15 AM

Post #3 of 4 (2034 views)
Re: [FishMonger] question about split [In reply to] Can't Post

thank you but you know what the problem the english isnt my mother lang so i cant understand an official lang i already have a book about perl so if you would explain it in easy lang .


BillKSmith
Veteran

Jan 4, 2011, 4:24 PM

Post #4 of 4 (1978 views)
Re: [makals] question about split [In reply to] Can't Post

Because INPUT_RECORD_SEPARATOR ($/) is set, <> reads your entire file into $_. The split function breaks it up into lines. The array slice selects the first of these lines for printing. The second print prints a newline. This is necessary because split removed the original one.
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