split doubt

2006-05-25 Thread Saurabh Singhvi

Hi

the format of split() defines that one
can split a string into a fixed number
of specifies strings. for eg

($login, $passwd, $remainder) = split(/:/, $_, 3);

Now, the thing is, it splits on first 3 parts.
Can i do the reverse?? as in instead of the output
being the first 3 parts of split, the last 3 parts of
split for the same string.

thanks
Saurabh


Re: split doubt

2006-05-25 Thread Chris Charley


- Original Message - 
From: Saurabh Singhvi [EMAIL PROTECTED]

Newsgroups: perl.beginners
To: Perl FAq beginners@perl.org
Sent: Thursday, May 25, 2006 3:38 PM
Subject: split doubt



Hi

the format of split() defines that one
can split a string into a fixed number
of specifies strings. for eg

($login, $passwd, $remainder) = split(/:/, $_, 3);

Now, the thing is, it splits on first 3 parts.
Can i do the reverse?? as in instead of the output
being the first 3 parts of split, the last 3 parts of
split for the same string.

thanks
Saurabh



Sure, the code below will do that.

Chris


#!/usr/bin/perl
use strict;
use warnings;

my $string = a;b;c;d;e;f;

my ($stuff1, $stuff2, $stuff3) = (split /;/, $string)[-3..-1];

print $stuff1 $stuff2 $stuff3\n;

__END__
this prints...

d e f



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: split doubt

2006-05-25 Thread Joshua Colson
On Thu, 2006-05-25 at 19:38 +, Saurabh Singhvi wrote:
 Hi
 
 the format of split() defines that one
 can split a string into a fixed number
 of specifies strings. for eg
 
 ($login, $passwd, $remainder) = split(/:/, $_, 3);
 
 Now, the thing is, it splits on first 3 parts.
 Can i do the reverse?? as in instead of the output
 being the first 3 parts of split, the last 3 parts of
 split for the same string.

I'm sure there is a cleaner way to write this, but try:

($gecos, $home, $shell) = ( split(/:/, $_) )[-3..-1];


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: split doubt

2006-05-25 Thread Mr. Shawn H. Corey
On Thu, 2006-25-05 at 13:17 -0700, Joshua Colson wrote:
 On Thu, 2006-05-25 at 19:38 +, Saurabh Singhvi wrote:
  Hi
  
  the format of split() defines that one
  can split a string into a fixed number
  of specifies strings. for eg
  
  ($login, $passwd, $remainder) = split(/:/, $_, 3);
  
  Now, the thing is, it splits on first 3 parts.
  Can i do the reverse?? as in instead of the output
  being the first 3 parts of split, the last 3 parts of
  split for the same string.
 
 I'm sure there is a cleaner way to write this, but try:
 
 ($gecos, $home, $shell) = ( split(/:/, $_) )[-3..-1];
 
 

Wrong!

There is no way split can do the reverse of splitting of the first 2
parts of a string and placing the rest in the third part. Something that
may come close is:

my @data = split /:/, $_;
my $last = pop @data;
my $next_to_last = pop @data;
my $remainder = join( ':', @data );


-- 
__END__

Just my 0.0002 million dollars worth,
   --- Shawn

For the things we have to learn before we can do them, we learn by doing them.
  Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: split doubt

2006-05-25 Thread John W. Krahn
Saurabh Singhvi wrote:
 Hi

Hello,

 the format of split() defines that one
 can split a string into a fixed number
 of specifies strings. for eg
 
 ($login, $passwd, $remainder) = split(/:/, $_, 3);
 
 Now, the thing is, it splits on first 3 parts.
 Can i do the reverse?? as in instead of the output
 being the first 3 parts of split, the last 3 parts of
 split for the same string.

$ perl -le'
$_ = q[a:b:c:d:e:f:g];
( $login, $passwd, $remainder ) = split /:/, $_, 3;
print $login, $passwd, $remainder;
( $login, $passwd, $remainder ) = map scalar reverse, split /:/, reverse, 3;
print $login, $passwd, $remainder;
'
a, b, c:d:e:f:g
g, f, a:b:c:d:e




John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response