Sponsored Content
Full Discussion: Highest value matrix parsing
Top Forums Shell Programming and Scripting Highest value matrix parsing Post 302952941 by featheredfrog on Monday 24th of August 2015 12:44:51 PM
Old 08-24-2015
The shell fanatic is back. Somebody should time these examples (and maybe one using perl, also):
Code:
$ cat foodata
X a_2 a_3 s_4 t_6
b 0 0.9 0.004 0
c 0 0 1 0
d 0 0.98 0 0
e 0.0023 0.96 0 0.0034

$ ./foo.sh <foodata
b: a_3 0.9 (2)
c: s_4 1 (3)
d: a_3 0.98 (2)
e: a_3 0.96 (2)

$ cat foo.sh
#!/bin/bash
read header
columns=($header)
while read data; do
    line=($data)
    max=1 # index of first element
    for (( i=2 ; $i < ${#line[@]}; i++ )) ; do 
    if (( "$(echo ${line[$i]} '>' ${line[$max]} | bc -l)" == "1" )); then max=$i; fi
    done
  echo ${line[0]}: ${columns[$max]} ${line[$max]} "($max)"
done

Notes:
  1. I made a change to the first line of the datafile, so the headings lined up.
  2. Remove the last "($max)" if you don't need the index of the largest element
This is a fun exercise. Anyone want one in, like, SNOBOL?

---------- Post updated at 12:44 PM ---------- Previous update was at 12:30 PM ----------

I have 'way too much free time.
Code:
$ ./foo.pl <foodata
b    a_3    0.9
c    s_4    1
d    a_3    0.98
e    a_3    0.96

$ cat foo.pl
#!/usr/bin/perl -w
use strict;
my $debug=1;
my @headings=split ' ',<>;
while (<>) {
    chomp;
    my @line=split ' ',$_;
    my $max=1;
    foreach my $index ( 2 .. $#line ) { $max=$index if ($line[$max] < $line [$index]); }
    print $line[0]."\t".$headings[$max]."\t".$line[$max]."\n";
}

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sort with highest wc

Hi :) I'm a unix beginner and i've recently got an assignment to write up a script to print the most common IP address that made requests from a webserver. I'm really lost in this one...and if someone could pls tell me where to start i'll be really greatful ! thanx (1 Reply)
Discussion started by: ymf
1 Replies

2. Shell Programming and Scripting

Perl parsing compared to Ksh parsing

#! /usr/local/bin/perl -w $ip = "$ARGV"; $rw = "$ARGV"; $snmpg = "/usr/local/bin/snmpbulkget -v2c -Cn1 -Cn2 -Os -c $rw"; $snmpw = "/usr/local/bin/snmpwalk -Os -c $rw"; $syst=`$snmpg $ip system sysName sysObjectID`; sysDescr.0 = STRING: Cisco Internetwork Operating System Software... (1 Reply)
Discussion started by: popeye
1 Replies

3. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies

4. Shell Programming and Scripting

diagonal matrix to square matrix

Hello, all! I am struggling with a short script to read a diagonal matrix for later retrieval. 1.000 0.234 0.435 0.123 0.012 0.102 0.325 0.412 0.087 0.098 1.000 0.111 0.412 0.115 0.058 0.091 0.190 0.045 0.058 1.000 0.205 0.542 0.335 0.054 0.117 0.203 0.125 1.000 0.587 0.159 0.357... (11 Replies)
Discussion started by: yifangt
11 Replies

5. Ubuntu

How to convert full data matrix to linearised left data matrix?

Hi all, Is there a way to convert full data matrix to linearised left data matrix? e.g full data matrix Bh1 Bh2 Bh3 Bh4 Bh5 Bh6 Bh7 Bh1 0 0.241058 0.236129 0.244397 0.237479 0.240767 0.245245 Bh2 0.241058 0 0.240594 0.241931 0.241975 ... (8 Replies)
Discussion started by: evoll
8 Replies

6. Shell Programming and Scripting

awk? adjacency matrix to adjacency list / correlation matrix to list

Hi everyone I am very new at awk but think that that might be the best strategy for this. I have a matrix very similar to a correlation matrix and in practical terms I need to convert it into a list containing the values from the matrix (one value per line) with the first field of the line (row... (5 Replies)
Discussion started by: stonemonkey
5 Replies

7. Shell Programming and Scripting

Printing highest value from a list

Hi all, I'm trying to get the item with the maximum value, and was wondering if someone can help me with it. Heres my input file: apples 15 books 15 books 17 pens 12 pens 15 umbrella 13Here's what my output file should look like: apples 15 books 17 pens 15 umbrella 13 Can... (2 Replies)
Discussion started by: r4v3n
2 Replies

8. Programming

Matrix parsing help !

Hello every body ! I'm a new in this forum and beginner in Perl scripting and I have some problems :(:(:(! I have a big file like that : ID1 ID2 Identity chromosome07_194379 chromosome01_168057 0.975 chromosome01_100293 chromosome01_168057 ... (23 Replies)
Discussion started by: mchimich
23 Replies

9. Shell Programming and Scripting

Script read highest value

Hi All, Can someone help steer me to the simplest approach to write a script that reads the second column of a csv file and returns the highest value. example file: DATE,QUANTITY Mon Apr 30 01:56:00 2012,42 Mon Apr 30 02:24:00 2012,72 Mon Apr 30 02:56:00 2012,102 Mon Apr 30 02:59:00... (3 Replies)
Discussion started by: dekker74
3 Replies

10. Shell Programming and Scripting

Parsing a subset of data from a large matrix

I do have a large matrix of the following format and it is tab delimited ch-ab1-20 ch-bb2-23 ch-ab1-34 ch-ab1-24 er-cc1-45 bv-cc1-78 ch-ab1-20 0 2 3 4 5 6 ch-bb2-23 3 0 5 ... (6 Replies)
Discussion started by: Kanja
6 Replies
Tie::RefHash(3perl)					 Perl Programmers Reference Guide				       Tie::RefHash(3perl)

NAME
Tie::RefHash - use references as hash keys SYNOPSIS
require 5.004; use Tie::RefHash; tie HASHVARIABLE, 'Tie::RefHash', LIST; tie HASHVARIABLE, 'Tie::RefHash::Nestable', LIST; untie HASHVARIABLE; DESCRIPTION
This module provides the ability to use references as hash keys if you first "tie" the hash variable to this module. Normally, only the keys of the tied hash itself are preserved as references; to use references as keys in hashes-of-hashes, use Tie::RefHash::Nestable, included as part of Tie::RefHash. It is implemented using the standard perl TIEHASH interface. Please see the "tie" entry in perlfunc(1) and perltie(1) for more information. The Nestable version works by looking for hash references being stored and converting them to tied hashes so that they too can have references as keys. This will happen without warning whenever you store a reference to one of your own hashes in the tied hash. EXAMPLE
use Tie::RefHash; tie %h, 'Tie::RefHash'; $a = []; $b = {}; $c = *main; $d = "gunk"; $e = sub { 'foo' }; %h = ($a => 1, $b => 2, $c => 3, $d => 4, $e => 5); $a->[0] = 'foo'; $b->{foo} = 'bar'; for (keys %h) { print ref($_), " "; } tie %h, 'Tie::RefHash::Nestable'; $h{$a}->{$b} = 1; for (keys %h, keys %{$h{$a}}) { print ref($_), " "; } THREAD SUPPORT
Tie::RefHash fully supports threading using the "CLONE" method. STORABLE SUPPORT
Storable hooks are provided for semantically correct serialization and cloning of tied refhashes. RELIC SUPPORT
This version of Tie::RefHash seems to no longer work with 5.004. This has not been throughly investigated. Patches welcome ;-) LICENSE
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself MAINTAINER
Yuval Kogman <nothingmuch@woobling.org> AUTHOR
Gurusamy Sarathy gsar@activestate.com 'Nestable' by Ed Avis ed@membled.com SEE ALSO
perl(1), perlfunc(1), perltie(1) perl v5.14.2 2011-09-19 Tie::RefHash(3perl)
All times are GMT -4. The time now is 08:23 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy