The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl -write values in a file to @array in perl meghana Shell Programming and Scripting 12 07-17-2008 01:38 PM
PERL array spacing Jamison Shell Programming and Scripting 3 07-17-2008 01:10 AM
split to array in perl jaganadh Shell Programming and Scripting 3 07-06-2007 02:29 AM
hash,array and perl new2ss Shell Programming and Scripting 3 05-23-2007 08:30 AM
appending to perl array vivekshankar UNIX for Dummies Questions & Answers 7 06-03-2006 07:33 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 02-04-2008
Registered User
 

Join Date: Feb 2008
Posts: 56
how to get last value in an array in perl

Hi,

I have a set of file names say:

file1
file2
file3
..
filen

I may just do a ls -l or however it works to read this list into an array...

I need to pick the last file name that is "filen" from the list...

your help is greatly appreciated.....hope im clear on my question...
Reply With Quote
Forum Sponsor
  #2  
Old 02-04-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,913
Code:
zsh-4.3.4% ls
file1  file2  file3  file4  file5
zsh-4.3.4% perl -le'print $ARGV[-1]' file*
file5
Reply With Quote
  #3  
Old 02-04-2008
Registered User
 

Join Date: Nov 2007
Location: Belgium & France
Posts: 70
Code:
ls file* | sort | tail -1
Reply With Quote
  #4  
Old 02-04-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,913
Quote:
Originally Posted by V3l0 View Post
Code:
ls file* | sort | tail -1
Or

Code:
$ ls
file1  file2  file3  file4  file5
$ set -- file*;echo $_
file5
P.S. For some shells it should be:

Code:
set -- file*
echo $_

Last edited by radoulov; 02-04-2008 at 09:03 AM. Reason: corrected
Reply With Quote
  #5  
Old 02-04-2008
Registered User
 

Join Date: Feb 2008
Posts: 56
wow.. thanks for the really quick replies.. im doing this using a script tmp.pl ..

$ ls
file1 file2 file3 file4 file5
$ set -- file*;echo $_
file5

can u please tell me how to write this in a script??? thanks again
Reply With Quote
  #6  
Old 02-04-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,913
If it's a perl script, use perl:

Code:
zsh-4.3.4% ls
file1  file2  file3  file4  file5  temp.pl
zsh-4.3.4% cat temp.pl 
#!/usr/bin/perl 
use warnings;

@files = <file*>;
print "$files[-1]\n"; 
zsh-4.3.4% ./temp.pl 
file5
Reply With Quote
  #7  
Old 02-04-2008
Registered User
 

Join Date: Feb 2008
Posts: 56
i have got a solution ... its little different but it works.....

my @files = glob("file*");
my $last_file_name = pop(@files);

thanks for ur quick replies again........
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:24 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0