How to grab last element of array from split with no temporaryvariables?
How to grab last element of array from split with no temporaryvariables?
am 12.08.2011 01:17:51 von Siegfried Heintze
This works! Is there a way to do it with less typing? How can I do it=0Awit=
hout creating a temporary variable "@p"?=0AThanks,=0Asiegfried
find /x=
yz -exec perl -e 'foreach(@ARGV){ my @p=3Dsplit "/"; rename $_,=0A"./$p[$#p=
].txt" } '
=0A
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: How to grab last element of array from split with no temporaryvariables?
am 12.08.2011 01:35:26 von Shawn H Corey
On 11/08/11 07:17 PM, siegfried@heintze.com wrote:
> This works! Is there a way to do it with less typing? How can I do it
> without creating a temporary variable "@p"?
> Thanks,
> siegfried
>
> find /xyz -exec perl -e 'foreach(@ARGV){ my @p=split "/"; rename $_,
> "./$p[$#p].txt" } '
>
>
>
>
find /xyz -exec perl -e 'foreach(@ARGV){ my @_=split "/"; rename $_,
"./$_[$#_].txt" } '
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
"Make something worthwhile." -- Dear Hunter
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: How to grab last element of array from split with no temporaryvariables?
am 12.08.2011 01:45:31 von Jim Gibson
On 8/11/11 Thu Aug 11, 2011 4:17 PM, "siegfried@heintze.com"
scribbled:
> This works! Is there a way to do it with less typing? How can I do it
> without creating a temporary variable "@p"?
> Thanks,
> siegfried
>
> find /xyz -exec perl -e 'foreach(@ARGV){ my @p=split "/"; rename $_,
> "./$p[$#p].txt" } '
my $file = (split('/',$_))[-1];
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: How to grab last element of array from split with no temporary variables?
am 12.08.2011 02:15:04 von Jim Bauer
On Thu, 11 Aug 2011 16:17:51 -0700, wrote:
> This works! Is there a way to do it with less typing? How can I do it
> without creating a temporary variable "@p"?
rename($_, sprintf("./%s.txt", (split '/')[-1]));
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: How to grab last element of array from split with no temporaryvariables?
am 12.08.2011 03:42:57 von jwkrahn
Shawn H Corey wrote:
> On 11/08/11 07:17 PM, siegfried@heintze.com wrote:
>> This works! Is there a way to do it with less typing? How can I do it
>> without creating a temporary variable "@p"?
>> Thanks,
>> siegfried
>>
>> find /xyz -exec perl -e 'foreach(@ARGV){ my @p=split "/"; rename $_,
>> "./$p[$#p].txt" } '
>
> find /xyz -exec perl -e 'foreach(@ARGV){ my @_=split "/"; rename $_,
> "./$_[$#_].txt" } '
split() uses @_ by default so you could just say:
find /xyz -exec perl -e 'for(@ARGV){ split "/"; rename $_,
"./$_[-1].txt" } '
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: How to grab last element of array from split with no temporaryvariables?
am 12.08.2011 07:01:33 von Peter Scott
On Thu, 11 Aug 2011 16:17:51 -0700, siegfried wrote:
> This works!=20
Really? I get "find: missing argument to `-exec'"
I think your command also renames directories. You want that?
> Is there a way to do it with less typing? How can I do it
> without creating a temporary variable "@p"? Thanks,
> siegfried
>=20
> find /xyz -exec perl -e 'foreach(@ARGV){ my @p=3Dsplit "/"; rename $_,
> "./$p[$#p].txt" } '
Try this:
find /xyz -type f -print0 | perl -F/ -0lane 'rename $_,"$F[-1].txt" or=20
warn $!'
That should give you a good excuse to read perlrun :-)
--=20
Peter Scott
http://www.perlmedic.com/ http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=3D0137001274
http://www.oreillyschool.com/courses/perl4/
--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: How to grab last element of array from split with no temporaryvariables?
am 12.08.2011 08:30:32 von jwkrahn
Peter Scott wrote:
> On Thu, 11 Aug 2011 16:17:51 -0700, siegfried wrote:
>> This works!
>
> Really? I get "find: missing argument to `-exec'"
>
> I think your command also renames directories. You want that?
>
>> Is there a way to do it with less typing? How can I do it
>> without creating a temporary variable "@p"? Thanks,
>> siegfried
>>
>> find /xyz -exec perl -e 'foreach(@ARGV){ my @p=split "/"; rename $_,
>> "./$p[$#p].txt" } '
>
> Try this:
>
> find /xyz -type f -print0 | perl -F/ -0lane 'rename $_,"$F[-1].txt" or
> warn $!'
That won't work as that reads the contents of the files and not the file
names.
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: How to grab last element of array from split with no temporary variables?
am 12.08.2011 18:07:02 von Brandon McCaig
On Thu, Aug 11, 2011 at 7:17 PM, wrote:
> find /xyz -exec perl -e 'foreach(@ARGV){ my @p=split "/"; rename $_,
> "./$p[$#p].txt" } '
If I'm reading this right then it looks like you're trying to
recursively move all files in /xyz into the current directory.
Probably don't need Perl for that.
find /xyz -type f -execdir /bin/echo mv '{}' "`pwd`/{}.txt" \;
Remove the /bin/echo to actually execute it, but you should make
absolutely sure it does the Right Thing(tm) first. I haven't actually
executed it myself so I'm not 100% sure it does the right thing, but
the output looks correct.
--
Brandon McCaig
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: How to grab last element of array from split with no temporaryvariables?
am 12.08.2011 19:52:25 von Rob Dixon
On 12/08/2011 00:17, siegfried@heintze.com wrote:
> This works! Is there a way to do it with less typing? How can I do it
> without creating a temporary variable "@p"?
> Thanks,
> siegfried
>
> find /xyz -exec perl -e 'foreach(@ARGV){ my @p=split "/"; rename $_,
> "./$p[$#p].txt" } '
find /xyz -exec perl -e '/([^\/]+)\z/ and rename $_,"$1.txt" for @ARGV';
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: How to grab last element of array from split with no temporaryvariables?
am 12.08.2011 19:58:01 von Rob Dixon
On 12/08/2011 18:52, Rob Dixon wrote:
> On 12/08/2011 00:17, siegfried@heintze.com wrote:
>> This works! Is there a way to do it with less typing? How can I do it
>> without creating a temporary variable "@p"?
>> Thanks,
>> siegfried
>>
>> find /xyz -exec perl -e 'foreach(@ARGV){ my @p=split "/"; rename $_,
>> "./$p[$#p].txt" } '
>
> find /xyz -exec perl -e '/([^\/]+)\z/ and rename $_,"$1.txt" for @ARGV';
More accurately:
find /xyz -exec perl -e '/([^\/]+)\z/ and rename $_,"./$1.txt" for @ARGV'
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: How to grab last element of array from split with no temporary variables?
am 12.08.2011 21:23:58 von merlyn
>>>>> "John" == "John W Krahn" writes:
John> split() uses @_ by default so you could just say:
That's deprecated though, if not already gone. (Looks gone in Perl
5.14.) It was a readily-admitted misfeature.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: How to grab last element of array from split with no temporaryvariables?
am 13.08.2011 00:45:43 von jwkrahn
Randal L. Schwartz wrote:
>>>>>> "John" == "John W Krahn" writes:
>
> John> split() uses @_ by default so you could just say:
>
> That's deprecated though, if not already gone. (Looks gone in Perl
> 5.14.) It was a readily-admitted misfeature.
Unless you're playing golf. :-)
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: How to grab last element of array from split with no temporaryvariables?
am 14.08.2011 20:09:38 von Peter Scott
On Thu, 11 Aug 2011 23:30:32 -0700, John W. Krahn wrote:
> Peter Scott wrote:
>> On Thu, 11 Aug 2011 16:17:51 -0700, siegfried wrote:
>>> Is there a way to do it with less typing? How can I do it without
>>> creating a temporary variable "@p"? Thanks, siegfried
>>>
>>> find /xyz -exec perl -e 'foreach(@ARGV){ my @p=3Dsplit "/"; rename $_=
,
>>> "./$p[$#p].txt" } '
>>
>> Try this:
>>
>> find /xyz -type f -print0 | perl -F/ -0lane 'rename $_,"$F[-1].txt" or
>> warn $!'
>=20
> That won't work as that reads the contents of the files and not the fil=
e
> names.
No, STDIN is the list of filenames; Perl's not taking that as @ARGV. =20
Observe:
$ ls -lR /tmp/xxx
/tmp/xxx:
total 4
-rw-rw-r-- 1 peter peter 0 Aug 14 11:04 a
drwxrwxr-x 2 peter peter 4096 Aug 14 11:04 yyy
/tmp/xxx/yyy:
total 0
-rw-rw-r-- 1 peter peter 0 Aug 14 11:04 b
$ pwd
/tmp/zzz
$ find /tmp/xxx -type f -print0 | perl -F/ -0lane 'rename $_,"$F[-1].txt"=
=20
or warn $!'
$ ls -l
total 0
-rw-rw-r-- 1 peter peter 0 Aug 14 11:04 a.txt
-rw-rw-r-- 1 peter peter 0 Aug 14 11:04 b.txt
$ ls -lR /tmp/xxx
/tmp/xxx:
total 4
drwxrwxr-x 2 peter peter 4096 Aug 14 11:05 yyy
/tmp/xxx/yyy:
total 0
--=20
Peter Scott
http://www.perlmedic.com/ http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=3D0137001274
http://www.oreillyschool.com/courses/perl4/
--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: How to grab last element of array from split with no temporary variables?
am 18.08.2011 03:58:29 von merlyn
>>>>> ""John" == "John W Krahn" writes:
>> That's deprecated though, if not already gone. (Looks gone in Perl
>> 5.14.) It was a readily-admitted misfeature.
John> Unless you're playing golf. :-)
I'd argue that Golf itself is a misfeature of Perl. More like a bug.
Golfers do NOT understand the DAMAGE they are doing to Perl's
perception outside the Perl community, and I wish they'd damn well
stop.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: How to grab last element of array from split with no temporary variables?
am 18.08.2011 05:06:20 von sono-io
On Aug 17, 2011, at 6:58 PM, Randal L. Schwartz wrote:
> Golfers do NOT understand the DAMAGE they are doing to Perl's
> perception outside the Perl community,
What is golf?
Marc
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: How to grab last element of array from split with no temporary variables?
am 18.08.2011 06:06:30 von timothy adigun
--0016e6da93d0de2b1c04aabfbcdf
Content-Type: text/plain; charset=ISO-8859-1
> Golfers do NOT understand the DAMAGE they are doing to Perl's
> perception outside the Perl community,
What is golf?
Hi Marc,
Perl "golf" is a game where one reduce the number of characters {key
"strokes"} used in a perl program, just like how golf players seek to hit
balls into series of holes using the fewest number of strokes.
Regards,
Timothy
--0016e6da93d0de2b1c04aabfbcdf--
Re: How to grab last element of array from split with no temporary variables?
am 18.08.2011 15:52:48 von Brandon McCaig
On Wed, Aug 17, 2011 at 9:58 PM, Randal L. Schwartz
wrote:
> Golfers do NOT understand the DAMAGE they are doing to Perl's
> perception outside the Perl community, and I wish they'd damn well
> stop.
In my experience, people outside of the Perl community perceive /any/
Perl as obfuscated anyway. ;) I've heard people say that there are too
many symbols for their comfort, even in regards to very clean programs
with no brevity hacks... I think people fear Perl more than they fear
C. :P That's probably what attracted me to Perl in the first place
though: the "elitism" and potential brevity. Perl is obviously much
more than that, but nevertheless I consider those features too. :)
--
Brandon McCaig
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/