Re: Hi, my first question is about binary data

2001-06-18 Thread Tirthankar C.P





> Hey folks,
> 
> Please don't scold me if I am asking a very stupid
> question. =)
> 
> I am now handling some sort of text files,
> however, they are eating me a lot of harddisk.
> 
> I tried to using PACK, however, I found that I cannot
> unpack it if I pack the file with "B" or "H"...
> 
> Would anybody have any ideas or examples can tell me??
> Thank you very very much and thousands sorry if I am
> really stupid.
> 


pack is not meant for the job you're looking at. It's used to convert
ASCII data into binary Why don't you use standard compression packages
like,

1. gzip (on all unix systems)
2. bzip2 (best but not avbl on all systems--our DEC OSF v4.0 doesn't have
it).


HTH, 

-tir




Re: Sorting an array by one of it's fields.

2001-06-18 Thread Tirthankar C.P


Thanks a million to Jos Boumans, and Me (whoever that is). Me, thanks for
the explanation, and Jos, for your patient and detailed answer. 

-tir 




On Sat, 16 Jun 2001, Me wrote:

> > This should've worked. But why do I get a warning:
> >
> > Use of uninitialized value at ./mk2_ratingchangedb.pl line 39, 
> chunk 8.
> 
> Whenever you're dealing with baffling array errors
> like this, always think of off-by-one.
> 
> In this case:
> 
> > 30  for ($i=1; ...) {
> > 31  $dummy[$i][0] =
> 
> doesn't initialize $dummy[0].
> 
> 

Tirthankar C.Patnaik
Research Scholar
Indira Gandhi Institute of Development Research  Ring:+91-22-840 0919/20/21
Goregaon EastOff:- x593 Res: x675  
Mumbai 400 065   Fax:  91-022-8402752
India. http://www.igidr.ac.inemail:[EMAIL PROTECTED] 

Never argue with an idiot. They drag you down to their level, then
beat you with experience. -dilbert 

---  




Re: Sorting an array by one of it's fields.

2001-06-16 Thread Tirthankar C.P


Thnaks a lot Jos. The idea of reading the array into a hash is quite appealing, and 
simple too. But I have a small problem with this: 

What if I want to sort on the second column of the array? Or if there are more
than two columns? 

Say we have: 
my @arr = qw(
1 2 3 4
4 5 6 7
5 6 7 8
1 2 3 4
3 1 8 3  
); 
And if I wanted to sort on the 3rd column. 
Then I could write: 

@sorted  = sort { $a->[2] <=> $b->[2] } @arr; 

# or 'cmp' if the sorting is to be lexical. 

This should've worked. But why do I get a warning: 

Use of uninitialized value at ./mk2_ratingchangedb.pl line 39,  chunk 8. 

Here's the snippet of my code: 

29  my(@dummy, @dummy2, @wed);
30  for ($i=1; $i<=$N; $i++) {
31  $dummy[$i][0] = &ParseDate($data{$key}[$i][0]);
32  if (! $dummy[$i][0]) {
33warn "Could not parse $data{$key}[$i][0]\n";
34  }
35  $dummy[$i][1] = $data{$key}[$i][1];
36  }
37  @wed = @dummy;
38  &matrix_printer ("pre sort", @wed);
39  @dummy2 = sort { $a->[0] cmp  $b->[0] } @wed;
40  printf " \n";
41  &matrix_printer ("post sort", @dummy2);
42  } 

