For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2006 > -F vs split









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author -F vs split
Robert Citek

2006-08-25, 6:57 pm


Why do these two commands not produce the same output?

$ perl -le 'print join("--", split(/: /, "a: b"))'
a--b

$ echo 'a: b' | perl -F'/: /' -lane 'print join("--", @F)'
a: b

From reading 'perldoc perlrun' the -F option should behave just like
the pattern to split. What am I missing?

Regards,
- Robert
http://www.cwelug.org/downloads
Help others get OpenSource software. Distribute FLOSS
for Windows, Linux, *BSD, and MacOS X with BitTorrent

Robert Citek

2006-08-25, 6:57 pm


On Aug 25, 2006, at 2:43 PM, Robert Citek wrote:
> Why do these two commands not produce the same output?
>
> $ perl -le 'print join("--", split(/: /, "a: b"))'
> a--b
>
> $ echo 'a: b' | perl -F'/: /' -lane 'print join("--", @F)'
> a: b
>
> From reading 'perldoc perlrun' the -F option should behave just
> like the pattern to split. What am I missing?


Forgot to mention this is on OS X (perl 5.8.6), FC4 Linux (perl
5.8.6), and Ubuntu 6.06 (perl 5.8.7).

Regards,
- Robert
http://www.cwelug.org/downloads
Help others get OpenSource software. Distribute FLOSS
for Windows, Linux, *BSD, and MacOS X with BitTorrent

DJ Stunks

2006-08-25, 6:57 pm


Robert Citek wrote:
> Why do these two commands not produce the same output?
>
> $ perl -le 'print join("--", split(/: /, "a: b"))'
> a--b
>
> $ echo 'a: b' | perl -F'/: /' -lane 'print join("--", @F)'
> a: b
>
> From reading 'perldoc perlrun' the -F option should behave just like
> the pattern to split. What am I missing?


I would try adjusting the pattern you're sending to -F. The docs
indicate your single quotes are unnecessary.

I don't see it on Win32:

C:\>echo a: b | perl -F":\s" -lane "print join '--', @F"
a--b

C:\>perl -v
This is perl, v5.8.7 built for MSWin32-x86-multi-thread

-jp

Klaus

2006-08-25, 6:57 pm

Robert Citek wrote:
> Why do these two commands not produce the same output?
>
> $ perl -le 'print join("--", split(/: /, "a: b"))'
> a--b
>
> $ echo 'a: b' | perl -F'/: /' -lane 'print join("--", @F)'

^
You can't use literal whitespace in the pattern.

see perldoc perlrun

-Fpattern
specifies the pattern to split on if -a is also in effect. The pattern
may be surrounded by //, "", or '', otherwise it will be put in single
quotes. You can't use literal whitespace in the pattern.

> a: b


John W. Krahn

2006-08-25, 6:57 pm

Robert Citek wrote:
>
> Why do these two commands not produce the same output?
>
> $ perl -le 'print join("--", split(/: /, "a: b"))'
> a--b
>
> $ echo 'a: b' | perl -F'/: /' -lane 'print join("--", @F)'
> a: b
>
> From reading 'perldoc perlrun' the -F option should behave just like
> the pattern to split. What am I missing?


This is a "feature" :-) This thread may help explain:

http://groups.google.com/group/perl...655bddfdbf2d0d6

One way to do what you want:

$ echo 'a: b' | perl -F'/:\040/' -lane'print join "--", @F'
a--b


John
--
use Perl;
program
fulfillment
Robert Citek

2006-08-25, 6:57 pm


On Aug 25, 2006, at 3:30 PM, John W. Krahn wrote:
> This is a "feature" :-) This thread may help explain:
>
> http://groups.google.com/group/perl...wse_frm/thread/
> 2eca0c52a1b299c4/f655bddfdbf2d0d6?
> lnk=st&q=&rnum=1&hl=en#f655bddfdbf2d0d6
>
> One way to do what you want:
>
> $ echo 'a: b' | perl -F'/:\040/' -lane'print join "--", @F'
> a--b


Thanks, John, that worked for me, too. A less elegant solution I
found was to prefilter my input via sed:

$ echo 'a: b' |
sed -e 's/: /:/g' |
perl -F':' -lane 'print join("--", @F)'
a--b

I like yours better. Again, thanks.

Regards,
- Robert
http://www.cwelug.org/downloads
Help others get OpenSource software. Distribute FLOSS
for Windows, Linux, *BSD, and MacOS X with BitTorrent

Tom Phoenix

2006-08-25, 6:57 pm

On 8/25/06, Robert Citek <rwcitek@alum.calberkeley.org> wrote:

> $ echo 'a: b' | perl -F'/: /' -lane 'print join("--", @F)'
> a: b


For some reason, the space character seems not to match a space
character. I'd call it a bug. Other ways to match a space, like \s and
\x20, all seem to work:

echo 'a: b' | perl -F'/:\s+/' -lane 'print join("--", @F)'

See what happens when you file a bug report via the perlbug program. Cheers!

--Tom Phoenix
Stonehenge Perl Training
Dr.Ruud

2006-08-25, 6:57 pm

Robert Citek schreef:

> Why do these two commands not produce the same output?
>
> $ perl -le 'print join("--", split(/: /, "a: b"))'
> a--b
>
> $ echo 'a: b' | perl -F'/: /' -lane 'print join("--", @F)'
> a: b
>
> From reading 'perldoc perlrun' the -F option should behave just like
> the pattern to split. What am I missing?


Use -MO=Deparse to investigate the regenerated Perl code.

The '/: /' results in split(m[/:], $_, 0)

Try '/:\s/' and '/:\x20/' too.


It seems that a space in the -F-delimiter creates problems.

--
Affijn, Ruud

"Gewoon is een tijger."


John W. Krahn

2006-08-25, 6:57 pm

Tom Phoenix wrote:
> On 8/25/06, Robert Citek <rwcitek@alum.calberkeley.org> wrote:
>
>
> For some reason, the space character seems not to match a space
> character. I'd call it a bug. Other ways to match a space, like \s and
> \x20, all seem to work:
>
> echo 'a: b' | perl -F'/:\s+/' -lane 'print join("--", @F)'
>
> See what happens when you file a bug report via the perlbug program.


See the link in my post for the "bug" report I already filed on this.


John
--
use Perl;
program
fulfillment
DJ Stunks

2006-08-25, 6:57 pm

Dr.Ruud wrote:
> Robert Citek schreef:
>
>
> Use -MO=Deparse to investigate the regenerated Perl code.
>
> The '/: /' results in split(m[/:], $_, 0)


this -^^ gives you this ---------^

because you should use single quotes or slashes, but not both...

-jp

Tom Phoenix

2006-08-25, 6:57 pm

On 8/25/06, John W. Krahn <krahnj@telus.net> wrote:

> See the link in my post for the "bug" report I already filed on this.


Ah, I see that it's been fixed (or at least documented). Thanks.

--Tom Phoenix
Stonehenge Perl Training
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com