- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to find the longest argument in a list - ksh? ...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2003 07:58 AM
07-01-2003 07:58 AM
I am looking for the most efficient answer to this question:
How does one find the longest argument in a list?
Minimum code and speed count. I came up with this ksh snip:
list="abc123 abc1234 abc12345"
stdlen=0
for item in $list
do
len=$(echo $item | wc -c)
[ $len -gt $stdlen ] && stdlen=$len
done
echo $stdlen
This seems to report the real max-length + 1.
I'm certain that you gurus know a quicker/better way to do this. perl & awk suggestions welcome.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2003 08:03 AM
07-01-2003 08:03 AM
SolutionIn lieu of using 'wc' use the shell builtin :
len=${#item}
Hence your script becomes:
list="abc123 abc1234 abc12345"
stdlen=0
for item in $list
do
len=${#item}
[ $len -gt $stdlen ] && stdlen=$len
done
echo $stdlen
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2003 08:06 AM
07-01-2003 08:06 AM
Re: How to find the longest argument in a list - ksh? perl?
@list=qw/abc123 abc1234 abc12345/;
$stdlen=length($_) > $stdlen ? length($_) : $stdlen foreach @list;
print "$stdlen\n";
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2003 11:48 AM
07-01-2003 11:48 AM
Re: How to find the longest argument in a list - ksh? perl?
Where you calculate the length use this:
len=$#item
in $len you will have the size on $item
Caesar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2003 11:57 AM
07-01-2003 11:57 AM
Re: How to find the longest argument in a list - ksh? perl?
You seem to have a habit of providing *identical* answers as others have already posted, often *hours before*. Why is that?
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2003 12:55 PM
07-01-2003 12:55 PM
Re: How to find the longest argument in a list - ksh? perl?
If you want the argument itself, try this:
perl -e 'print @{[sort { length($b) <=> length($a) } @ARGV]}[0],"\n";'