Hi John, thanks for your E-mail. See below for my response.
On Sunday 17 Apr 2011 04:17:32 John W. Krahn wrote: > Shlomi Fish wrote: > > On Saturday 16 Apr 2011 09:06:02 Gurunath Katagi wrote: > >> hi .. i am new to perl .. > >> i have a input file something pasted as below .. > >> > >> 16 50 > >> 16 30 > >> 16 23 > >> 17 88 > >> 17 99 > >> 18 89 > >> 18 1 > >> .. > >> -- > >> > >> and i want the output something like this : > >> 16 50 30 23 > >> 17 88 99 > >> 18 99 1 > >> > >> i.e for each values in the first column, i want the elements in the > >> second column to be a one row .. > >> > >> can be anybody give me psuedocode , to how to go about this .. > >> thank you > > > > This should work (tested): > > > > Regards, > > > > Shlomi Fish > > > > ----------------------------- SNIP ------------------------ > > > > #!/usr/bin/perl > > > > use strict; > > use warnings; > > > > my $filename = shift(@ARGV); > > > > open my $in_fh, '<', $filename > > > > or die "Cannot open '$filename' for reading"; > > You should include the $! or $^E variable in the error message so you > know why it failed. Correct - it was not oversight on my part. > > > my $last_key = undef; > > ICK! > Well, I'm trying to specify that I initialise it to a value where it needs to be set. > > my @values; > > > > sub print_line > > { > > You are using a subroutine for this??? Really? > Yes, because it is duplicate code. > Why are you using global variables instead of lexically scoped > variables? You should know better. > Well, «my @values;» is a lexically scoped variable - it's just part of the global scope of the program. You cannot access it from a different module. Regarding global variables - I could have created an object and a module out of it, but that's overengineering for such a silly script, which is all the author has asked for. See: http://en.wikipedia.org/wiki/You_ain%27t_gonna_need_it > > print $last_key, ' ', join(' ', @values), "\n"; > > Or: > > print join( ' ', $last_key, @values ), "\n"; > > Or even: > > print "$last_key @values\n"; > > Is there really a good reason to make it more complicated than it needs > to be? > Well, I dislike interpolating arrays, and I was trying to be explicit. But «print "$last_key @values\n";» will also work nicely. > > return; > > > > } > > > > while (my $line =<$in_fh>) > > { > > > > chomp($line); > > > > my ($new_key, $new_value) = split(/\s+/, $line); > > What if there is leading whitespace? What if there's an earthquake? It wasn't in the SPEC. > > You do realize that split(/\s+/) removes ALL (trailing) whitespace so > using chomp() is redundant. I realise it now. But using chomp on a line explicitly, in case you are not interested in keeping the newline is a good habit which should be followed religiously. Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ The Case for File Swapping - http://shlom.in/file-swap Every successful open-source project will eventually spawn a sub-project. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/