Help - Split Function Blowing My Mind Away!! 
Author Message
 Help - Split Function Blowing My Mind Away!!

Please consider the following function:

sub wri{*filter*}try
{


   {
      print "$field<br>\n";
   }

Quote:
}

During debugging I have printed the value of $_[0] and it's as expected.
The value is:

Derek Battams|Mail Address Here|URL Here|Comment Here

When I do the print in the foreach loop I get each character as an element

delimiter?  If I were to change the delimiter to something like "tt" then
the split is done as expected.  I'm new to Perl so I'm probably doing
something stupid, but I just don't see it.

Help appreciated,

Derek Battams



Fri, 25 Jan 2002 03:00:00 GMT  
 Help - Split Function Blowing My Mind Away!!


  DB> Derek Battams|Mail Address Here|URL Here|Comment Here

  DB> When I do the print in the foreach loop I get each character as an

  DB> with the pipe as the delimiter?  If I were to change the delimiter
  DB> to something like "tt" then the split is done as expected.  I'm
  DB> new to Perl so I'm probably doing something stupid, but I just
  DB> don't see it.

split takes a full regular expression and not just a fixed string as its
split argument. and | is a special character in regexes. so you have to
quote it to get it to look like a plain char.

        split( '\|', blah

will do it.

for more, read these documents:

perldoc -f split
perldoc perlre

uri

--
Uri Guttman  -----------------  SYStems ARCHitecture and Software Engineering

Have Perl, Will Travel  -----------------------------  http://www.sysarch.com
The Best Search Engine on the Net -------------  http://www.northernlight.com
"F**king Windows 98", said the general in South Park before shooting Bill.



Fri, 25 Jan 2002 03:00:00 GMT  
 Help - Split Function Blowing My Mind Away!!
[Posted and a courtesy copy sent.]



...

Quote:

...
>       Why isn't the string being split with the pipe as the
> delimiter?  If I were to change the delimiter to something like "tt" then
> the split is done as expected.  I'm new to Perl so I'm probably doing
> something stupid, but I just don't see it.

The first argument to split(), though for historical reasons often
written as a string, is really a regular expression.  The vertical bar
is a regular-expression metacharacter.

Write that line thus:


And all will be well.  (This is on my Top Ten list of Perl surprises for
beginners.  The vertical bar is a popular field separator!)

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Fri, 25 Jan 2002 03:00:00 GMT  
 Help - Split Function Blowing My Mind Away!!

<URL::">
??
?? When I do the print in the foreach loop I get each character as an element

?? delimiter?

That's because the first argument of split is a regex. In regexes, many
punctuation characters mean something special. Which characters they
are, and what they mean is explained in the manual about regexes. Please
consult the manual and find nirvana.

Abigail
--

0)x299=>C=>(0)x9=>XC=>(0)x39=>L=>(0)x9=>XL=>(0)x29=>X=>IX=>0=>0=>0=>V=>IV=>0=>0
=>I=>$r=-2449231+gm_julian_day+time);do{until($r<$#r){$_.=$r[$#r];$r-=$#r}for(;
!$r[--$#r];){}}while$r;$,="\x20";print+$_=>September=>MCMXCIII=>()'

  -----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
   http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 73,000 Newsgroups - Including  Dedicated  Binaries Servers ==-----



Fri, 25 Jan 2002 03:00:00 GMT  
 Help - Split Function Blowing My Mind Away!!

Quote:

>Please consider the following function:



split wants a regular expression as its first argument. Regular expressions
have a certain syntax that is explained in the perlre documentation.

You want something like : split /\|/. $_[0]

I suspect anyway.

--
Sam

The most exciting phrase to hear in science, the one that heralds new
discoveries, is not "Eureka!" (I found it!) but "That's funny ..."
        --Isaac Asimov



Sat, 26 Jan 2002 03:00:00 GMT  
 Help - Split Function Blowing My Mind Away!!

Quote:
>Write that line thus:


>And all will be well.  (This is on my Top Ten list of Perl surprises for
>beginners.  The vertical bar is a popular field separator!)

A case of "Ceci n'est pas une pipe"?

Anno



Sat, 26 Jan 2002 03:00:00 GMT  
 Help - Split Function Blowing My Mind Away!!


Quote:

> >Write that line thus:


> >And all will be well.  (This is on my Top Ten list of Perl surprises for
> >beginners.  The vertical bar is a popular field separator!)

> A case of "Ceci n'est pas une pipe"?

You bet it isnt.  It is ASCII VL (vertical line).  Calling it a 'pipe'
(as the poster did, and which I refused to echo) boggles one's mind with
irrelevant semantics from an unrelated domain.

As Freud (might have) said, 'Not every cigar is a phallic symbol.'

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Sat, 26 Jan 2002 03:00:00 GMT  
 Help - Split Function Blowing My Mind Away!!

Quote:



>> >Write that line thus:


>> >And all will be well.  (This is on my Top Ten list of Perl surprises for
>> >beginners.  The vertical bar is a popular field separator!)

>> A case of "Ceci n'est pas une pipe"?

>You bet it isnt.  It is ASCII VL (vertical line).  Calling it a 'pipe'
>(as the poster did, and which I refused to echo) boggles one's mind with
>irrelevant semantics from an unrelated domain.

The twisted logic in that quote allows me to reply: Why, that's
what I said.

Anno



Sat, 26 Jan 2002 03:00:00 GMT  
 Help - Split Function Blowing My Mind Away!!

Quote:

> The first argument to split(), though for historical reasons often
> written as a string, is really a regular expression.  The vertical bar
> is a regular-expression metacharacter.
> Write that line thus:

> And all will be well.  (This is on my Top Ten list of Perl surprises for
> beginners.  The vertical bar is a popular field separator!)

If you really have such a list, I'd be very interested in seeing it.

--
John Callender

http://www.west.net/~jbc/



Sat, 26 Jan 2002 03:00:00 GMT  
 Help - Split Function Blowing My Mind Away!!
[Posted and a courtesy copy mailed.]



Quote:

> > The first argument to split(), though for historical reasons often
> > written as a string, is really a regular expression.  The vertical bar
> > is a regular-expression metacharacter.

> > Write that line thus:


> > And all will be well.  (This is on my Top Ten list of Perl surprises for
> > beginners.  The vertical bar is a popular field separator!)

> If you really have such a list, I'd be very interested in seeing it.

Well, I'll show them, in no particular order.  I'm sure others will be
able to slice and dice, transmogrify and improve.

I put this list together without looking at the 'Common Goofs for
Novices' section in the Blue Camel (page 528 et seq.).  These are my
informal recollections from comp.lang.perl.misc.

Not included are mechanical failures, such as syntax errors, failure to
use '-w' and 'use strict;', etc.  Those in the list are all semantic
errors, unremarked at run time except by bad results.

Also not included are performance botches (such as hideous sorts with
repeated complex key extraction) or poor algorithms (such as character-
at-a-time processing where functions would do the job), or style errors
(lower-case filehandles, upper-case variables, uninterpolating double-
quoted strings).

Failure to check for failure of system calls.

Failure to chomp() input lines.

Failure to binmode() a binary file.

Failure to escape metacharacters in split().

Failure to prefix directory before file tests using readdir().

Use of ==, etc. instead of eq, etc.

Use of = instead of == or eq.

Use of captured variables from regex match without checking for success.


Y2K and month-1 errors using localtime().

Confusion of Boolean tests for true/false, defined(), exists().

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Sat, 26 Jan 2002 03:00:00 GMT  
 Help - Split Function Blowing My Mind Away!!

11 Aug 1999 00:22:24 -0400, Ronald J Kimball

Quote:




> > > > And all will be well.  (This is on my Top Ten list of Perl surprises for
> > > > beginners.  The vertical bar is a popular field separator!)

> > > If you really have such a list, I'd be very interested in seeing it.

> > Well, I'll show them, in no particular order.  I'm sure others will be
> > able to slice and dice, transmogrify and improve.

> > [snip]

> That looked more like a list of Top Ten beginner mistakes, and
> split(/|/) wasn't even on it.

Huh?

Failure to escape metacharacters in split().

Quote:
>    Where's the list of Top Ten Perl surprises for beginners?  :)

The surprises come at runtime, when things don't work as they *should*.

OK, surprises.  Gotchas.  That's harder.  Let's start with the one at
hand:

1. The first argument to join() is a string, but to split() is a regex
(no matter that it might look like a string).

2. "e:\foo\bar" is unlikely to be a good filename, but 'e:/foo/bar' is.

I'll keep thinking.  Anyone???

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Sat, 26 Jan 2002 03:00:00 GMT  
 Help - Split Function Blowing My Mind Away!!

Quote:

> [Posted and a courtesy copy mailed.]




> > > And all will be well.  (This is on my Top Ten list of Perl surprises for
> > > beginners.  The vertical bar is a popular field separator!)

> > If you really have such a list, I'd be very interested in seeing it.

> Well, I'll show them, in no particular order.  I'm sure others will be
> able to slice and dice, transmogrify and improve.
> [snip]

That looked more like a list of Top Ten beginner mistakes, and
split(/|/) wasn't even on it.  Where's the list of Top Ten Perl
surprises for beginners?  :)

--
 _ / '  _      /       - aka -

    /                                http://www.tiac.net/users/chipmunk/
        "It's funny 'cause it's true ... and vice versa."



Sun, 27 Jan 2002 03:00:00 GMT  
 Help - Split Function Blowing My Mind Away!!

Quote:

>> [snip]

>That looked more like a list of Top Ten beginner mistakes, and
>split(/|/) wasn't even on it.  Where's the list of Top Ten Perl
>surprises for beginners?  :)

???

Failure to escape metacharacters in split().

Andreas



Sun, 27 Jan 2002 03:00:00 GMT  
 Help - Split Function Blowing My Mind Away!!

Quote:

> OK, surprises.  Gotchas.  That's harder.  Let's start with the one at
> hand:

> 1. The first argument to join() is a string, but to split() is a regex
> (no matter that it might look like a string).

> 2. "e:\foo\bar" is unlikely to be a good filename, but 'e:/foo/bar' is.

> I'll keep thinking.  Anyone???

How about:

        Autovivification

        $/ is a string. Not a regexp.

        for() loops localize $_, while() loops don't.

        metacharacters in scalars used in pattern matching
        /$match/ (should be /\Q$match\E/)

?

Ala



Sun, 27 Jan 2002 03:00:00 GMT  
 Help - Split Function Blowing My Mind Away!!

Quote:



Are you saying that both of those are wrong, or that the first is wrong
and the second correct?

(Because I use that second form, or variants of it, all the time with no
problem...)

--
Michael Kozlowski                                        
http://www.cs.wisc.edu/~mlk/



Sun, 27 Jan 2002 03:00:00 GMT  
 
 [ 20 post ]  Go to page: [1] [2]

 Relevant Pages 

1. jpg files blown away

2. Anyway to do away with ?: in the map function

3. How to use split function to split on a backslash

4. Problem with join function (and split function)

5. help to take space away from an array

6. Help with split please - or maybe split isn't correct

7. using split function twice on same line

8. Split function

9. Using SPLIT function with a Period

 

 
Powered by phpBB® Forum Software