* kinda perl split ...
@ 2006-03-21 13:42 Marc Chantreux
2006-03-21 13:51 ` Dominic Mitchell
2006-03-21 15:33 ` Marc Chantreux
0 siblings, 2 replies; 10+ messages in thread
From: Marc Chantreux @ 2006-03-21 13:42 UTC (permalink / raw)
To: zsh-users
Hi all,
i'm always searching for shortest ways in zsh (coming from perl world).
in perl :
while ( <>) {
chomp;
my ( $a , $b ) = split /\t/;
print "b=$b a=$a"
}
in zsh :
while {read} {
content=( ${(ps:\t:)REPLY} )
a=$content[1]
b=content[2]
print "b=$b a=$a"
}
is there something more faster to initialize a and b (peraps closer than
split) ?
regards
mc
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: kinda perl split ...
2006-03-21 13:42 kinda perl split Marc Chantreux
@ 2006-03-21 13:51 ` Dominic Mitchell
2006-03-21 14:22 ` Marc Chantreux
2006-03-21 15:33 ` Marc Chantreux
1 sibling, 1 reply; 10+ messages in thread
From: Dominic Mitchell @ 2006-03-21 13:51 UTC (permalink / raw)
To: Marc Chantreux; +Cc: zsh-users
On Tue, Mar 21, 2006 at 02:42:12PM +0100, Marc Chantreux wrote:
> Hi all,
>
> i'm always searching for shortest ways in zsh (coming from perl world).
>
> in perl :
>
> while ( <>) {
> chomp;
> my ( $a , $b ) = split /\t/;
> print "b=$b a=$a"
> }
>
> in zsh :
>
> while {read} {
> content=( ${(ps:\t:)REPLY} )
> a=$content[1]
> b=content[2]
> print "b=$b a=$a"
> }
>
> is there something more faster to initialize a and b (peraps closer than
> split) ?
Just use the read builtin:
echo foo bar | while read a b junk
do
echo "a=$a b=$b"
done
I've got a habit of reading any remaining fields into a variable called
"junk", just in case.
-Dom
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: kinda perl split ...
2006-03-21 13:51 ` Dominic Mitchell
@ 2006-03-21 14:22 ` Marc Chantreux
2006-03-21 14:54 ` Dominic Mitchell
0 siblings, 1 reply; 10+ messages in thread
From: Marc Chantreux @ 2006-03-21 14:22 UTC (permalink / raw)
To: Dominic Mitchell; +Cc: zsh-users
Dominic Mitchell wrote:
> echo foo bar | while read a b junk
> do
> echo "a=$a b=$b"
> done
so if the separator is ':', you should redefine IFS
oldIFS=$IFS
IFS=':'
echo foo bar | while read a b junk
do
echo "a=$a b=$b"
done
IFS=$oldIFS
oldIFS=
or with a subshell:
( IFS=':'
echo foo bar | while read a b junk
do
echo "a=$a b=$b"
done )
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: kinda perl split ...
2006-03-21 14:22 ` Marc Chantreux
@ 2006-03-21 14:54 ` Dominic Mitchell
0 siblings, 0 replies; 10+ messages in thread
From: Dominic Mitchell @ 2006-03-21 14:54 UTC (permalink / raw)
To: Marc Chantreux; +Cc: zsh-users
On Tue, Mar 21, 2006 at 03:22:29PM +0100, Marc Chantreux wrote:
> Dominic Mitchell wrote:
>
> > echo foo bar | while read a b junk
> > do
> > echo "a=$a b=$b"
> > done
>
> so if the separator is ':', you should redefine IFS
>
> oldIFS=$IFS
> IFS=':'
> echo foo bar | while read a b junk
> do
> echo "a=$a b=$b"
> done
> IFS=$oldIFS
> oldIFS=
>
> or with a subshell:
>
> ( IFS=':'
> echo foo bar | while read a b junk
> do
> echo "a=$a b=$b"
> done )
Or even simpler, just redefine the variable for that read command:
echo foo:bar | while IFS=: read a b junk
do
echo "a=$a b=$b"
done
-Dom
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: kinda perl split ...
2006-03-21 13:42 kinda perl split Marc Chantreux
2006-03-21 13:51 ` Dominic Mitchell
@ 2006-03-21 15:33 ` Marc Chantreux
2006-03-21 17:10 ` Bart Schaefer
1 sibling, 1 reply; 10+ messages in thread
From: Marc Chantreux @ 2006-03-21 15:33 UTC (permalink / raw)
Cc: zsh-users
hi again,
what about this perl code ?
my %user;
@user{qw( login passwd )} = split /:/;
i've tried something there... peraps it's a good tips :
http://khatar.phear.org/shell/zshEnum.html
(comments are in french but zsh workers can ignore them).
regards
mc
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: kinda perl split ...
2006-03-21 15:33 ` Marc Chantreux
@ 2006-03-21 17:10 ` Bart Schaefer
2006-03-22 13:12 ` Marc Chantreux
0 siblings, 1 reply; 10+ messages in thread
From: Bart Schaefer @ 2006-03-21 17:10 UTC (permalink / raw)
To: zsh-users
On Mar 21, 4:33pm, Marc Chantreux wrote:
}
} my %user;
} @user{qw( login passwd )} = split /:/;
You can still use "read".
local -A user
while IFS=: read 'user['${^=:-login passwd uid gid gecos home shell}']'
do
if (( ${user[uid]} > 100 )) { print $user[login] }
done < /etc/passwd
} http://khatar.phear.org/shell/zshEnum.html
A better way to write "enum":
enum() {
integer i=0
eval integer -g $^@='$[++i]'
}
Then you don't need "eval $(enum ...)" because enum does the assignments
for you.
You can also do it this way:
local -a user i; i=( login passwd uid gid gecos home shell )
while IFS=: read -A user
do
if (( ${user[$i[(i)uid]]} > 100 )) { print $user[$i[(i)login]] }
done < /etc/passwd
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2006-03-22 15:19 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-03-21 13:42 kinda perl split Marc Chantreux
2006-03-21 13:51 ` Dominic Mitchell
2006-03-21 14:22 ` Marc Chantreux
2006-03-21 14:54 ` Dominic Mitchell
2006-03-21 15:33 ` Marc Chantreux
2006-03-21 17:10 ` Bart Schaefer
2006-03-22 13:12 ` Marc Chantreux
2006-03-22 14:33 ` Bart Schaefer
2006-03-22 15:04 ` Marc Chantreux
2006-03-22 15:18 ` Marc Chantreux
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).