zsh-workers
 help / color / mirror / code / Atom feed
* Getting rid of temporaries...
@ 2003-09-10 20:34 DervishD
  2003-09-10 22:38 ` Danek Duvall
  0 siblings, 1 reply; 15+ messages in thread
From: DervishD @ 2003-09-10 20:34 UTC (permalink / raw)
  To: Zsh

    Hi all :))

    I want to do something with a list of files, and currently I need
a temporary variable (an array). I can do the same using find, I
suppose, but I want to do it in Zsh.

    I have a directory structure containing files whose names match
the expression '*.??.jpg'. There are some images numbered with a two
digit number. There are only one-directory depth. So, with this:

    print -l /directory/*/*

    I can print all filenames. No problem with this. The problem is
that I want to strip the number and the '.jpg' suffix from the names.
If I have the filename in a parameter, I can do:

    print -l ${parameter%.??.jpg}

    and that will do. The problem is that I want to do it in all
filenames, so I must say something like:

    typeset -a array=(/directory/*/*)
    print -l ${array%.??.jpg} | uniq

    The 'uniq' is because without the number and the extension, there
are lots of duplicates (that is what the number is for ;)), and I
want to get rid of them.

    Now the question: how can I do this without using the temporary
parameter 'array' and, if possible, without 'uniq'. I think that some
arrays can be configured to store unique copies of elements, and in
fact the (t) flag (expansion flag?) can say 'unique' for those
arrays, but I don't have damned idea of how can I declare such
arrays.

    My excuses if I'm doing lots of questions (even trivial of weird
ones), but I'm on the way of seriously learning Zsh, and I'm starting
with the user friendly guide ;)) Thanks a lot for your collaboration
and help, truly :)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Getting rid of temporaries...
  2003-09-10 20:34 Getting rid of temporaries DervishD
@ 2003-09-10 22:38 ` Danek Duvall
  2003-09-11  1:06   ` Bart Schaefer
  2003-09-11  9:30   ` DervishD
  0 siblings, 2 replies; 15+ messages in thread
From: Danek Duvall @ 2003-09-10 22:38 UTC (permalink / raw)
  To: Zsh

I'm not sure if it's possible to get rid of the temporary *and* the uniq
in a simple fashion, but:

    typeset -U array
    array=( /directory/*/* )
    array=( ${^array%.<00-99>.jpg} )
    print -l $array

will do what you want.  The "typeset -U" makes $array discard duplicate
elements, but that requires the reassignment once the dups are gone,
though it might be a nice candidate for yet another parameter expansion
flag.  :)

The "^" turns on rcexpandparam for the expansion of $array, which means
that, as an array, each element is modified.  And I turned your "??"
into "<00-99>", which explicitly only matches two-digit numbers (you
could also use "<->" to match all numbers).

Danek


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Getting rid of temporaries...
  2003-09-10 22:38 ` Danek Duvall
@ 2003-09-11  1:06   ` Bart Schaefer
  2003-09-11  1:46     ` Danek Duvall
                       ` (2 more replies)
  2003-09-11  9:30   ` DervishD
  1 sibling, 3 replies; 15+ messages in thread
From: Bart Schaefer @ 2003-09-11  1:06 UTC (permalink / raw)
  To: Zsh

On Sep 10, 10:34pm, DervishD wrote:
} Subject: Getting rid of temporaries...
}
}     I have a directory structure containing files whose names match
} the expression '*.??.jpg'.
} [...] I want to strip the number and the '.jpg' suffix from the names.

}     Now the question: how can I do this without using the temporary
} parameter 'array' and, if possible, without 'uniq'.

If you have the array, it's easy to do without uniq.  To do it without
the array begins to creep into the realm of "so difficult it's not
worth bothering."

The 'e' globbing flag gets you most of the way:

    print -l *.??.jpg(e['REPLY=${REPLY%.??.jpg}'])

Of course, that uses the magic temporary $REPLY variable, so it hasn't
really eliminated temporaries.

On Sep 10,  3:38pm, Danek Duvall wrote:
}
}     typeset -U array
}     array=( /directory/*/* )
}     array=( ${^array%.<00-99>.jpg} )
}     print -l $array