What could be wrong? ( The array @wed was REQUIRED. Otherwise my code
didn't work at all).

Thanks for the prompt response, 

-tir



On Sat, 16 Jun 2001, Jos I. Boumans wrote:

>  ok, so if i get this right, @dummy has the following format:
> 
> my @dummy = qw(
> 1996013100:00:00MAAA
> 281100:00:00MA-
> 1997063000:00:00MAAA
> 1998122200:00:00MAA
> 2000112400:00:00MD
> );
> 
> now we have that established, let's say that as soon as you think 'gee i
> need to SORT something on ONE VALUE in this data' you should probably be
> using a hash.
> so that's what we'll do
> 
> what we'll do is read every line of @dummy, split it on white space and put
> that as key/value pairs in a hash, like so:
> my %hash = map { split /\s/ } @dummy;
> 
> this is equivalent to the more verbose:
> my @data;
> for my $element (@dummy) {
> my ($key, $value) = split(/\s/, $element);
> push(@datay, $key, $value);
> }
> my %hash = @data;
> 
> as you can see, the above is quite a bit more writing, slow and ugly then
> the map solution, but it's more 'logical' to read for most.
> also note that there's no objection to saying: %hash =
> @array_with_key_value_pairs
> the other way around is a bit more tricky, seeing hashes keep their
> key/value pairs seemingly unordened, wheras arrays keep indexes... so you
> can't say:
> %hash = @array_with_key_value_pairs;
> @copy_of_array_with_key_value_pairs = %hash;
> and assume that
> @copy_of_array_with_key_value_pairs eq @array_with_key_value_pairs;
> 
> having said that, let's see how we can get our info out of the hash like we
> want to:
> for (sort keys %hash) {print "$_ has value $hash{$_}\n"}
> 
> the above will print:
> 1996013100:00:00 is MAAA
> 1997063000:00:00 is MAAA
> 1998122200:00:00 is MAA
> 281100:00:00 is MA-
> 2000112400:00:00 is MD
> 
> Hope this does what you want,
> 
> hth,
> 
> Jos Boumans
> 
> 
> 
> > Folks,
> > # How do I sort an array by one of it's fields?
> >
> > # @dummy before sorting has this:
> >
> > 1996013100:00:00MAAA
> > 281100:00:00MA-
> > 1997063000:00:00MAAA
> > 1998122200:00:00MAA
> > 2000112400:00:00MD
> 
> > I would like to sort by it by the first column, which is a date read by
> > ParseDate fron Date::Manip.
> 
> 
> 

Tirthankar C.Patnaik
Research Scholar
Indira Gandhi Institute of Development Research  Ring:+91-22-840 0919/20/21
Goregaon EastOff:- x593 Res: x675  
Mumbai 400 065   Fax:  91-022-8402752
India. http://www.igidr.ac.inemail:[EMAIL PROTECTED] 

Never argue with an idiot. They drag you down to their level, then
beat you with experience. -dilbert 

---  




Sorting an array by one of it's fields.

2001-06-16 Thread Tirthankar C.P



Folks, 
# How do I sort an array by one of it's fields? 

I have a this code: 

  for ($i=1; $i<=$N; $i++) {  }
   $dummy[$i][0] = &ParseDate($data{$key}[$i][0]);
if (! $dummy[$i][0]) {  }
  warn "Could not parse $data{$key}[$i][0]\n";
 }
  $dummy[$i][1] = $data{$key}[$i][1];
  }
 &dumpdummy("Before sorting --", \@dummy);
   # Now we need to sort the # two columns of @dummy by # column 0 (date).
 @dummy2 = sort { $a->[0]  cmp  $b->[0]} @dummy; 
&dumpdummy("After sorting -- ", \@dummy2);
}

# @dummy before sorting has this: 

1996013100:00:00MAAA
281100:00:00MA-
1997063000:00:00MAAA
1998122200:00:00MAA
2000112400:00:00MD 


&dumpdummy is just a sub that writes the array in a nice way. 

I would like to sort by it by the first column, which is a date read by 
ParseDate fron Date::Manip. 

What am I doing wrong? How would it be done anyway? 

TIA, 
-tir














Simple Matrix Question

2001-05-16 Thread Tirthankar C.P



What is the simplest way to read in a matrix, say


19911231 12200 4274
 19921231 12205 7822
 19931231 3313 8208
 19941231 2362 12292
 19951231 4348 11673
 19961231 11751 14255 
19971231 6318 12339
19981231 14901 11530
19991231 373 17353 


