[30966] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 2211 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Feb 16 00:09:43 2009

Date: Sun, 15 Feb 2009 21:09:07 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 15 Feb 2009     Volume: 11 Number: 2211

Today's topics:
    Re: cpan defaults on first use. Can I provide them in a <brian.d.foy@gmail.com>
        Default initial array value? [TMTOWTDI] <amphetamachine@gmail.com>
    Re: Default initial array value? [TMTOWTDI] <amphetamachine@gmail.com>
    Re: Default initial array value? [TMTOWTDI] <someone@example.com>
    Re: Default initial array value? [TMTOWTDI] <amphetamachine@gmail.com>
    Re: Default initial array value? [TMTOWTDI] sln@netherlands.com
    Re: FAQ 6.14 How do I process each word on each line? <larry@example.invalid>
    Re: Regexp discovery - using ^ with /m is a time sink <whynot@pozharski.name>
    Re: restrict a hash to 15 pairs and iterate over it <tadmc@seesig.invalid>
    Re: restrict a hash to 15 pairs and iterate over it <xhoster@gmail.com>
    Re: restrict a hash to 15 pairs and iterate over it <hjp-usenet2@hjp.at>
    Re: restrict a hash to 15 pairs and iterate over it <mstep@podiuminternational.org>
    Re: restrict a hash to 15 pairs and iterate over it sln@netherlands.com
    Re: restrict a hash to 15 pairs and iterate over it sln@netherlands.com
    Re: time and place of satellite coincidence <larry@example.invalid>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sun, 15 Feb 2009 16:41:51 -0600
From: brian d  foy <brian.d.foy@gmail.com>
Subject: Re: cpan defaults on first use. Can I provide them in a non-interative format?
Message-Id: <150220091641519349%brian.d.foy@gmail.com>

In article <Xns9BB091CB5BE366650A1FC0D7811DDBC81@85.214.105.209>, Rahul
<nospam@nospam.invalid> wrote:

> After I do a "yum install cpan" the first time around cpan insists on 
> asking all the questions about default's, preferred download hosts etc. 
> 
> Is there  a way to provide this info. in a non-interactive manner? I need 
> to do this on a bunch of similar machines.

The latest cpan-script has a -j option to load an arbitrary
configuration file. I haven't made a release of it yet, but it is on 
Github:

     http://github.com/briandfoy/cpan-script/tree/master


------------------------------

Date: Sun, 15 Feb 2009 10:27:46 -0800 (PST)
From: h3xx <amphetamachine@gmail.com>
Subject: Default initial array value? [TMTOWTDI]
Message-Id: <fcca4daf-800d-4664-8a70-86a544fc14b6@v4g2000vbb.googlegroups.com>

I know this kind of goes against traditional programming practices,
but here goes:

I need to initialize an array to all zeroes to begin with, but the
only way I see is to set each index manually:

my $ary = [ map {0} 0 .. $last_index ];

Because the indices invariably get set to undef if I just set the last
index. I've read the perlvar manpage over and over again and can't
find a relevant variable to determine what undefined array indices get
set to instead of undef.

My question is, Is there more than one way to do this?


------------------------------

Date: Sun, 15 Feb 2009 10:32:28 -0800 (PST)
From: h3xx <amphetamachine@gmail.com>
Subject: Re: Default initial array value? [TMTOWTDI]
Message-Id: <dab2e090-b1dc-4b21-9665-18d2c184baf2@m15g2000vbp.googlegroups.com>

On Feb 15, 12:27=A0pm, h3xx <amphetamach...@gmail.com> wrote:
> I know this kind of goes against traditional programming practices,
> but here goes:
>
> I need to initialize an array to all zeroes to begin with, but the
> only way I see is to set each index manually:
>
> my $ary =3D [ map {0} 0 .. $last_index ];
>
> Because the indices invariably get set to undef if I just set the last
> index. I've read the perlvar manpage over and over again and can't
> find a relevant variable to determine what undefined array indices get
> set to instead of undef.
>
> My question is, Is there more than one way to do this?

I mean, I can think of this:

$ary =3D [ split //, '0' x $num_indices ];

But that's dealing with strings parsing and regex matching, which is
slower than what I'm thinking of.


------------------------------

Date: Sun, 15 Feb 2009 10:32:36 -0800
From: "John W. Krahn" <someone@example.com>
Subject: Re: Default initial array value? [TMTOWTDI]
Message-Id: <8dZll.8369$EO2.1649@newsfe04.iad>

h3xx wrote:
> I know this kind of goes against traditional programming practices,
> but here goes:
> 
> I need to initialize an array to all zeroes to begin with, but the
> only way I see is to set each index manually:
> 
> my $ary = [ map {0} 0 .. $last_index ];
> 
> Because the indices invariably get set to undef if I just set the last
> index. I've read the perlvar manpage over and over again and can't
> find a relevant variable to determine what undefined array indices get
> set to instead of undef.
> 
> My question is, Is there more than one way to do this?

my @ary = ( 0 ) x $size;



John
-- 
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov


------------------------------

Date: Sun, 15 Feb 2009 12:08:21 -0800 (PST)
From: h3xx <amphetamachine@gmail.com>
Subject: Re: Default initial array value? [TMTOWTDI]
Message-Id: <69901c1d-86e2-4647-b2be-681ab73fd815@m15g2000vbl.googlegroups.com>

On Feb 15, 12:32=A0pm, "John W. Krahn" <some...@example.com> wrote:
> h3xx wrote:
> > I know this kind of goes against traditional programming practices,
> > but here goes:
>
> > I need to initialize an array to all zeroes to begin with, but the
> > only way I see is to set each index manually:
>
> > my $ary =3D [ map {0} 0 .. $last_index ];
>
> > Because the indices invariably get set to undef if I just set the last
> > index. I've read the perlvar manpage over and over again and can't
> > find a relevant variable to determine what undefined array indices get
> > set to instead of undef.
>
> > My question is, Is there more than one way to do this?
>
> my @ary =3D ( 0 ) x $size;
>
> John
> --
> Those people who think they know everything are a great
> annoyance to those of us who do. =A0 =A0 =A0 =A0-- Isaac Asimov

Nice! Thanks. I had no idea about that trick!


------------------------------

Date: Mon, 16 Feb 2009 04:31:36 GMT
From: sln@netherlands.com
Subject: Re: Default initial array value? [TMTOWTDI]
Message-Id: <kvqhp49gpkbf1mpfgc0mcn1pe0c3qtlvm4@4ax.com>

On Sun, 15 Feb 2009 10:27:46 -0800 (PST), h3xx <amphetamachine@gmail.com> wrote:

>I know this kind of goes against traditional programming practices,
>but here goes:
>
>I need to initialize an array to all zeroes to begin with, 
[snip]

There is never a need to do this.
-sln



------------------------------

Date: Sun, 15 Feb 2009 22:02:55 -0700
From: Larry Gates <larry@example.invalid>
Subject: Re: FAQ 6.14 How do I process each word on each line?
Message-Id: <j7akvr2rqy8s$.mfwqfp0ve6rw$.dlg@40tude.net>

On Sat, 14 Feb 2009 06:03:01 -0800, PerlFAQ Server wrote:

>     To work with only alphanumeric sequences (including underscores), you
>     might consider
> 
>             while (<>) {
>                     foreach $word (m/(\w+)/g) {
>                             # do something with $word here
>                     }
>             }

So if your data are 
quick brown %% fixed %noise
you will do something with $word 4 times?
-- 
larry gates

So please don't think I have a "down" on the MVS people.  I'm just pulling
off their arms to beat other people over the head with.
             -- Larry Wall in <199808050415.VAA24026@wall.org>


------------------------------

Date: Mon, 16 Feb 2009 01:03:44 +0200
From: Eric Pozharski <whynot@pozharski.name>
Subject: Re: Regexp discovery - using ^ with /m is a time sink
Message-Id: <slrngph7rh.mcq.whynot@orphan.zombinet>

On 2009-02-14, Koszalek Opalek <koszalekopalek@interia.pl> wrote:
> #!/usr/bin/perl
>
>=pod
>
> The code below benchmarks two regexp's that look for lines
> starting with the equals sign in a multiline string. The
> regexps differ only by how the line break is detected.
> The first regexp uses the ^ metacharacter and the /m flag:
>         qr{\G^=[^\n]*}ism
> The other relies on a negative look-behind assertion:
>         qr{\G(?<=\n)=[^\n]*}ism
>
*SKIP*
> Could someone explain what's going behind the scenes in
> the regexp engine? Is it scanning the complete string for
> line breaks if I use ^, even though it has to match only
> at pos() ?

You can do it yourself.  Your distro is supposed to provide B<perl>
compiled with I<-DDEBUGGING> enabled.  Than use I<-D512> option,
F<perldebguts> has more.

If my understanding of I<-D512> output is correct, then answer is: no,
perl doesn't.

>
>=cut
>
>
> use strict;
> use Time::HiRes qw( time );
> $| = 1;
>
> my $gibberish;
> for( 1 .. 1000 ) {
>         for( 1 .. int(rand  50) ) {
>                 $gibberish .= chr( int( rand 60) + 32 );
>         };
>         $gibberish .= "\n";
> }

Please don't.  (if I retranslate it back to English correctly) "Random
random number generators cycle after random number of cycles"
(attributed to Knuth).  Once you use random patterns you'll get random
results -- if your results are random then no-one (you -- first) can't
trust those results.

I've tried your REs -- and for me successful look-behind is slightly
faster than anything else.  (I should admit I've never used C<m//gism>,
and C<qr/\G/> so that's possible I've messed something up.)

*CUT*

-- 
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom


------------------------------

Date: Sun, 15 Feb 2009 09:14:01 -0600
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: restrict a hash to 15 pairs and iterate over it
Message-Id: <slrngpgc9p.ung.tadmc@tadmc30.sbcglobal.net>

Marek <mstep@podiuminternational.org> wrote:
> On Feb 15, 10:06 am, Ben Morrow <b...@morrow.me.uk> wrote:
>
>>
>> so for a list like
>>
>>     1  2  1  4  2
>>
>> you would get the results
>>
>>     1: 2 times
>>     2: 2 times
>>     4: 1 time
>>
>
> yes! :-)