This suggestion is on the right track, but it's not equivalent to Raul's
original one, because your first assignment may match names that do not
match *.<00-99>.jpg, which won't be modified by the second assignment.

} will do what you want.  The "typeset -U" makes $array discard duplicate
} elements, but that requires the reassignment once the dups are gone

I'm not sure what you mean by "requires the reassignment once the dups
are gone".  You have to strip the suffixes before zsh can tell what the
duplicates are.

} though it might be a nice candidate for yet another parameter expansion
} flag.  :)

You mean like ${(u)...}, which is in 4.1.1-dev-* ...
 
} The "^" turns on rcexpandparam for the expansion of $array, which means
} that, as an array, each element is modified.

That's not what rcexpandparam means.  It means that, if the expansion is
concatenated with other strings before and/or behind, then every element
is individually so concatenated.  You don't have any concatenation going
on here, so the caret isn't needed.

So we end up with, perhaps:

    typeset -U array
    array=( *.<00-99>.jpg(e['REPLY=${REPLY%.??.jpg}']) )
    print -l $array


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Getting rid of temporaries...
  2003-09-11  1:06   ` Bart Schaefer
@ 2003-09-11  1:46     ` Danek Duvall
  2003-09-11  6:29     ` Oliver Kiddle
  2003-09-11  9:36     ` DervishD
  2 siblings, 0 replies; 15+ messages in thread
From: Danek Duvall @ 2003-09-11  1:46 UTC (permalink / raw)
  To: zsh-workers

On Thu, Sep 11, 2003 at 01:06:23AM +0000, Bart Schaefer wrote:

> This suggestion is on the right track, but it's not equivalent to Raul's
> original one, because your first assignment may match names that do not
> match *.<00-99>.jpg, which won't be modified by the second assignment.

Neither does his original; he was selecting all files, too.  But yes,
you'd probably want to use a more specific globbing pattern.

> } will do what you want.  The "typeset -U" makes $array discard duplicate
> } elements, but that requires the reassignment once the dups are gone
> 
> I'm not sure what you mean by "requires the reassignment once the dups
> are gone".  You have to strip the suffixes before zsh can tell what the
> duplicates are.

That's what I meant; I was concentrating on the two-step process and
muddled the rest of it.

> } The "^" turns on rcexpandparam for the expansion of $array, which means
> } that, as an array, each element is modified.
> 
> That's not what rcexpandparam means.

Yup.  It's gotten stuck in my mind that it's needed for situations other
than just concatenation, including anything that needs to operate on
every array element.  That's obviously wrong.

> So we end up with, perhaps:
> 
>     typeset -U array
>     array=( *.<00-99>.jpg(e['REPLY=${REPLY%.??.jpg}']) )
>     print -l $array

Or

    array=( *.<00-99>.jpg(e['REPLY=${REPLY%.??.jpg}']) )
    print -l ${(u)array}

which was more what I was hoping for.

There's still no way of expanding a glob inside ${}, short of ${$(echo *)},
is there?

Danek


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Getting rid of temporaries...
  2003-09-11  1:06   ` Bart Schaefer
  2003-09-11  1:46     ` Danek Duvall
@ 2003-09-11  6:29     ` Oliver Kiddle
  2003-09-11  8:56       ` Bart Schaefer
  2003-09-11  9:39       ` DervishD
  2003-09-11  9:36     ` DervishD
  2 siblings, 2 replies; 15+ messages in thread
From: Oliver Kiddle @ 2003-09-11  6:29 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh

Bart wrote:
> 
> If you have the array, it's easy to do without uniq.  To do it without
> the array begins to creep into the realm of "so difficult it's not
> worth bothering."
> 
> The 'e' globbing flag gets you most of the way:
> 
>     print -l *.??.jpg(e['REPLY=${REPLY%.??.jpg}'])

Isn't that just the same as:

      print -l *.??.jpg(:r:r)

Can do the unique with the `e' globbing flag but not without
temporaries or eval.

Oliver


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Getting rid of temporaries...
  2003-09-11  6:29     ` Oliver Kiddle
@ 2003-09-11  8:56       ` Bart Schaefer
  2003-09-11  9:40         ` DervishD
  2003-09-11  9:43         ` DervishD
  2003-09-11  9:39       ` DervishD
  1 sibling, 2 replies; 15+ messages in thread
From: Bart Schaefer @ 2003-09-11  8:56 UTC (permalink / raw)
  To: zsh-workers

On Sep 10,  6:46pm, Danek Duvall wrote:
}
} There's still no way of expanding a glob inside ${}, short of ${$(echo *)},
} is there?

No, there's not, because filename generation is always done last.

On Sep 11,  8:29am, Oliver Kiddle wrote:
} Subject: Re: Getting rid of temporaries...
}
} >     print -l *.??.jpg(e['REPLY=${REPLY%.??.jpg}'])
} 
} Isn't that just the same as:
} 
}       print -l *.??.jpg(:r:r)

Yes, it is, but it's harder to generalize the latter to an arbitrary
name rewrite.  What if there weren't a dot before the number part?


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Getting rid of temporaries...
  2003-09-10 22:38 ` Danek Duvall
  2003-09-11  1:06   ` Bart Schaefer
@ 2003-09-11  9:30   ` DervishD
  1 sibling, 0 replies; 15+ messages in thread
From: DervishD @ 2003-09-11  9:30 UTC (permalink / raw)
  To: Danek Duvall, Zsh

    Hi Danek :)

 * Danek Duvall <duvall@emufarm.org> dixit:
> The "typeset -U" makes $array discard duplicate elements

    That's what I was looking for :))) I didn't find it in the
manual. I think I need more sleep. Or more caffeine. Or both O:)

> The "^" turns on rcexpandparam for the expansion of $array, which means
> that, as an array, each element is modified.

    I have rcexpandparam enabled by default, but anyway I see no use
of it here, since (AFAIK), that option is for cases like FOO${array}BAR
expanding to FOOelement1BAR FOOelement2BAR instead of the more common
FOOelement1 element2BAR.

> And I turned your "??" into "<00-99>", which explicitly only
> matches two-digit numbers (you could also use "<->" to match all
> numbers).

    Well, I didn't care so much about the globbing because I'm pretty
sure about the filenames, but yours is better.

    Thanks a lot!

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Getting rid of temporaries...
  2003-09-11  1:06   ` Bart Schaefer
  2003-09-11  1:46     ` Danek Duvall
  2003-09-11  6:29     ` Oliver Kiddle
@ 2003-09-11  9:36     ` DervishD
  2 siblings, 0 replies; 15+ messages in thread
From: DervishD @ 2003-09-11  9:36 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh

    Hi Bart :)

 * Bart Schaefer <schaefer@brasslantern.com> dixit:
> }     Now the question: how can I do this without using the temporary
> } parameter 'array' and, if possible, without 'uniq'.
> If you have the array, it's easy to do without uniq.

    Yes, with typeset -U, I just learned it a minute ago O:))

> The 'e' globbing flag gets you most of the way:

    I tried, but didn't get any result.
 
>     print -l *.??.jpg(e['REPLY=${REPLY%.??.jpg}'])

    I didn't write anything so elaborated O:). Thanks for the line :)
But this doesn't do the 'uniqueness', so I still depend on the array
(typesetted with -U) or 'uniq'. No problem about that.
 
> Of course, that uses the magic temporary $REPLY variable, so it hasn't
> really eliminated temporaries.

    No problem, because I want to get rid of the array so I could put
this in an alias instead of a function. Just for learning.
 
> }     array=( /directory/*/* )
> This suggestion is on the right track, but it's not equivalent to Raul's
> original one, because your first assignment may match names that do not
> match *.<00-99>.jpg, which won't be modified by the second assignment.

    Mine did, too. I'm sure that all filenames are of the form
name.digitdigit.jpg, so I carelessly matched all filenames.

> } though it might be a nice candidate for yet another parameter expansion
> } flag.  :)
> You mean like ${(u)...}, which is in 4.1.1-dev-* ...

    Oh, yes, I knew that I read it somewhere in this list. But I have
4.0.6 and that doesn't work, of course. Thanks for pointing :)

>     typeset -U array
>     array=( *.<00-99>.jpg(e['REPLY=${REPLY%.??.jpg}']) )
>     print -l $array

    Well, this is pretty compact, too :)) Thanks a lot :) I've
learned to use the (e) flag!

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Getting rid of temporaries...
  2003-09-11  6:29     ` Oliver Kiddle
  2003-09-11  8:56       ` Bart Schaefer
@ 2003-09-11  9:39       ` DervishD
  2003-09-12  9:27         ` Oliver Kiddle
  1 sibling, 1 reply; 15+ messages in thread
From: DervishD @ 2003-09-11  9:39 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: Bart Schaefer, Zsh

    Hi Oliver :)

 * Oliver Kiddle <okiddle@yahoo.co.uk> dixit:
> >     print -l *.??.jpg(e['REPLY=${REPLY%.??.jpg}'])
> Isn't that just the same as:
>       print -l *.??.jpg(:r:r)

    Yes!! I'm not used with the 'r' flag, and I didn't think of it.
But yes, removing both suffixes at one time is just the same. In
fact, both the .jpg and the two digit number are just suffixes for the
image name.
 
> Can do the unique with the `e' globbing flag but not without
> temporaries or eval.

    How can I do using eval? Assume that I don't have the (u) flag...

    Thanks !

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Getting rid of temporaries...
  2003-09-11  8:56       ` Bart Schaefer
@ 2003-09-11  9:40         ` DervishD
  2003-09-11  9:43         ` DervishD
  1 sibling, 0 replies; 15+ messages in thread
From: DervishD @ 2003-09-11  9:40 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-workers

    Hi Bart :)

 * Bart Schaefer <schaefer@brasslantern.com> dixit:
> } >     print -l *.??.jpg(e['REPLY=${REPLY%.??.jpg}'])
> } Isn't that just the same as:
> }       print -l *.??.jpg(:r:r)
> Yes, it is, but it's harder to generalize the latter to an arbitrary
> name rewrite.  What if there weren't a dot before the number part?

    In this case it's not an issue, since the filenames will always
have that name. The only change may be the number of digits, no more,
no less. There will always be two suffixes: the image type and the
order number.

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Getting rid of temporaries...
  2003-09-11  8:56       ` Bart Schaefer
  2003-09-11  9:40         ` DervishD
@ 2003-09-11  9:43         ` DervishD
  1 sibling, 0 replies; 15+ messages in thread
From: DervishD @ 2003-09-11  9:43 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-workers

    Hi Bart :)

 * Bart Schaefer <schaefer@brasslantern.com> dixit:
> } >     print -l *.??.jpg(e['REPLY=${REPLY%.??.jpg}'])
> } Isn't that just the same as:
> }       print -l *.??.jpg(:r:r)
> Yes, it is, but it's harder to generalize the latter to an arbitrary
> name rewrite.  What if there weren't a dot before the number part?

    I almost forgot... Since some names may have *three* digits in
the number part in the future, is better for me to do :r:r instead of
pattern matching.

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Getting rid of temporaries...
  2003-09-11  9:39       ` DervishD
@ 2003-09-12  9:27         ` Oliver Kiddle
  2003-09-12 18:39           ` Bart Schaefer
  2003-09-13 17:12           ` DervishD
  0 siblings, 2 replies; 15+ messages in thread
From: Oliver Kiddle @ 2003-09-12  9:27 UTC (permalink / raw)
  To: DervishD, Zsh

DervishD wrote:

> > Can do the unique with the `e' globbing flag but not without
> > temporaries or eval.
> 
>     How can I do using eval? Assume that I don't have the (u) flag...

Wasn't as easy as I thought. Thought I could do something like:

guniq() { eval "[[ -z ${~REPLY:r:r}.<${~REPLY:r:e}->.jpg(N[2]) ]]" }
print -l *.??.jpg(e:guniq:)

But eval insists on quoting the third word in what it runs so it does
  [[ -z 'foo.<28->.jpg(N[2])' ]]
which is no use.

The guniq() function there isn't necessary, it just makes quoting
easier.

You can do this though:

guniq() { [[ -z "$2" ]] }
print -l *.??.jpg(e['eval "guniq ${REPLY:r:r}.<${REPLY:r:e}->.jpg"']:r:r)

This solution needs the separate function and is in effect using $@ as
a temporary array though.

Oliver


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Getting rid of temporaries...
  2003-09-12  9:27         ` Oliver Kiddle