in perl? I just know that the matrix has to have three rows, but the
number of columns can vary. 

any ideas? 

TIA, 
-tir




Re: Breaking up a file.

2001-05-10 Thread Tirthankar C.P


Many thanks to Paul. His solution works. 

-tir


On Thu, 10 May 2001, Paul wrote:

> 
> --- "Tirthankar C.P" <[EMAIL PROTECTED]> wrote:
> > 
> > Folks, I have a file like this:
> > 
> > Asea Brown Boveri Ltd.
> > 02-Aug-1999  |  02-Aug-1999  |  399.05
> > 03-Aug-1999  |  03-Aug-1999  |  395.00
> > 04-Aug-1999  |  04-Aug-1999  |  426.5
> > 06-Aug-1999  |  06-Aug-1999  |  406.00
> > 31-Jul-2000  |  31-Jul-2000  |  203.00
> > 01-Aug-2000  |  01-Aug-2000  |  203.65
> > |  Overall  |  399.05  |  203.65
> > Asian Paints (India) Ltd.
> > 02-Aug-1999  |  02-Aug-1999  | 
> > 03-Aug-1999  |  03-Aug-1999  |
> > 28-Jul-2000  |  28-Jul-2000  |
> > 31-Jul-2000  |  31-Jul-2000  |
> > 01-Aug-2000  |  01-Aug-2000  |
> > |  Overall  |  196.88  |  280.70 
> > Associated Cement Cos. Ltd.
> > 02-Aug-1999  |  02-Aug-1999  |  196.10  |
> > 03-Aug-1999  |  03-Aug-1999  |  211.75  |
> > 04-Aug-1999  |  04-Aug-1999  |  224.80  |
> > 10-Aug-1999  |  10-Aug-1999  |  231.10  |
> > 
> > I would like to break up this file into three parts, based on the
> > company name, i.e., so that the data following the stock name
> > (till the next stock), comes in that file. Any ideas? 
> 
> Hmm
> 
>  my $dat;  # just making memory space for the file
>  open IN, $file or die $!; # assuming $file already has the name
>  { local $/ = undef;   # $/ is the input record seperator
>$dat = ;# slurp it all in at once
>  } # close the local() scope to restore $/
>  my @sections = split /\n(?=[a-z])/i, $dat; # see below
> 
> Notes:
>  1) I've never done this. It may not work. (lol)
>  2) If it does work, this will leave every section but the last one
> without it's trailing newline. (just FYI)
> 
> Explanation:
> 
>  my @sections = split /\n(?=[a-z])/i, $dat;
> 
> Obviously, the my() creates @sections, which will hold each section of
> the file in a seperate cell (with which you may then do as you please).
> 
> split() is going to break $dat into the pieces that you want, based on
> the pattern.
> 
>  /\n(?=[a-z])/i means "find every newline followed by an alphabetic
> character". (?=$pat) is a zero-width lookahead, so that only newlines
> followed by letters count. If that's not exactly the effect you want,
> you could easily edit the pattern. =o)
> 
> This is assuming the structure of the file is pretty uniformly
> consistent with the example you gave.
> 
> Good luck,
> Paul
> 
> =
> print "Just another Perl Hacker\n"; # edited for readability =o)
> =
> Real friends are those whom, when you inconvenience them, are bothered less by it 
>than you are. -- me. =o) 
> =
> "There are trivial truths and there are great Truths.
>  The opposite of a trival truth is obviously false.
>  The opposite of a great Truth is also true."  -- Neils Bohr
> 
> __
> Do You Yahoo!?
> Yahoo! Auctions - buy the things you want at great prices
> http://auctions.yahoo.com/
> 

Tirthankar C.Patnaik
Research Scholar
Indira Gandhi Institute of Development Research  Ring:+91-22-840 0919/20/21
Goregaon EastOff:- x593 Res: x675  
Mumbai 400 065   Fax:  91-022-8402752
India. http://www.igidr.ac.inemail:[EMAIL PROTECTED] 

