Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Dividing a column by it's first number Post 302534335 by bartus11 on Monday 27th of June 2011 02:45:30 PM
Old 06-27-2011
Code:
#!/usr/bin/perl
open I,"$ARGV[0]";
chomp(@x=<I>);
for ($i=0;$i<=$#x;$i++){
  @y=split / +/,$x[$i];
  for ($j=0;$j<=$#y;$j++){
    push @{$c[$j]},$y[$j];
  }
}
for ($i=0;$i<=$#y;$i++){
  for $j (@{$c[$i]}){
    $d[$i]=$j if ($j!=0 && !$d[$i]);
  }
  @one=grep {$_ == 1} @{$c[$i]};
  if ($#one==$#{$c[$i]}){
    @{$c_out[$i]}=map {("0")} @{$c[$i]};
  } else {
    for ($j=0;$j<=$#{$c[$i]};$j++){
      $c_out[$i][$j]=$c[0][$j]*$c[$i][$j];
    }
  }
}
for ($i=0;$i<=$#x;$i++){
  for ($j=0;$j<=$#y;$j++){
    if ($j<2){
      print $c[$j][$i],"  ";
    } else {
      print $c_out[$j][$i],"  ";
    }
  }
  print "\n";
}

This User Gave Thanks to bartus11 For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

returning a column number

Hi all, i was doing a small program where if i was to be given the first 3 letters of any month i.e. in the form of Jan or Apr then it would return the column number where it finds a match. To do this i created a 12 element array of months with first 3 letters and if i echo'ed the contents of... (2 Replies)
Discussion started by: scriptingmani
2 Replies

2. UNIX for Dummies Questions & Answers

Grep by column number

I have a data file that is arranged like this: Marketing Ranjit Singh Eagles Dean Johnson FULL Marketing Ken Whillans Eagles Karen Thompson FULL Sales Peter RobertsonGolden TigersRich Gardener PART President Sandeep Jain Wimps Ken Whillans CONT... (7 Replies)
Discussion started by: hitman247m
7 Replies

3. Shell Programming and Scripting

Dividing by zero

Does anyone know how to include as a script maybe an "echo" warning that explains that if a user uses the second number "zero" when dividing, that the result will BE "zero." I need, example: 5/0 (second number) = 0, in script form. current script: echo "Enter a number" read num1 echo... (4 Replies)
Discussion started by: jefferj54
4 Replies

4. Shell Programming and Scripting

How to get the column number in awk?

Hi Guys, I have a question on how i can get the column number in a file and used it in awk. i have a file which it has these records inside it. ... (7 Replies)
Discussion started by: reignangel2003
7 Replies

5. Shell Programming and Scripting

Help with compare two column and print out column with smallest number

Input file : 5 20 500 2 20 41 41 0 23 1 Desired output : 5 2 20 0 1 By comparing column 1 and 2 in each line, I hope can print out the column with smallest number. I did try the following code, but it don't look good :( (2 Replies)
Discussion started by: perl_beginner
2 Replies

6. Shell Programming and Scripting

Dividing a column and submitting it as stdin

I currently have two programs written in C named "remove" and "calculate. When I call ./remove it removes some data from stdin. When I call ./calculate which takes in an argument and also data from stdin and calculates and returns a value. Currently writing a script that calls both of these... (1 Reply)
Discussion started by: kadanakanz
1 Replies

7. Shell Programming and Scripting

Split column data if the table has n number of column's

please write a shell script Table -------------------------- 1 2 3 a b c 3 4 5 c d e 7 8 9 f g h Output should be like this --------------- 1 2 3 3 4 5 7 8 9 a b c c d e f g h (1 Reply)
Discussion started by: Priti2277
1 Replies

8. Shell Programming and Scripting

Split column data if the table has n number of column's with some record

Split column data if the table has n number of column's with some record then how to split n number of colmn's line by line with records Table --------- Col1 col2 col3 col4 ....................col20 1 2 3 4 .................... 20 a b c d .................... v ... (11 Replies)
Discussion started by: Priti2277
11 Replies

9. Shell Programming and Scripting

Adding number in one column

Hello I have something like this a 1 b 1 c 1 d 1 e 1 This is inside 1.dat and this is what I am trying to get a 2 b 2 c 2 d 2 e 2 (4 Replies)
Discussion started by: jeo_fb
4 Replies

10. Shell Programming and Scripting

Dividing a column by it's last number

Hi! I have a file as below: 0.000145 5.82056e-08 0.000146 6.82088e-08 0.000150 7.42115e-08 0.000156 8.52142e-08 0.000176 8.82169e-08 0.000178 9.82197e-08 I'd like to divide the values of the second column by its last entry (here 9.82197e-08). I would be thankful if someone could... (6 Replies)
Discussion started by: Setareh12
6 Replies
Inline::Files(3)					User Contributed Perl Documentation					  Inline::Files(3)