> So the first step would be to read in the numbers until 15th line and
> see if there are double numbers or triple numbers. In my example here
> there are two 1 and two 2 ... Then I do something with this result and
> I read in the next 15 numbers starting with 2 ...
>
>
> 1 2 3 4 1 5 6 7 8 9 10 2 11 12 13 14 15 16 17
>   ^                               ^
>
> next step we read in starting with 3
>
>
> 1 2 3 4 1 5 6 7 8 9 10 2 11 12 13 14 15 16 17
>     ^                                ^
>
> But you gave already a valuable hint: we only need a hash of each
>
> %number = (1 => [1,5]);


If you instead use a hash that buffers 15 elements at a time, then
you can generate a hash with the counts directly, as you don't seem
to want to know the actual line numbers...

I thinks this does what you are asking for:


-----------------------------
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;

my $size = 5;  # 5 instead of 15
my $line = 0;  # "line" counter
my %lines;     # buffer up $size lines

while ( <DATA> ) {
    next if /\./; # skip dates
    chomp;
    $line++;

    $lines{$line} = $_;

    if ( keys %lines == $size ) {

        print Dumper \%lines;  # for debugging

        # count what is in the buffer
        my %nums;
        $nums{$_}++ for values %lines;

        # display what is in the (counted) buffer
        foreach my $num ( sort { $a <=> $b } keys %nums ) {
            printf "%3d: %3d times\n", $num, $nums{$num};
        }
        print "---------\n";

        # maintain buffer size
        delete $lines{ $line - $size + 1};
    }
}

__DATA__
01.01.98
31
33
01.02.98
01.03.98
14
7
35
16
20
20
13
55
1
1
7
-----------------------------


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


------------------------------

Date: Sun, 15 Feb 2009 11:06:55 -0800
From: Xho Jingleheimerschmidt <xhoster@gmail.com>
Subject: Re: restrict a hash to 15 pairs and iterate over it
Message-Id: <4998679d$0$30506$ed362ca5@nr5-q7.newsreader.com>

