Hi Hori, Please check below for my comments on your code. On 11 Feb 2013 03:00, "Tiago Hori" <tiago.h...@gmail.com> wrote: > > Hi All, > > I am trying to force myself to not use one of perl's modules to parse tab > delimited files (like TXT::CSV), so Are you referring to module Text::CSV and it's cousin Text::CSV_XS? please be patient and don't tell me > just to go and use them. I am trying to re-ivent the wheel, so to speak, > because as we do with science, we repeat experiments to lean about the > process even tough we know the outcome. > > So I started by putting reading in the files and go one line at time, > putting those line in arrays and matching a specific line of interest. With > join I could then turn the array of interest in a scalar and print that > out. That is almost what I wanted (see code below): > > #! /usr/bin/perl > use strict; > use warnings; > > my $filename_data = $ARGV[0]; > my $filename_target = $ARGV[1]; > my $line_number = 1; You really don't need the variable $ line_number within your while loop, since you could use perl's line number "$." > my @targets; > > open FILE, "<", $filename_data or die $!; > open TARGET, "<", $filename_target or die $!; > It good practice to use lexical variable as filehandles and not BAREWORDs. Like : open my $ fh,'<', $ filename or die "can't open file:$!"; > while (<TARGET>){ > push (@targets, $_); This push each of the line with the "\n" into your array. > } > > close (TARGET); Also, try to check return of the close function on the filehandles. > > while (<FILE>){ > chomp; > my $line = $_; > my @elements = split ("\t", $line); > my $row_name = $elements[0]; > if ($line_number == 1){ or if ($. == 1){ # Of course, some may argue that your variable makes the code, readable. > my $header = join("\t", @elements); > print $header, "\n"; > $line_number = 2;} > elsif($line_number = 2){ Is your file just a 2 line document? If not, what happens if $ line_number is more than 2? However, if the row you are interested in is just the second line of your document, then that is ok. > foreach (@targets){ > chomp; You probably wouldn't have needed the line above, if you had removed the new line when populating your array variable in the first place. > my $target = $_; > if ($row_name eq $target){ > my $data = join("\t", @elements); > print $data,"\n"; > } > } > } > } > > close (FILE); > > Realistic, I don't want the whole row. So I started thinking about how to > get specific columns. I started reading on the internet and the ideas seems > to be placing the arrays containing the lines in a hash indexed by the row > names. So I did this: > > #! /usr/bin/perl > > use strict; > use warnings; > > my %hash; > open FILE, "test.txt" or die $!; Please stay with the 3 argument for open function. > my $key; > > while (my $line = <FILE>) { > chomp($line); > my @array = split("\t", $line); > $key = shift(@array); > print $key, "\n"; > push @{ $hash{$key} }, @array; > } > > my $out = join("\t",@{$hash{Row}}); > print $out,"\n"; > print $hash{Row}[1]; > > > close FILE; You are using only one file in your second script. > That works fine and reads the lines into an array with the elements > separated into the hash. > > So my questions are: is this the best way of accomplishing this two tasks? > I am really looking for suggestions to improve what I am doing. Is using > the hashes the best way? > > I am have trying to figure out a way of extracting the columns and placing > them in a hash or a hash of array
Please check the documentation: perldsc. This might help. or an array. I can't figure out or find > it in the internet. Can someone give me directions? I am not asking for > code, but some ideas or some pseudocode would be great! > > Thanks! > > T. > > > -- > "Education is not to be used to promote obscurantism." - Theodonius > Dobzhansky. > > "Gracias a la vida que me ha dado tanto > Me ha dado el sonido y el abecedario > Con él, las palabras que pienso y declaro > Madre, amigo, hermano > Y luz alumbrando la ruta del alma del que estoy amando > > Gracias a la vida que me ha dado tanto > Me ha dado la marcha de mis pies cansados > Con ellos anduve ciudades y charcos > Playas y desiertos, montañas y llanos > Y la casa tuya, tu calle y tu patio" > > Violeta Parra - Gracias a la Vida > > Tiago S. F. Hori. PhD. > Ocean Science Center-Memorial University of Newfoundland