Never argue with an idiot. They drag you down to their level, then
beat you with experience. -dilbert 

---  




Breaking up a file.

2001-05-10 Thread Tirthankar C.P


Folks, I have a file like this:

Asea Brown Boveri Ltd.
02-Aug-1999  |  02-Aug-1999  |  399.05
03-Aug-1999  |  03-Aug-1999  |  395.00
04-Aug-1999  |  04-Aug-1999  |  426.5
06-Aug-1999  |  06-Aug-1999  |  406.00
31-Jul-2000  |  31-Jul-2000  |  203.00
01-Aug-2000  |  01-Aug-2000  |  203.65
|  Overall  |  399.05  |  203.65
Asian Paints (India) Ltd.
02-Aug-1999  |  02-Aug-1999  | 
03-Aug-1999  |  03-Aug-1999  |
28-Jul-2000  |  28-Jul-2000  |
31-Jul-2000  |  31-Jul-2000  |
01-Aug-2000  |  01-Aug-2000  |
|  Overall  |  196.88  |  280.70 
Associated Cement Cos. Ltd.
02-Aug-1999  |  02-Aug-1999  |  196.10  |
03-Aug-1999  |  03-Aug-1999  |  211.75  |
04-Aug-1999  |  04-Aug-1999  |  224.80  |
10-Aug-1999  |  10-Aug-1999  |  231.10  |

I would like to break up this file into three parts, based on the company
name, i.e., so that the data following the stock name (till the next
stock), comes in that file. Any ideas? 

1.Asea Brown Boveri Ltd.
2.Asian Paints (India) Ltd.
3.Associated Cement Cos. Ltd.

TIA
-tir




Re: Loading extra modules.

2001-05-09 Thread Tirthankar C.P



> 
> Be careful -- you can't just move modules into a directory and expect them
> to work, since some depend on loading native code shared libraries.

Brett was right. I tried installing it, but wasn't successful. I then got
the Sys Admin folks to install it, and it went just fine!! 

Apparently, even if I give PREFIX= as a command line param, you get the
following errors: 



Manifying blib/man3/Date::Manip.3 Skipping
/home/tir/COMP/perl/modules/lib/site_perl/5.005/Date/Manip.pm (unchanged)
Skipping /home/tir/COMP/perl/modules/lib/site_perl/5.005/Date/Manip.pod
(unchanged) Installing
/home/tir/COMP/perl/modules/lib/site_perl/5.005/Date/lib/perl5/site_perl/5.005/Date/Manip.pm

Installing
/home/tir/COMP/perl/modules/lib/site_perl/5.005/Date/lib/perl5/site_perl/5.005/Date/Manip.pod
Installing


/home/tir/COMP/perl/modules/lib/site_perl/5.005/perl5/site_perl/5.005/i386-linux/auto/Date/Manip/.packlist

Skipping /home/tir/COMP/perl/modules/lib/perl5/man/man3/Date::Manip.3
(unchanged) Installing
/home/tir/COMP/perl/modules/lib/perl5/man/man3/lib/perl5/man/man3/Date::Manip.3
Writing
/home/tir/COMP/perl/modules//lib/site_perl/5.005/i386-linux/auto/Date/Manip/.packlist
/bin/sh:
/home/tir/COMP/perl/modules//lib/5.00503/i386-linux/perllocal.pod: No such
file or directory make: [doc_site_install] Error 1 (ignored)
Appending installation info to
/home/tir/COMP/perl/modules//lib/5.00503/i386-linux/perllocal.pod   


I don't know what to make of this!!
Thanks anyway, 
-tir




Loading extra modules.

2001-05-09 Thread Tirthankar C.P



Folks, how do I load extra modules? I got Date::Manip today, and I know I
should be putting it in the search-path of perl (@INC ?), but how do I do
it? Can I get a small code snippet? 

I do not have su privileges on the machine I work, and would like the
search-path to have my modules dir, $HOME/perlmodules in it. 

TIA, 
-tir