Marek wrote:
> On Feb 15, 10:06 am, Ben Morrow <b...@morrow.me.uk> wrote:
> 
>> What do you mean by 'the occurrences of numbers'? Do you mean the number
>> of times each number shows up, so for a list like
>>
>>     1  2  1  4  2
>>
>> you would get the results
>>
>>     1: 2 times
>>     2: 2 times
>>     4: 1 time
>>
> 
> yes! :-)
> 
>> If you need a data structure like this (I'm not yet sure whether you do)
>> you want something more like
>>
>>     my %numbers = (
>>         20 => [7, 8]
>>     );
>>
>> You don't need to record the count separately: Perl arrays know how long
>> they are.
>>
> 
> Thank you! Good idea!

If you need to know both the counts of occurence and the locations, then 
this is a good idea.  The list of locations automatically includes the 
count of occurences.  But if *only* need the count, then I wouldn't 
store the list of locations as well.


> 
> I have a large file with many numbers in it. Each line a number and
> sometimes some dates. I read in from the beginning 15 numbers, here
> separated with a tab:
> 
> 1 2 3 4 1 5 6 7 8 9 10 2 11 12 13 14 15 16 17
> ^                              ^
> 
> 
> So the first step would be to read in the numbers until 15th line and
> see if there are double numbers or triple numbers. In my example here
> there are two 1 and two 2 ... Then I do something with this result and
> I read in the next 15 numbers starting with 2 ...
> 
> 
> 1 2 3 4 1 5 6 7 8 9 10 2 11 12 13 14 15 16 17
>   ^                               ^

So it is a sliding window.

my @window;
my %count;
while (<>) { chomp;
   next if looks_like_date_not_number($_);
   push @window, $_;
   $count{$_}++;
   if (@window>15) {
     ## window is too big, get rid of the first one;
     $count{$window[0]}--;
     shift @window;
   };
   if (@window==15) {
     # do whatever you want to do with %count
   }
};

Xho


------------------------------

Date: Sun, 15 Feb 2009 21:30:25 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: restrict a hash to 15 pairs and iterate over it
Message-Id: <slrngpgur2.tl3.hjp-usenet2@hrunkner.hjp.at>

On 2009-02-15 12:21, Marek <mstep@podiuminternational.org> wrote:
> I have a large file with many numbers in it. Each line a number and
> sometimes some dates. I read in from the beginning 15 numbers, here
> separated with a tab:
>
> 1 2 3 4 1 5 6 7 8 9 10 2 11 12 13 14 15 16 17
> ^                              ^
>
>
> So the first step would be to read in the numbers until 15th line and
> see if there are double numbers or triple numbers. In my example here
> there are two 1 and two 2 ... Then I do something with this result and
> I read in the next 15 numbers starting with 2 ...
>
>
> 1 2 3 4 1 5 6 7 8 9 10 2 11 12 13 14 15 16 17
>   ^                               ^
>
> next step we read in starting with 3
>
>
> 1 2 3 4 1 5 6 7 8 9 10 2 11 12 13 14 15 16 17
>     ^                                ^

So you have a sliding window of 15 lines, and you always want to know
how often a number occurs within that window? That is you want an output
similar to this:

    line 1 - 15:
        1: 2
        2: 2
        3: 1
        4: 1
        5: 1
        6: 1
        7: 1
        8: 1
        9: 1
        10: 1
        11: 1
        12: 1
        13: 1
    line 2 - 16:
        1: 1
        2: 2
        3: 1
        4: 1
        5: 1
        6: 1
        7: 1
        8: 1
        9: 1
        10: 1
        11: 1
        12: 1
        13: 1
        14: 1
    line 3 - 17:
        1: 1
        2: 1
        3: 1
        4: 1
        5: 1
        6: 1
        7: 1
        8: 1
        9: 1
        10: 1
        11: 1
        12: 1
        13: 1
        14: 1
        15: 1

One way to achieve this is to keep the window in an array. Add new lines
with push, and remove old lines with shift. For each line added,
increment the count of the corresponding number. For each line removed,
decrement it. The counts can be kept in an array or a hash.

        hp


------------------------------

Date: Sun, 15 Feb 2009 12:51:16 -0800 (PST)
From: Marek <mstep@podiuminternational.org>
Subject: Re: restrict a hash to 15 pairs and iterate over it
Message-Id: <6f4f9227-4984-4dd0-bc3c-33f441b806c8@f20g2000yqg.googlegroups.com>



Wow! I am impressed! I would never have found these solutions on
myself!

Special thanx to Tad; so short and elegant! I love these lines:

if ( keys %lines == $size )

and

$nums{$_}++ for values %lines;

and this one is my favourite :-)

delete $lines{ $line - $size + 1};

Also Xho's suggestion is really tricky! Thank you!

Good evening to all!


marek



------------------------------

Date: Mon, 16 Feb 2009 03:25:00 GMT
From: sln@netherlands.com
Subject: Re: restrict a hash to 15 pairs and iterate over it
Message-Id: <3bmhp49kfpvpkbh4mcs98dfg5n31hkb75m@4ax.com>

On Sat, 14 Feb 2009 23:51:34 -0800 (PST), Marek <mstep@podiuminternational.org> wrote:

>
>
>Hello all!
>
>
>I am still a beginner, so please be patient with me.
>
>I have a big file with numbers and dates like follows here:
>
>
>01.01.98
>31
>33
>14
>7
>35
>16
>20
>20
>13
>55
>1
>1
>7
>
>
>etc etc
>
>I need a complicate hash to know the occurrences of numbers in a scope
>of 15:
>
>We skip the dates, and we count the lines. The structure of my %hash
>looks like follows:
>
>($number{$line, $line, ...}) => $how_many_times
>
>In my example the 20 occurs in line 7 and 8 -> two times:
>
>20{7,8} => 2
>
>And we iterate over it, and keep only 15 numbers in the hash and count
>each time the occurrences of each number.
>
>Could somebody help me with this?
>
>
>Thank you in advance
>
>
>marek

A rolling Frame that tracks line's of occurances is not as easy as you think.
The concept is simple, the implementation is another thing altogether.
This would not be a problem to present in a beginner Perl class.
Its not actually Perl that would be a problem, its the implemtation of a rolling
frame and tracking of line numbers from a given criteria.

The below code is just a rudimentary framework to demonstrate the constructs that
would be necessary. You might need a hardened programmer with large application
experience to deal with rolling frames and data tracking.

Could this rough code be thinned out? Sure. It just demonstrates the concept, its
not production quality.

Btw, the frame size was set to 5 for the example, change it to 15 or whatever it is
your doing.

Well, good luck and have fun!
-sln

------------------------

# Frames.pl
# -------------------------
# Template:
# We assume a valid frame of 5 (not based on line count) This could be 15 or any number
# @Frame_Cache = (number, number, number, ...); ## 5 elements
# %Items = (number => [line,line,line], number => [line,line,line],...);


use strict;
use warnings;


my @Frame_Cache = ();
my %Items = ();
my ($cache_size, $lncount, $framesize) =  (0, 0, 5);

while (<DATA>)
{
	++$lncount;

	# Digits only, anything else is invalid
	/^\s*(\d+)\s*$/;
	next if (!$1);

	# Add item to frame cache
	   push @Frame_Cache, $1;

	# Add line number onto item array stack (in hash)
	   push @{$Items{$1}}, $lncount;

	   print "\nAdding $1 (line $lncount)\n";

	# Continue until full frame
	   ++$cache_size;
	   next if ($cache_size < $framesize);

	# First full frame, the roll starts on next one
	# Show Frame, do something with %Items
	   if ($cache_size == $framesize)
	   {
		PrintItems();
		next;
	   }

	# Frame is moving, take head off cache
	   my $item_number = shift @Frame_Cache;

	# Adjust lines going out of frame (all array's in hash).
	# Delete the item number line array if it is empty.

	   print "Taking $item_number off (line ".${$Items{$item_number}}[0].")\n";

	   my $line_going_out_of_frame = ${$Items{$item_number}}[0];
	   for my $nbr (keys %Items)
	   {
		shift @{$Items{$nbr}} if (${$Items{$nbr}}[0] <= $line_going_out_of_frame);
		delete	$Items{$nbr} if (!@{$Items{$nbr}});
	   }

	# Show Frame, do something with %Items
	PrintItems();
}

# You could print items down here if there is no full frame
# ...

# end of program ...


# This prints the items hash (could use Data::Dumper), but more importantly
# gives a template to access the data.
# When your through with debug printing, just comment the print part out.
# Process the data here, refactor this sub when done.
# No sub should access global data imho.
# -----------------
sub PrintItems
{
	print "Frame ".($cache_size-$framesize+1)." - $cache_size\n";
	for my $nbr (sort {$a<=>$b} keys %Items) {
		print "number = $nbr, on lines = [ @{$Items{$nbr}} ]\n";
	}
}

__DATA__

01.01.98
99
31
33
14
7
35
16
20
20
13
55
1
1
7
0
2
3
0
2
3
0
2
3
0
2
3
0
2
3
0


---------------
Output:


c:\temp>perl frames.pl

Adding 99 (line 3)

Adding 31 (line 4)

Adding 33 (line 5)

Adding 14 (line 6)

Adding 7 (line 7)
Frame 1 - 5
number = 7, on lines = [ 7 ]
number = 14, on lines = [ 6 ]
number = 31, on lines = [ 4 ]
number = 33, on lines = [ 5 ]
number = 99, on lines = [ 3 ]

Adding 35 (line 8)
Taking 99 off (line 3)
Frame 2 - 6
number = 7, on lines = [ 7 ]
number = 14, on lines = [ 6 ]
number = 31, on lines = [ 4 ]
number = 33, on lines = [ 5 ]
number = 35, on lines = [ 8 ]

