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:
Split Function

 



aksagar
New User

Feb 28, 2002, 9:12 PM

Post #1 of 15 (3313 views)
Split Function Can't Post

Hi,
I am currently analising the pre-written tool in perl. I stuck at a line which actually splits a string on some basis.Can someone explain me on what basis it is spliting a string.Attached is a piece od code contaning Split function.


$kanji = 0;
$rest = $maxrest;
@l = split(/([\001-\006\b\f\n\r\e<crlf>])/);
while (defined($w = shift(@l))) {
if ($w eq '') { next; }
if ($w eq "\f") { $bl || &nl; &rp; next; }
# formfeed
$bl && &bl;
if ($w eq "\b") { $rest++, print ' bs' if $rest < $maxrest; next; }
if ($w eq "\n") { &nl; next; }
# newline
if ($w eq "\r") { &cr; &bl; next; }
# carriage return
if ($w eq "\1") { print ' B'; next; }
# bold start
if ($w eq "\2") { print ' R'; next; }
# bold end
if ($w eq "\3") { print ' I'; next; }
# italic start
if ($w eq "\4") { print ' R'; next; }
# italic end
if ($w eq "\5") { $kanji = 1; next; }
# kanji start
if ($w eq "\6") { $kanji = 0; next; }
# kanji end
if ($w eq "\e") {
# half line/char moves
if ($l[$[] =~ s/^6//) { &half_char_back; next; }
if ($l[$[] =~ s/^7//) { &half_char_forw; next; }
if ($l[$[] =~ s/^8//) { &half_line_back; next; }
if ($l[$[] =~ s/^9//) { &half_line_forw; next; }
$w = $esc;
}


yapp
User

Mar 1, 2002, 2:28 AM

Post #2 of 15 (3302 views)
Re: [aksagar] Split Function [In reply to] Can't Post


In Reply To
@l = split(/([\001-\006\b\f\n\r\e])/);

Do this line troubles you?

split accepts a regular expression as first parameter.
The second parameter contains the script to be splitted. $_ will be used when that value is not provided. not that some lines set $_ when to variable is used to assign the value to. (like while(<FILE>)

The regular expression is actually very simple. everything between [ and ] is a character class. In other words, one of those characters should match. That is: ASCII codes 1 to 6, a \b, \f, \n (newline), \r \e
That's it.

See 'perlfaq6' and 'perlre' in the perldoc documentation for more information

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool


aksagar
New User

Mar 1, 2002, 3:06 AM

Post #3 of 15 (3297 views)
Re: [yapp] Split Function [In reply to] Can't Post

Hi ,
Thanks for the instant reply but one more thing is use of
() and [] I am not getting that thing what happens if you put characters in [] or () or like([\b\r])

Thanks once again


Paul
Enthusiast

Mar 1, 2002, 3:15 AM

Post #4 of 15 (3295 views)
Re: [aksagar] Split Function [In reply to] Can't Post

Using ( ) stores the match in the special numerical scalar, $1, $2 etc...so you could do:

$_ = 'Hello my name is Joe Bloggs';
/^(\w+)\s(\w+)/ and ($hello,$my) = ($1,$2);

[ ] are used for character classes. ie they match a range of characters eg... [a-z0-9] they also disable meta-characters. Putting a ^ at the beginning tells the regex not to match any of the characters....eg

/^[^a-z]+$/

....will match a string that doesn't have a,b,c,d,e....z


(This post was edited by RedRum on Mar 1, 2002, 3:16 AM)


freddo
User

Mar 1, 2002, 6:35 AM

Post #5 of 15 (3288 views)
Re: [aksagar] Split Function [In reply to] Can't Post

Hi Aksagar,

Still about (), when they're used in split patterns, they return the char used when splitting:[perl]$x = "1+2-3";
print join ':', split /([+-])/, $x; # 1:+:2:-:3
print join ':', split /[+-]/, $x; # 1:2:3[/perl]

'hope this helps
freddo
;---


Jasmine
Administrator / Moderator

Mar 1, 2002, 4:06 PM

Post #6 of 15 (3282 views)
Re: [aksagar] Split Function [In reply to] Can't Post

If you're curious about what /([\001-\006\b\f\n\r\e<crlf>])/ does in the split line, here's the regex, as explained by [url=http://search.cpan.org/search?dist=YAPE-Regex-Explain]YAPE::Regex::Explain


Code
  /                        '/' 
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[\001- any character of: '\001' to '\006', '\b'
\006\b\f\n\r\e<crlf>] (backspace), '\f' (form feed), '\n'
(newline), '\r' (carriage return), '\e'
(escape), '<', 'c', 'r', 'l', 'f', '>'
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
/ '/'



aksagar
New User

Mar 3, 2002, 5:41 PM

Post #7 of 15 (3267 views)
Re: [aksagar] Split Function [In reply to] Can't Post

Hi All,
Thanks for the instant help.

Best Regards,
aksagar


yapp
User

Mar 4, 2002, 8:55 AM

Post #8 of 15 (3256 views)
Re: [Jasmine] Split Function (off topic) [In reply to] Can't Post

YAPE::Regex::Explain ??

Pretty cool. I might even want to write a CGI interface for it!
Do you know anymore of those cool modules?

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool


Jasmine
Administrator / Moderator

Mar 4, 2002, 10:33 AM

Post #9 of 15 (3250 views)
Re: [yapp] Split Function (off topic) [In reply to] Can't Post

[url=http://search.cpan.org/search?dist=YAPE-Regex-Explain]YAPE::Regex::Explain is [ japhy ]'s baby. Here's what I use for the interface -- not pretty, but totally functional Smile

[perl]#!/usr/bin/perl

use CGI;
use YAPE::Regex::Explain;
use strict;

my $in = CGI->new();
my $regex;
my $exp;

{ local $/; chomp( $regex = $in->param('regex') ) }

print $in->header,
$in->start_html( -title => 'YAPE' ),
$in->start_form( -method => 'post'),
$in->textarea( { -cols => '60', -rows => '3', -name => 'regex' } ),
$in->submit( -value => 'Explain!' ),
$in->end_form,
$in->hr;

$exp = YAPE::Regex::Explain->new( eval "qr/\$regex/" );
$exp->parse or $exp->error and die "explain error: " . $exp->error;

print $in->pre( $exp->explain ), $in->end_html;[/perl]

edit: fixed url


(This post was edited by Jasmine on Mar 4, 2002, 7:51 PM)


Paul
Enthusiast

Mar 4, 2002, 11:47 AM

Post #10 of 15 (3246 views)
Re: [Jasmine] Split Function (off topic) [In reply to] Can't Post

No modules found matching 'YAPE-Regex-Explain'


mhx
Enthusiast / Moderator

Mar 4, 2002, 12:37 PM

Post #11 of 15 (3244 views)
Re: [RedRum] Split Function (off topic) [In reply to] Can't Post

Why? I have no problem to find [url=http://search.cpan.org/search?dist=YAPE-Regex-Explain]it at CPAN?

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



Paul
Enthusiast

Mar 4, 2002, 12:39 PM

Post #12 of 15 (3242 views)
Re: [mhx] Split Function (off topic) [In reply to] Can't Post

Don't ask me Smile, I only pasted what cpan showed me Pirate


mhx
Enthusiast / Moderator

Mar 4, 2002, 12:40 PM

Post #13 of 15 (3240 views)
Re: [RedRum] Split Function (off topic) [In reply to] Can't Post

Sometimes the CPAN server acts a bit strange, I know from my own experience Tongue

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



Paul
Enthusiast

Mar 4, 2002, 12:48 PM

Post #14 of 15 (3238 views)
Re: [mhx] Split Function (off topic) [In reply to] Can't Post

It is _reaaaaally_ slow, I do know that much Angelic


Jasmine
Administrator / Moderator

Mar 4, 2002, 7:53 PM

Post #15 of 15 (3230 views)
Re: [RedRum] Split Function (off topic) [In reply to] Can't Post

My bad. One of the urls in my post was wrong.

The correct url to YAPE::Regex::Explain is [url=http://search.cpan.org/search?dist=YAPE-Regex-Explain]http://search.cpan.org/search?dist=YAPE-Regex-Explain.

Fixed the broken url in my last post. Sorry 'bout that.

 
 


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

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