I am newbie with Perl. I have an assignment to transpose rows into columns of a huge data.
customers goods transportation
---------- ----- --------------
A, B, C, D rice truck
E, G, D corn train
............. ..... .........
T, H, K, M, N wheat air cargo
And I would like to have an output look like:
customers goods transportation
---------- ----- --------------
A rice truck
B rice truck
C rice truck
D rice truck
............. ..... .........
N wheat air cargo
Could anyone help. Thank you very much.
Most probably you have come across the map function, study how it works and how you construct rows or columns and you will get it, good luck! :)
Thank you all. After few hours trying I could figure out how to do my assignment.
It needs some simple steps of manually intervention for the input and output,
i.e. add an , at the end of the first column of the input and remove \ in
the second column of the output using Excel.
It is time to submit the output. I appreciate if someone has a better Perl
code to solve it.
#!/usr/bin/perl
my #records;
while (<DATA>) {
chomp;
my #columns = split ", ", $_;
push #records, \#columns;
}
foreach my $record (#records) {
foreach my $column (#{$record}) {
if (\$column != \$$record[-1]) {
print "$column\t \\$$record[-1]\n";
}
}
}
__DATA__
A, B, C, D, Rice Truck
E, G, D, Corn Train
T, H, K, M, N, Wheat Air cargo
__OUTPUT__
A \ Rice Truck
B \ Rice Truck
C \ Rice Truck
D \ Rice Truck
E \ Corn Train
G \ Corn Train
D \ Corn Train
T \ Wheat Air cargo
H \ Wheat Air cargo
K \ Wheat Air cargo
M \ Wheat Air cargo
N \ Wheat Air cargo
It's a long time since I looked at perl, but is this perl mod any good?
Data::Pivot
Related
I am trying to generate a new variable using the stored result r(mean) from the command sum.
I have a continuous variable 'age'.
So,
sum age
g age0=age–r(mean)
The problem is that this gives the error
unknown function age–r()
r(133);.
Works for me:
. sysuse auto, clear
(1978 Automobile Data)
. su mpg
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
mpg | 74 21.2973 5.785503 12 41
. gen mpg0 = mpg - r(mean)
. su mpg0
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
mpg0 | 74 -4.03e-08 5.785503 -9.297297 19.7027
Watch for strange characters, i.e. that the minus sign really is a hyphen [sic].
I'm trying to obtain the displacements of a node from an FEA .txt results file using matlab. I want to search for the node (e.g. 5151) and then read the displacements, the problem is the node is mentioned a couple of times before getting to the deformations.
The part of the results I'm interested in looks like this......
N O D E O U T P U T
THE FOLLOWING TABLE IS PRINTED FOR NODES BELONGING TO NODE SET ASSEMBLY_TIP_NODES
NODE FOOT- U1 U2 U3 UR1 UR2 UR3
NOTE
5101 2.6105E-03 -1.1943E-02 6.0023E-03 -8.6770E-02 -1.6432E-02 -1.1048E-02
5102 2.5224E-03 -1.1267E-02 5.6868E-03 -8.6763E-02 -1.6390E-02 -1.0943E-02
5103 2.4340E-03 -1.0589E-02 5.3709E-03 -8.6725E-02 -1.6269E-02 -1.0666E-02
5104 2.3455E-03 -9.9121E-03 5.0549E-03 -8.6542E-02 -1.6052E-02 -1.0267E-02
5105 2.2575E-03 -9.2374E-03 4.7396E-03 -8.6041E-02 -1.5708E-02 -9.7843E-03
5106 2.1710E-03 -8.5682E-03 4.4262E-03 -8.5111E-02 -1.5217E-02 -9.2317E-03
5107 2.0869E-03 -7.9085E-03 4.1164E-03 -8.3688E-02 -1.4585E-02 -8.6334E-03
5108 2.0063E-03 -7.2623E-03 3.8120E-03 -8.1750E-02 -1.3825E-02 -8.0049E-03
5109 1.9299E-03 -6.6336E-03 3.5150E-03 -7.9308E-02 -1.2958E-02 -7.3631E-03
5110 1.8586E-03 -6.0265E-03 3.2271E-03 -7.6400E-02 -1.2011E-02 -6.7206E-03
5111 1.7928E-03 -5.4442E-03 2.9500E-03 -7.3084E-02 -1.1010E-02 -6.0892E-03
5112 1.7329E-03 -4.8897E-03 2.6851E-03 -6.9435E-02 -9.9818E-03 -5.4777E-03
5113 1.6791E-03 -4.3652E-03 2.4334E-03 -6.5533E-02 -8.9528E-03 -4.8933E-03
etc, so what I want to do is search for a unique string ' N O D E O U T P U T' and then search for '5151' and import its displacements into an array.
Example output
nodeDisp = 1.6791E-03 -4.3652E-03 2.4334E-03 -6.5533E-02 -8.9528E-03 -4.8933E-03
I would like to import a table from another postgress database (9.1.7) to mine which is (9.1.11). I tried to import the dump but I get a bunch of syntax errors, I'm assuming there is some issue with the version mismatch?
Is there a better solution other than downgrading my postgress installation to match the desired input file?
This is the command I used to export the database on the 9.1.7 system:
pg_dump superdb -U tester -a -t guidedata > /tmp/guidedata.sql
This is the command I used to import the dump file guidedata.sql
psql linuxdb -U tester -h localhost < guidedata.sql
This is the top portion of the database dump file which I am attempting to import:
--
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET search_path = public, pg_catalog;
--
-- Data for Name: epg; Type: TABLE DATA; Schema: public; Owner: spy
--
COPY epg (id, channel, sdate, stime, duration, stitle, ltitle, theme, sdesc, ldesc, mpaa, rating, stereo, surround, sap, closedcaptioned, animated, blackwhite, rerun, live, ismovie, nudity, language, violence, adulttheme, halfstars, field1) FROM stdin;
90056520 AMC 01092014 0100 270 Titanic Titanic 8,15 A woman falls for an artist aboard the ill-fated ship. Leonardo DiCaprio, Kate Winslet (1997) A society girl abandons her haughty fiance for a penniless artist on the ill-fated ship's maiden voyage. (3:15) MPAAPG13 TVPG f f f t f f t f t t t t t 8 f
90056521 AMC 01092014 0530 180 Love Actually Love Actually 23,15 Various people deal with relationships in London. Hugh Grant, Laura Linney (2003) A prime minister, an office worker, a pop star, a jilted writer, married couples and various others deal with relationships in London. (2:15) MPAAR TVPG f f t t f f t f t t t f t 6 f
90056522 AMC 01092014 0830 150 Four Four Weddings and a Funeral 23,15 An English charmer meets a lusty American. Hugh Grant, Andie MacDowell (1994) An English charmer and a lusty American make love over a course of surprising events. (1:56) MPAAR TV14 f f t t f f t f t f t f t 7 f
90056523 AMC 01092014 1100 30 Paid Prog. Paid Programming 0 Paid programming. Paid programming. f f f f f f t f f f f f f 0 f
90056524 AMC 01092014 1130 30 Williams Montel Williams 19 Living well with Montel and the effects of identity theft. Living well with Montel and the devastating effects of identity theft. f f f f f f t f f f f f f 0 f
90056525 AMC 01092014 1200 30 Cindy Crawford Cindy Crawford Reveals Secret to Ageless Beauty 19 Cindy Crawford's skin secret with Meaningful Beauty. Cindy Crawford's supermodel secret to youthful, radiant-looking skin with Meaningful Beauty. f f f f f f t f f f f f f 0 f
90056526 AMC 01092014 1230 30 More Sex More Sex, Less Stress 19 Androzene promotes male sexual health & nourishes the body. Androzene promotes male sexual health and nourishes the body. f f f f f f t f f f f f f 0 f
90056527 AMC 01092014 1300 30 WEN Hair Care WEN by Chaz Dean Revolutionary Hair Care System 19 WEN by Chaz Dean is revolutionary hair care. WEN by Chaz Dean is revolutionary hair care that cleans and conditions without many shampoo's harsh detergents or sulfates. Natural ingredients help make hair shinier, fuller, softer and more manageable! By trusted GuthyRenker. f f f f f f t f f f f f f 0 f
90056528 AMC 01092014 1330 30 Medicare Looking for a Medicare plan? Tune in now! 19 Watch and learn about Humana Medicare Advantage plans. Watch and learn about Humana Medicare Advantage plans. f f f f f f t f f f f f f 0 f
90056529 AMC 01092014 1400 5 Stooges The Three Stooges 6 The caveman boys meet cavewomen. Moe Howard, Larry Fine ''I'm a Monkey's Unc
Here is some of the error output I see on the console when attempting to import:
ERROR: syntax error at or near "Route"
LINE 1: Route 66 renovation gives Ron a change of heart. TVPG t f f...
^
ERROR: syntax error at or near "Ron"
LINE 1: Ron and Jason bring out the Pontiac GTO. TVPG t f f t f f t...
^
ERROR: syntax error at or near "repairing"
LINE 1: repairing the clutch and drive shaft on the 1995 BMW. TVPG ...
^
ERROR: syntax error at or near "classic"
LINE 1: classic Bucik;
Thanks
My guess - one system is linux/unix/mac and the other is Windows.
It's complaining about the first line of the block of data because there is a stray carriage-return (\r) character.
Solution: use the (recommended unless you have a good reason not to) "custom" format with -Fc or --format=custom on your dump command. That should sort it.
I have a table with columns a, b, c. Can I calculate the correlation matrix of cor[a;a], cor[a;b], cor[a;c] using functional form somehow?
?[table; (); 0b; (`aa`ab`ac)!((cor; `a; `a); (cor; `a; `b);(cor; `a; `b));
How can i generate the list of the last argument?
(cor; a;b)
q)show t:([]a:5?1.0;b:5?1.0;c:5?1.0)
a b c
------------------------------
0.389056 0.949975 0.6919531
0.391543 0.439081 0.4707883
0.08123546 0.5759051 0.6346716
0.9367503 0.5919004 0.9672398
0.2782122 0.8481567 0.2306385
q)u cor/:\:u:flip t
| a b c
-| --------------------------------
a| 1 -0.1328262 0.6671159
b| -0.1328262 1 -0.1830702
c| 0.6671159 -0.1830702 1
So manually typed out form:
q)t:([] a:10?10; b:10?10; c:10?10)
q)?[t;();0b;`aa`ab`ac!((cor;`a;`a);(cor;`a;`b);(cor;`a;`c))]
aa ab ac
-----------------------
1 -0.2530506 0.7966834
If you wanted to generate the last argument, assuming you wanted all permutations of first column combined with all columns:
q)a:{(`$raze'[string x])!(cor),/:x}{x[0],/:x}cols t;
q)?[t;();0b;a]
aa ab ac
-----------------------
1 -0.2530506 0.7966834
If you wanted all column permutations:
q)a:{(`$raze'[string x])!(cor),/:x}{x cross x}cols t
q)?[t;();0b;a]
aa ab ac ba bb bc ca cb cc
----------------------------------------------------------------------
1 -0.2530506 0.7966834 -0.2530506 1 -0.268787 0.7966834 -0.268787 1
I am working on a project(I have to implement it in Perl but I am not good at it) that reads DNA and finds its RNA. Divide that RNA's into triplets to get the equivalent protein name of it. I will explain the steps:
1) Transcribe the following DNA to RNA, then use the genetic code to translate it to a sequence of amino acids
Example:
TCATAATACGTTTTGTATTCGCCAGCGCTTCGGTGT
2) To transcribe the DNA, first substitute each DNA for it’s counterpart (i.e., G for C, C for G, T for A and A for T):
TCATAATACGTTTTGTATTCGCCAGCGCTTCGGTGT
AGTATTATGCAAAACATAAGCGGTCGCGAAGCCACA
Next, remember that the Thymine (T) bases become a Uracil (U). Hence our sequence becomes:
AGUAUUAUGCAAAACAUAAGCGGUCGCGAAGCCACA
Using the genetic code is like that
AGU AUU AUG CAA AAC AUA AGC GGU CGC GAA GCC ACA
then look each triplet (codon) up in the genetic code table. So AGU becomes Serine, which we can write as Ser, or
just S. AUU becomes Isoleucine (Ile), which we write as I. Carrying on in this way, we get:
SIMQNISGREAT
I will give the protein table:
So how can I write that code in Perl? I will edit my question and write the code that what I did.
Try the script below, it accepts input on STDIN (or in file given as parameter) and read it by line. I also presume, that "STOP" in the image attached is some stop state. Hope I read it all well from that picture.
#!/usr/bin/perl
use strict;
use warnings;
my %proteins = qw/
UUU F UUC F UUA L UUG L UCU S UCC S UCA S UCG S UAU Y UAC Y UGU C UGC C UGG W
CUU L CUC L CUA L CUG L CCU P CCC P CCA P CCG P CAU H CAC H CAA Q CAG Q CGU R CGC R CGA R CGG R
AUU I AUC I AUA I AUG M ACU T ACC T ACA T ACG T AAU N AAC N AAA K AAG K AGU S AGC S AGA R AGG R
GUU V GUC V GUA V GUG V GCU A GCC A GCA A GCG A GAU D GAC D GAA E GAG E GGU G GGC G GGA G GGG G
/;
LINE: while (<>) {
chomp;
y/GCTA/CGAU/; # translate (point 1&2 mixed)
foreach my $protein (/(...)/g) {
if (defined $proteins{$protein}) {
print $proteins{$protein};
}
else {
print "Whoops, stop state?\n";
next LINE;
}
}
print "\n"
}