Adding 16 (line 9)
Taking 31 off (line 4)
Frame 3 - 7
number = 7, on lines = [ 7 ]
number = 14, on lines = [ 6 ]
number = 16, on lines = [ 9 ]
number = 33, on lines = [ 5 ]
number = 35, on lines = [ 8 ]

Adding 20 (line 10)
Taking 33 off (line 5)
Frame 4 - 8
number = 7, on lines = [ 7 ]
number = 14, on lines = [ 6 ]
number = 16, on lines = [ 9 ]
number = 20, on lines = [ 10 ]
number = 35, on lines = [ 8 ]

Adding 20 (line 11)
Taking 14 off (line 6)
Frame 5 - 9
number = 7, on lines = [ 7 ]
number = 16, on lines = [ 9 ]
number = 20, on lines = [ 10 11 ]
number = 35, on lines = [ 8 ]

Adding 13 (line 12)
Taking 7 off (line 7)
Frame 6 - 10
number = 13, on lines = [ 12 ]
number = 16, on lines = [ 9 ]
number = 20, on lines = [ 10 11 ]
number = 35, on lines = [ 8 ]

Adding 55 (line 13)
Taking 35 off (line 8)
Frame 7 - 11
number = 13, on lines = [ 12 ]
number = 16, on lines = [ 9 ]
number = 20, on lines = [ 10 11 ]
number = 55, on lines = [ 13 ]

Adding 1 (line 14)
Taking 16 off (line 9)
Frame 8 - 12
number = 1, on lines = [ 14 ]
number = 13, on lines = [ 12 ]
number = 20, on lines = [ 10 11 ]
number = 55, on lines = [ 13 ]

Adding 1 (line 15)
Taking 20 off (line 10)
Frame 9 - 13
number = 1, on lines = [ 14 15 ]
number = 13, on lines = [ 12 ]
number = 20, on lines = [ 11 ]
number = 55, on lines = [ 13 ]

Adding 7 (line 16)
Taking 20 off (line 11)
Frame 10 - 14
number = 1, on lines = [ 14 15 ]
number = 7, on lines = [ 16 ]
number = 13, on lines = [ 12 ]
number = 55, on lines = [ 13 ]

Adding 2 (line 18)
Taking 13 off (line 12)
Frame 11 - 15
number = 1, on lines = [ 14 15 ]
number = 2, on lines = [ 18 ]
number = 7, on lines = [ 16 ]
number = 55, on lines = [ 13 ]

Adding 3 (line 19)
Taking 55 off (line 13)
Frame 12 - 16
number = 1, on lines = [ 14 15 ]
number = 2, on lines = [ 18 ]
number = 3, on lines = [ 19 ]
number = 7, on lines = [ 16 ]

Adding 2 (line 21)
Taking 1 off (line 14)
Frame 13 - 17
number = 1, on lines = [ 15 ]
number = 2, on lines = [ 18 21 ]
number = 3, on lines = [ 19 ]
number = 7, on lines = [ 16 ]

Adding 3 (line 22)
Taking 1 off (line 15)
Frame 14 - 18
number = 2, on lines = [ 18 21 ]
number = 3, on lines = [ 19 22 ]
number = 7, on lines = [ 16 ]

Adding 2 (line 24)
Taking 7 off (line 16)
Frame 15 - 19
number = 2, on lines = [ 18 21 24 ]
number = 3, on lines = [ 19 22 ]

Adding 3 (line 25)
Taking 2 off (line 18)
Frame 16 - 20
number = 2, on lines = [ 21 24 ]
number = 3, on lines = [ 19 22 25 ]

Adding 2 (line 27)
Taking 3 off (line 19)
Frame 17 - 21
number = 2, on lines = [ 21 24 27 ]
number = 3, on lines = [ 22 25 ]

Adding 3 (line 28)
Taking 2 off (line 21)
Frame 18 - 22
number = 2, on lines = [ 24 27 ]
number = 3, on lines = [ 22 25 28 ]

Adding 2 (line 30)
Taking 3 off (line 22)
Frame 19 - 23
number = 2, on lines = [ 24 27 30 ]
number = 3, on lines = [ 25 28 ]

Adding 3 (line 31)
Taking 2 off (line 24)
Frame 20 - 24
number = 2, on lines = [ 27 30 ]
number = 3, on lines = [ 25 28 31 ]

c:\temp>


------------------------------

Date: Mon, 16 Feb 2009 03:37:19 GMT
From: sln@netherlands.com
Subject: Re: restrict a hash to 15 pairs and iterate over it
Message-Id: <tmnhp4ls02b66o8gp3oosclbq61c3fmuha@4ax.com>