@ 2003-09-12 18:39           ` Bart Schaefer
  2003-09-13 17:13             ` DervishD
  2003-09-13 17:12           ` DervishD
  1 sibling, 1 reply; 15+ messages in thread
From: Bart Schaefer @ 2003-09-12 18:39 UTC (permalink / raw)
  To: Zsh

On Sep 12, 11:27am, Oliver Kiddle wrote:
}
} Wasn't as easy as I thought. Thought I could do something like:
} 
} guniq() { eval "[[ -z ${~REPLY:r:r}.<${~REPLY:r:e}->.jpg(N[2]) ]]" }
} print -l *.??.jpg(e:guniq:)
} 
} But eval insists on quoting the third word in what it runs so it does
}   [[ -z 'foo.<28->.jpg(N[2])' ]]
} which is no use.

It's not eval that quotes that, it's [[ ]].  The argument to -z et al.
is never expanded as a glob pattern.

If you use [ -z ... ] instead, you'll get better results.  Well, except
in 4.0.6, where you'll get a core dump, but that seems to be fixed in
4.0.7 and 4.1.1.

So without the function, it's

print -l *.??.jpg(e@'eval "[ -z ${~REPLY:r:r}.<${~REPLY:r:e}->.jpg(N[2]) ]"'@)

where @ is chosen as a character that appears nowhere in the expression.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Getting rid of temporaries...
  2003-09-12  9:27         ` Oliver Kiddle
  2003-09-12 18:39           ` Bart Schaefer
@ 2003-09-13 17:12           ` DervishD
  1 sibling, 0 replies; 15+ messages in thread
From: DervishD @ 2003-09-13 17:12 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: Zsh

    Hi Oliver :)

 * Oliver Kiddle <okiddle@yahoo.co.uk> dixit:
> > > Can do the unique with the `e' globbing flag but not without
> > > temporaries or eval.
> >     How can I do using eval? Assume that I don't have the (u) flag...
> Wasn't as easy as I thought. Thought I could do something like:

    I can see for the expression. Thanks for the effort :)))
 
    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Getting rid of temporaries...
  2003-09-12 18:39           ` Bart Schaefer
@ 2003-09-13 17:13             ` DervishD
  0 siblings, 0 replies; 15+ messages in thread
From: DervishD @ 2003-09-13 17:13 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh

    Hi Bart :)

 * Bart Schaefer <schaefer@brasslantern.com> dixit:
> }   [[ -z 'foo.<28->.jpg(N[2])' ]]
> } which is no use.
> It's not eval that quotes that, it's [[ ]].

    Oh :(((

> So without the function, it's
> print -l *.??.jpg(e@'eval "[ -z ${~REPLY:r:r}.<${~REPLY:r:e}->.jpg(N[2]) ]"'@)
> where @ is chosen as a character that appears nowhere in the expression.

    Thanks a lot for the expressión :))))

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2003-09-13 17:10 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-10 20:34 Getting rid of temporaries DervishD
2003-09-10 22:38 ` Danek Duvall
2003-09-11  1:06   ` Bart Schaefer
2003-09-11  1:46     ` Danek Duvall
2003-09-11  6:29     ` Oliver Kiddle
2003-09-11  8:56       ` Bart Schaefer
2003-09-11  9:40         ` DervishD
2003-09-11  9:43         ` DervishD
2003-09-11  9:39       ` DervishD
2003-09-12  9:27         ` Oliver Kiddle
2003-09-12 18:39           ` Bart Schaefer
2003-09-13 17:13             ` DervishD
2003-09-13 17:12           ` DervishD
2003-09-11  9:36     ` DervishD
2003-09-11  9:30   ` DervishD

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).