[comp.lang.perl] split

jdr@sloth.mlb.semi.harris.com (Jim Ray) (02/01/91)

Probably sounds like a dumb question; but, is there any way to use
split given positional rather than string separators ( like --
split at cols 6 19 37 etc ).




--
Jim Ray                                Harris Semiconductor
Internet:  jdr@semi.harris.com         PO Box 883   MS 62B-022
Phone:     (407) 729-5059              Melbourne, FL  32901

rbj@uunet.UU.NET (Root Boy Jim) (02/01/91)

In article <1991Feb1.052629.16672@mlb.semi.harris.com> jdr@sloth.mlb.semi.harris.com (Jim Ray) writes:
>Probably sounds like a dumb question; but, is there any way to use
>split given positional rather than string separators ( like --
>split at cols 6 19 37 etc ).

Yes, there is, except you have to call it unpack.

perl -de 0

  DB<1> $qaz='one+two+three+four'

  DB<2> @wsx=unpack(A3xA3xA5xA4,$qaz)

  DB<3> p "@wsx"
one two three four
  DB<4> p $wsx[2]
three
  DB<5> p $#wsx
3
  DB<6> q

You can also use substring. And m// followed by $digit.
The following produces the same @wsx from $qaz:

	$qaz =~ m/(...).(...).(.....).(....)/;
	@wsx = ($1, $2, $3, $4);

"There's more than one way to do it."

	die "old age\n" if $^T > (60*60*24*365*70);	# more or less
-- 

	Root Boy Jim Cottrell <rbj@uunet.uu.net>
	Close the gap of the dark year in between

subbarao@phoenix.Princeton.EDU (Kartik Subbarao) (04/22/91)

Why do I have to say this in perl:

@foo = split(/[ ,]+/, join(' ', @ARGV));

When all I just want to say is:

@foo = splif(/[ ,]+/, @ARGV);

what I get in the second instance is the scalar number of things that were
split, rather than the actual split items....Am I misinterpreting what
split does?


		-Kartik

--
internet# rm `df | tail +2 | awk '{ printf "%s/quotas\n",$6}'`

subbarao@phoenix.Princeton.EDU -| Internet
kartik@silvertone.Princeton.EDU (NeXT mail)  
SUBBARAO@PUCC.BITNET			          - Bitnet

arielf@tasu8c.UUCP (Ariel Faigon) (04/22/91)

+--- In <z36534@idunno.Princeton.EDU> subbarao@phoenix (Kartik Subbarao) says:
| Why do I have to say this in perl:
|
| @foo = split(/[ ,]+/, join(' ', @ARGV));
|
| When all I just want to say is:
|
| @foo = splif(/[ ,]+/, @ARGV);
|
| what I get in the second instance is the scalar number of things that were
| split, rather than the actual split items....Am I misinterpreting what
| split does?
+---
Welcome to the context sensitive club ;-)

split converts a string (scalar) to a list (just like the similar
'unpack' function.)
It thus expects a scalar as its second argument.
Thus the second argument, the list @ARGV, is evaluated in a scalar-context.
A list in a scalar-context returns the number of elements in the list.

To recap the list<->scalar conversion functions:
'split' and 'unpack'  expect a list     and return a scalar
		their counterparts:
'join'  and 'pack'    expect a scalar   and return a list
Ariel Faigon, FAX group, NSTA
National Semiconductor (Israel)
6 Maskit st.  P.O.B. 3007, Herzlia 46104, Israel   Tel. (972)52-522272
arielf@taux01.nsc.com   @{hplabs,pyramid,sun,decwrl} 34 48 E / 32 10 N

rbj@uunet.UU.NET (Root Boy Jim) (04/23/91)

In <z36534@idunno.Princeton.EDU> subbarao@phoenix (Kartik Subbarao) writes:
?Why do I have to say this in perl:
?
?@foo = split(/[ ,]+/, join(' ', @ARGV));
?
?When all I just want to say is:
?
?@foo = split(/[ ,]+/, @ARGV);

Split takes anywhere from zero to three arguments.
The applicable one here is @array = split(/pat/,$string);

You should, however, be able to get away with

	@foo = split(/[ ,]+/, "@ARGV");

which will have the same effect as the join
providing you didn't change one of those variables
which I'm too lazy to look up (I lied: it's $").
-- 
		[rbj@uunet 1] stty sane
		unknown mode: sane

arielf@taux01.nsc.com (Ariel Faigon) (04/23/91)

+--- In article <5769@taux01.nsc.com> I tried to help and wrote:
|
| To recap the list<->scalar conversion functions:
| 'split' and 'unpack'  expect a list     and return a scalar
| 		their counterparts:
| 'join'  and 'pack'    expect a scalar   and return a list
+---
Which, of course, should be the other way round:

  'split' and 'unpack'  expect a scalar  and return a list
  		their counterparts:
  'join'  and 'pack'    expect a list    and return a scalar

Thanks to Eric W. Ziegast (ziegast@eng.umd.edu) for pointing this out.
---
-- 
Ariel Faigon, CTP group, NSTA
National Semiconductor (Israel)
6 Maskit st.  P.O.B. 3007, Herzlia 46104, Israel   Tel. (972)52-522312
arielf@taux01.nsc.com   @{hplabs,pyramid,sun,decwrl} 34 48 E / 32 10 N