On Mon, 16 Feb 2009 03:25:00 GMT, sln@netherlands.com wrote:

>On Sat, 14 Feb 2009 23:51:34 -0800 (PST), Marek <mstep@podiuminternational.org> wrote:
>
>>
>>
>>Hello all!
>>
>>
>>I am still a beginner, so please be patient with me.
>>
>>I have a big file with numbers and dates like follows here:
>>
>>
>>01.01.98
>>31
>>33
>>14
>>7
>>35
>>16
>>20
>>20
>>13
>>55
>>1
>>1
>>7
>>
>>
>>etc etc
>>
>>I need a complicate hash to know the occurrences of numbers in a scope
>>of 15:
>>
>>We skip the dates, and we count the lines. The structure of my %hash
>>looks like follows:
>>
>>($number{$line, $line, ...}) => $how_many_times
>>
>>In my example the 20 occurs in line 7 and 8 -> two times:
>>
>>20{7,8} => 2
>>
>>And we iterate over it, and keep only 15 numbers in the hash and count
>>each time the occurrences of each number.
>>
>>Could somebody help me with this?
>>
>>
>>Thank you in advance
>>
>>
>>marek
>
>A rolling Frame that tracks line's of occurances is not as easy as you think.
>The concept is simple, the implementation is another thing altogether.
>This would not be a problem to present in a beginner Perl class.
>Its not actually Perl that would be a problem, its the implemtation of a rolling
>frame and tracking of line numbers from a given criteria.
>
>The below code is just a rudimentary framework to demonstrate the constructs that
>would be necessary. You might need a hardened programmer with large application
>experience to deal with rolling frames and data tracking.
>
>Could this rough code be thinned out? Sure. It just demonstrates the concept, its
>not production quality.
>
>Btw, the frame size was set to 5 for the example, change it to 15 or whatever it is
>your doing.
>
>Well, good luck and have fun!
>-sln
>
>------------------------
>
># Frames.pl
># -------------------------
># Template:
># We assume a valid frame of 5 (not based on line count) This could be 15 or any number
># @Frame_Cache = (number, number, number, ...); ## 5 elements
># %Items = (number => [line,line,line], number => [line,line,line],...);
>
>
[snip]
>	# Digits only, anything else is invalid
>	/^\s*(\d+)\s*$/;
>	next if (!$1);
                ^^^^^
        next if (!defined $1)

Oops. I always say 'check your work'. Gotcha on me!

-sln


------------------------------

Date: Sun, 15 Feb 2009 21:54:38 -0700
From: Larry Gates <larry@example.invalid>
Subject: Re: time and place of satellite coincidence
Message-Id: <1hd2ru85ouoba.16kxgk6chj5d9$.dlg@40tude.net>

On Fri, 13 Feb 2009 23:42:26 -0600, Tad J McClellan wrote:

> #!/usr/bin/perl
> use warnings;
> use strict;
> use HTML::TreeBuilder;
> use LWP::Simple;
> 
> my $site_url = 'http://www.fourmilab.ch/cgi-bin/Yoursky';
> my $url_args = 'z=1&lat=35.0836&ns=North&lon=106.651&ew=West';
> my $t = get "$site_url?$url_args" || "Problem";
> 
> my $tree = HTML::TreeBuilder->new_from_content($t);
> 
> foreach my $elem ( $tree->look_down('_tag', 'center') ) {
>     print $elem->as_text(), "\n";

I try to balance my posts between things that have worked and things that
haven't, and I think I'm at the tipping point now.

I get output I like for part of it, but I can't extend the methods to reach
a solution.  This version was working until I tried to incorporate stuff
from HTML::Element, and then I get the complaints from perl.exe:

#!/usr/bin/perl
use warnings;
use strict;
use HTML::TreeBuilder;
use LWP::Simple;



my $site_url = 'http://www.fourmilab.ch/cgi-bin/Yoursky';
my $url_args = 'z=1&lat=35.0836&ns=North&lon=106.651&ew=West';
my $t = get "$site_url?$url_args" || "Problem";

my $tree = HTML::TreeBuilder->new_from_content($t);

foreach my $elem ( $tree->look_down('_tag', 'center') ) {

    print "%%\n", $elem;
    print $elem->as_text(), "\n";
}

foreach my $elem ( $tree->find_by_attribute('name', 'jd') ) {
print "****";
    print $elem->attr('value'), "\n";
}

    use HTML::Element;
    $a = HTML::Element->new('a', href => 'http://www.perl.com/');
    $a->push_content("The Perl Homepage");

    $tag = $a->tag;
    print "$tag starts out as:",  $a->starttag, "\n";
    print "$tag ends as:",  $a->endtag, "\n";
    print "$tag\'s href attribute is: ", $a->attr('href'), "\n";

    $links_r = $a->extract_links();
    print "Hey, I found ", scalar(@$links_r), " links.\n";

    print "And that, as HTML, is: ", $a->as_HTML, "\n";
    $a = $a->delete;



# perl index6.pl

#end script; show dump of dos windwo

C:\MinGW\source>perl index6.pl
%%
HTML::Element=HASH(0x1acf1d0)Sky above 35░5'N 106░39'3"W at Mon 2009 Feb 16
4:32
 UTC
%%
HTML::Element=HASH(0x1af74f4)Explain symbols in the map.Click in map to aim
tele
scope.View horizon at this observing site.
%%
HTML::Element=HASH(0x1af78b4)Explain controls in the following panel.Date
and Ti
me Now Universal time: Julian day:Observing Site Latitude:  North South
Longitud
e:  East WestSet for nearby cityDisplay Options Ecliptic and equator Moon
and pl
anets Deep sky objects of magnitude  and brighter Constellations: áááááááá
Outli
nes áááááááá Names aligned with horizon? áááááááá BoundariesStars:
ááááááááShow
stars brighter than magnitude  áááááááá Names for magnitude  and brighter
ááááá
ááá Bayer/Flamsteed codes for mag.  and brighter  Invert North and
SouthImage si
ze:  pixels Colour scheme: ColourBlack on white backgroundWhite on black
backgro
undNight vision (red)Asteroid andComet Tracking Paste orbital elements
below:  E
cho elements

%%
Wide character in print at index6.pl line 18.
HTML::Element=HASH(0x1b08cd0) RightAscensionDeclinationDistance(AU)From
35┬░5'N
 106°39'3"W:AltitudeAzimuthSun21h 59m 16s−12°
17.8'0.988−45.719110.332SetM
ercury20h 15m 33s−19° 47.0'1.017−69.097132.831SetVenus0h 28m 22s+7°
18.9'0
 .451−4.407102.133SetMoon15h 2m 48s−22° 37.9'62.5
ER−35.118−85.358SetMar
s20h 46m 38s−19° 1.2'2.309−63.450120.976SetJupiter20h 49m 21s−18°
16.0'6
 .029−62.468121.250SetSaturn11h 26m 37s+6°
1.6'8.46024.724−79.502UpUranus23h
 28m 52s−4° 9.4'21.000−23.167101.784SetNeptune21h 46m 2s−13°
50.1'31.019
−49.201111.502SetPluto18h 10m 47s−17°
43.5'32.153−68.083−138.502Set
****2454878.68916

C:\MinGW\source> perl index6.pl
Global symbol "$tag" requires explicit package name at index6.pl line 30.
Global symbol "$tag" requires explicit package name at index6.pl line 31.
Global symbol "$tag" requires explicit package name at index6.pl line 32.
Global symbol "$tag" requires explicit package name at index6.pl line 33.
Global symbol "$links_r" requires explicit package name at index6.pl line
35.
Global symbol "$links_r" requires explicit package name at index6.pl line
36.
Execution of index6.pl aborted due to compilation errors.

C:\MinGW\source> 

Outside of basically not knowing my I failed here, I have 2 other
questions.

q1)  I didn't see look_down as a method in HTML::TreeBuilder, HTML::Element
and one other HTML::Something that I looked for it.  Where do I find a
definition of the look_down method?

q2)  I *still* have problems with control structures in perl.  Why does
this not print the index variable for the loop?

#!/usr/bin/perl
use warnings;
use strict;
use HTML::TreeBuilder;
use LWP::Simple;

my $site_url = 'http://www.fourmilab.ch/cgi-bin/Yoursky';
my $url_args = 'z=1&lat=35.0836&ns=North&lon=106.651&ew=West';
my $t = get "$site_url?$url_args" || "Problem";

my $tree = HTML::TreeBuilder->new_from_content($t);

foreach my $elem ( $tree->look_down('_tag', 'center') ) {

    print "%%\n", $elem;
    print $elem->as_text(), "\n";
}

# perl index3.pl

Thanks for your comment.
-- 
larry gates

May you do Good Magic with Perl.
             -- Larry Wall's blessing


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 2211
***************************************


home help back first fref pref prev next nref lref last post