NAME
Inline::Files - Multiple virtual files at the end of your code VERSION
This document describes version 0.68 of Inline::Files, released July 23, 2011. SYNOPSIS
use Inline::Files; my Code $here; # etc. # etc. # etc. __FOO__ This is a virtual file at the end of the data __BAR__ This is another virtual file __FOO__ This is yet another such file WARNING
It is possible that this module may overwrite the source code in files that use it. To protect yourself against this possibility, you are strongly advised to use the "-backup" option described in "Safety first". This module is still experimental. Regardless of whether you use "-backup" or not, by using this module you agree that the authors will b<under no circumstances> be responsible for any loss of data, code, time, money, or limbs, or for any other disadvantage incurred as a result of using Inline::Files. DESCRIPTION
Inline::Files generalizes the notion of the "__DATA__" marker and the associated "<DATA>" filehandle, to an arbitrary number of markers and associated filehandles. When you add the line: use Inline::Files; to a source file you can then specify an arbitrary number of distinct virtual files at the end of the code. Each such virtual file is marked by a line of the form: __SOME_SYMBOL_NAME_IN_UPPER_CASE__ The following text -- up to the next such marker -- is treated as a file, whose (pseudo-)name is available as an element of the package array @SOME_SYMBOL_NAME_IN_UPPER_CASE. The name of the first virtual file with this marker is also available as the package scalar $SOME_SYMBOL_NAME_IN_UPPER_CASE. The filehandle of the same name is magical -- just like "ARGV" -- in that it automatically opens itself when first read. Furthermore -- just like "ARGV" -- the filehandle re-opens itself to the next appropriate virtual file (by "shift"-ing the first element of @SOME_SYMBOL_NAME_IN_UPPER_CASE into $SOME_SYMBOL_NAME_IN_UPPER_CASE) whenever it reaches EOF. So, just as with "ARGV", you can treat all the virtual files associated with a single symbol either as a single, multi-part file: use Inline::Files; while (<FILE>) { print "$FILE: $_"; } __FILE__ File 1 here __FILE__ File 2 here __OTHER_FILE__ Other file 1 __FILE__ File 3 here or as a series of individual files: use Inline::Files; foreach $filename (@FILE) { open HANDLE, $filename; print "<<$filename>> "; while (<HANDLE>) { print; } } __FILE__ File 1 here __FILE__ File 2 here __OTHER_FILE__ Other file 1 __FILE__ File 3 here Note that these two examples completely ignore the lines: __OTHER_FILE__ Other file 1 which would be accessed via the "OTHER_FILE" filehandle. Unlike "<ARGV>"/@ARGV/$ARGV, Inline::Files also makes use of the hash associated with an inline file's symbol. That is, when you create an inline file with a marker "__WHATEVER__", the hash %WHATEVER will contain information about that file. That information is: $WHATEVER{file} The name of the disk file in which the inlined "__WHATEVER__" files were defined; $WHATEVER{line} The line (starting from 1) at which the current inline "__WHATEVER__" file being accessed by "<WHATEVER>" started. $WHATEVER{offset} The byte offset (starting from 0) at which the current inline "__WHATEVER__" file being accessed by "<WHATEVER>" started. $WHATEVER{writable} Whether the the current inline file being accessed by "<WHATEVER>" is opened for output. The hash and its elements are read-only and the entry values are only meaningful when the corresponding filehandle is open. Writable virtual files If the source file that uses Inline::Files is itself writable, then the virtual files it contains may also be opened for write access. For example, here is a very simple persistence mechanism: use Inline::Files; use Data::Dumper; open CACHE or die $!; # read access (uses $CACHE to locate file) eval join "", <CACHE>; close CACHE or die $!; print "$var was '$var' "; while (<>) { chomp; $var = $_; print "$var now '$var' "; } open CACHE, ">$CACHE" or die $!; # write access print CACHE Data::Dumper->Dump([$var],['var']); close CACHE or die $!; __CACHE__ $var = 'Original value'; Unlike "ARGV", if a virtual file is part of a writable file and is automagically opened, it is opened for full read/write access. So the above example, could be even simpler: use Inline::Files; use Data::Dumper; eval join "", <CACHE>; # Automagically opened print "$var was '$var' "; while (<>) { chomp; $var = $_; print "$var now '$var' "; } seek CACHE, 0, 0; print CACHE Data::Dumper->Dump([$var],['var']); __CACHE__ $var = 'Original value'; In either case, the original file is updated only at the end of execution, on an explicit "close" of the virtual file's handle, or when "Inline::Files::Virtual::vf_save" is explicitly called. Creating new Inline files on the fly. You can also open up new Inline output files at run time. Simply use the open function with a valid new Inline file handle name and no file name. Like this: use Inline::Files; open IFILE, '>'; print IFILE "This line will be placed into a new Inline file "; print IFILE "which is marked by '__IFILE__' "; Safety first Because Inline::Files handles are often read-write, it's possible to accidentally nuke your hard-won data. But Inline::Files can save you from yourself. If Inline::Files is loaded with the "-backup" option: use Inline::Files -backup; then the source file that uses it is backed up before the inline files are extracted. The backup file is the name of the source file with the suffix ".bak" appended. You can also specify a different name for the backup file, by associating that name with the "-backup" flag: use Inline::Files -backup => '/tmp/sauve_qui_peut'; SEE ALSO
The Inline::Files::Virtual module The Filter::Util::Call module BUGS ADDED BY Alberto Simoes (ambs@cpan.org) UNWITTING PAWN OF AN AUTHOR
Damian Conway (damian@conway.org) EVIL MASTERMIND BEHIND IT ALL
Brian Ingerson (INGY@cpan.org) COPYRIGHT
Copyright (c) 2001-2009. Damian Conway. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://www.perl.com/perl/misc/Artistic.html perl v5.16.3 2011-07-23 Inline::Files(3)
All times are GMT -4. The time now is 05:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy