[27042] in Perl-Users-Digest
Perl-Users Digest, Issue: 8954 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Feb 14 21:05:36 2006
Date: Tue, 14 Feb 2006 18:05:05 -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 Tue, 14 Feb 2006 Volume: 10 Number: 8954
Today's topics:
Re: arrays tripping me up <matthew.garrish@sympatico.ca>
Re: arrays tripping me up <jgibson@mail.arc.nasa.gov>
Re: Arrays <FJRussonc@earthlink.net>
Re: Arrays robic0
Re: Looking for script setting a pixel marker in an ima <rvtol+news@isolution.nl>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 14 Feb 2006 17:53:48 -0500
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: arrays tripping me up
Message-Id: <YbtIf.25913$T35.398649@news20.bellglobal.com>
"Jim Gibson" <jgibson@mail.arc.nasa.gov> wrote in message
news:140220061245553470%jgibson@mail.arc.nasa.gov...
> In article <43f2374f$0$23279$db0fefd9@news.zen.co.uk>, Obantec Support
> <usenet@NOSPAM.obantec.net> wrote:
>
>> Hi
>>
>> i am reading data from a mysql source and i need to on certain lines when
>> a
>> match occurs read the next few lines and output if not blank, then
>> continue
>> the loop.
>>
>> all lines have text except for the odd blank line.
>>
>> i am using
>>
>> $nic_nam="ShowMe";
>>
>> foreach $i (@result)
>> {
>>
>> if ($i=~ m/$nic_nam/) {
>>
>> print "Found SHowMe<br>\n";
>>
>> #now if this matches out put the next few lines while not blank from
>> @results
>> #max is only ever 7 lines.
>>
>> }
>> }
>
> Use a C-style for loop (untested):
>
> for( my $i = 0; $i < @result; $i++ ) {
for my $i (0..$#result) {
This is a Perl group after all... : )
Matt
------------------------------
Date: Tue, 14 Feb 2006 17:27:51 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: arrays tripping me up
Message-Id: <140220061727512245%jgibson@mail.arc.nasa.gov>
In article <YbtIf.25913$T35.398649@news20.bellglobal.com>, Matt Garrish
<matthew.garrish@sympatico.ca> wrote:
> "Jim Gibson" <jgibson@mail.arc.nasa.gov> wrote in message
> news:140220061245553470%jgibson@mail.arc.nasa.gov...
> > In article <43f2374f$0$23279$db0fefd9@news.zen.co.uk>, Obantec Support
> > <usenet@NOSPAM.obantec.net> wrote:
[problem processing array]
> >
> > Use a C-style for loop (untested):
> >
> > for( my $i = 0; $i < @result; $i++ ) {
>
[snipped snippet un-snipped:]
> for my $i (0..$#result) {
> if( $result[$i] =~ /$nic_nam/ ) {
> print "Match found: $result[$i];
> while( $result[$i+1] != /^\s*$/ ) {
> print $result[++$i];
> }
> }
> }
>
> This is a Perl group after all... : )
Matt:
Did you try it? Your suggestion doesn't work. The OP wanted to print
non-blank lines occuring in the array after any element matching a
regex was encountered.
I am afraid you have run afoul of the
knee-jerk-reaction-to-C-style-loops syndrome (or what some native
Choctaw speakers might describe as "cargo-cult programming" :^) that
occurs frequently in this group. Fortunately, the inventor of Perl saw
fit to include C-style loops in the language, perhaps for a good
reason.
Jim 45% cat matt.pl
#!/usr/local/bin/perl
use strict;
use warnings;
my @result = ( 1 .. 10 );
print "Matt's buggy loop:\n";
for my $i ( 0 .. $#result ) {
print "$result[$i]\n";
$i++ if( $i % 2 == 0 );
}
print "\nJim's correct loop:\n";
for( my $j = 0; $j < @result; $j++ ) {
print "$result[$j]\n";
$j++ if( $j % 2 == 0 );
}
Jim 46% perl matt.pl
Matt's buggy loop:
1
2
3
4
5
6
7
8
9
10
Jim's correct loop:
1
3
5
7
9
Jim 47%
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
------------------------------
Date: Tue, 14 Feb 2006 23:30:52 GMT
From: "Frank J. Russo" <FJRussonc@earthlink.net>
Subject: Re: Arrays
Message-Id: <MKtIf.11464$Nv2.10253@newsread1.news.atl.earthlink.net>
Jim, Thanks, that is the most inspired view and what I was looking for.
And I appreciate you not flaming me.
I have tried a number of thing but not all of them worked. This is the
better way to get to where I want to be.
Followup question: I am feverishly going thru my books / notes / and
documentation but have not located how do I write to a file and read in from
a file a HASH?
I know how to use files I am doing that now but not with a hash.
Thanks again
Frank
"Jim Gibson" <jgibson@mail.arc.nasa.gov> wrote in message
news:140220061212061768%jgibson@mail.arc.nasa.gov...
> In article <9lcIf.15246$rH5.2635@newsread2.news.atl.earthlink.net>,
> Frank J. Russo <FJRussonc@earthlink.net> wrote:
>
>> ? Does Perl handle multilevel (dimension) arrays? I am assuming it does.
>>
>> I am attempting to improve the way data from my program is stored and
>> recovered.
>>
>> If I was working in any other language I would define an array e.g.
>> Calendar(12,5,1).
>>
>> The data I have is 12 months (Jan - Dec) each month can have 4 or 5 dates
>> attached and each date has a single note (string).
>>
>> How would you organize or define this?
>
> I would use a hash. If you only have one note per day, then you don't
> really have a multi-level array problem. I would use a standard date
> format, e.g. yyyy-mm-dd as an index into a hash:
>
> my %notes;
> $notes{'2006-02-14'} = 'Valentines Day';
>
> This might be more efficient than nested array references, particularly
> if you have sparse data with only 4-5 entries per month.
>
> --
> Jim Gibson
>
> Posted Via Usenet.com Premium Usenet Newsgroup Services
> ----------------------------------------------------------
> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
> ----------------------------------------------------------
> http://www.usenet.com
------------------------------
Date: Tue, 14 Feb 2006 16:27:44 -0800
From: robic0
Subject: Re: Arrays
Message-Id: <m6t4v1te75n8gt2bmrg2e1bknlkmj745gr@4ax.com>
On Tue, 14 Feb 2006 03:43:01 GMT, "Frank J. Russo" <FJRussonc@earthlink.net> wrote:
>? Does Perl handle multilevel (dimension) arrays? I am assuming it does.
>
>I am attempting to improve the way data from my program is stored and
>recovered.
>
>If I was working in any other language I would define an array e.g.
>Calendar(12,5,1).
>
>The data I have is 12 months (Jan - Dec) each month can have 4 or 5 dates
>attached and each date has a single note (string).
>
>How would you organize or define this?
>
>Frank
>
You describe something more suitable to record processing,
non-homogenous 3-dimentional arrays always do.
Perhaps something like this...
my @calendar = ();
# calendar array of records
@calendar =
(
# calendar record (structure)
[ 4, # (element 0) month
22, # (element 1) day
2006, # (element 2) year
"this is an important date", # (element 3) # notes
1, # dirty flag
],
# more records
# [],[],[]....
);
# dynamically allocate and add records to calendar
# read records from file or from command line (use split)
my $record = [2,10,2006,'some notes on this date', 1]; # new set dirty flag
push @calendar, $record;
# do some calendar record query
# do some sorts, get all activity on the 22nd of each month for 2006
@calendar = sort {$a->[2] <=> $b->[2]} @calendar;
@calendar = sort {$a->[1] <=> $b->[1]} @calendar;
print "Activity for the 22nd of each month in 2006:\n";
for (@calendar) {
last unless ($_->[2] == 2006);
print "$_->[0],$_->[1],$_->[2]: $_->[3]\n" if ($_->[1] == 22);
}
# append new records to end of file
# open DBCAL for append (assumed, and tab delimiters).
# its assumed its a simple calendar without fixed sizes
# for the records, otherwise its possible to seek and modify
# file data, etc...
for (@calendar) {
if ($_->[4]) {
print DBCAL "$->[0],\t$->[1],\t$->[2],\t$->[3]\n";
}
# if you wish to use whitespace in the context of your record notes, you
# may want to use a unprintable character (or series of such) for
# delimeters. Its easier to reserve the \n as a line delimeter, though
# can work around this several ways.
------------------------------
Date: Wed, 15 Feb 2006 00:56:36 +0100
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Looking for script setting a pixel marker in an image
Message-Id: <dstuff.14g.1@news.isolution.nl>
GHL schreef:
> I am looking for a script, which is able to set a pixel marker in an
> image file at position x|y (whereas xmax and ymax are the maximum
> width and height of that image in pixels).
> The color value shall be handed over with constant col.
>
> For reason of simplicity the filename does not vary and should be
> filename.ext and the path can be define hardcoded in the script.
>
> It would be of great help if the pointsize of the pixel marker could
> be defined within the script using a numeric value, thus 2 would mean
> the marker is 2x2 pixels in size.
>
> Seems simple but I didn't find a solution to this problem (and my Perl
> knowledge is only very basic...).
>
> +++
>
> Does anyone of you know a script, which can do just this (or even
> more) or can remember where he has seen any (any specific link
> appreciated !) ?
>
> Thanks so much in advance !
Try GD::Simple, http://search.cpan.org/~lds/GD/GD/Simple.pm
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
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 V10 Issue 8954
***************************************