[29069] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 313 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Apr 6 14:10:07 2007

Date: Fri, 6 Apr 2007 11:09:11 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 6 Apr 2007     Volume: 11 Number: 313

Today's topics:
    Re: Creating Word Documents with Perl <spamtrap@dot-app.org>
    Re: Creating Word Documents with Perl <bik.mido@tiscalinet.it>
    Re: Creating Word Documents with Perl <amerar@iwc.net>
    Re: Creating Word Documents with Perl <bik.mido@tiscalinet.it>
    Re: Creating Word Documents with Perl <sbryce@scottbryce.com>
        Hash/array reference question <bcc@nospam.net>
    Re: Hash/array reference question <mritty@gmail.com>
    Re: Hash/array reference question <bcc@nospam.net>
    Re: Hash/array reference question <bik.mido@tiscalinet.it>
        removing intermediate elements from an array, keeping f <jbl02NO@SPAMhotmail.com>
    Re: removing intermediate elements from an array, keepi <mritty@gmail.com>
    Re: removing intermediate elements from an array, keepi <jurgenex@hotmail.com>
    Re: removing intermediate elements from an array, keepi <someone@example.com>
    Re: removing intermediate elements from an array, keepi <someone@example.com>
        Traversing a hash with array refs as keys? <bcc@nospam.net>
    Re: Traversing a hash with array refs as keys? <someone@example.com>
    Re: Traversing a hash with array refs as keys? <jurgenex@hotmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 06 Apr 2007 11:20:06 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Creating Word Documents with Perl
Message-Id: <m2bqi1fs2h.fsf@local.wv-www.com>

"amerar@iwc.net" <amerar@iwc.net> writes:

> I need to create a Microsoft Word document on Perl.   This will be on
> a Linux Box.

Have a look at RTF::Writer.

Word associates itself with .rtf files when installed in Windows, so your
users will see the "Word document" icon. If they have filename extensions
hidden, they won't even notice it's not a .doc file - it'll Just Work.

sherm--

-- 
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net


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

Date: Fri, 06 Apr 2007 17:32:08 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Creating Word Documents with Perl
Message-Id: <sppc13ht8b9qk8oq9a3u81e2heflk02afg@4ax.com>

On 6 Apr 2007 07:49:51 -0700, "amerar@iwc.net" <amerar@iwc.net> wrote:

>I need to create a Microsoft Word document on Perl.   This will be on
>a Linux Box.
>
>The actual process is, I need the script to query a MySQL database and
>get the data for the Word document, create the document, and email it.
>
>The first and last steps are easy.  I am lost on how to create the
>Word document.  And, as I mentioned, this is on a Linux box, not a
>Windows box.....

I believe that there are ways to do this, with Windows-specific
modules. Unfortunately this path won't work from Linux, unless by
means of some arcane trickery, but I doubt that. However is there any
compelling reason why the documents *must* be in M$ Word? Are they
meant to be edited? If not, you may consider e.g. nicely typeset
pdf's...


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: 6 Apr 2007 09:21:42 -0700
From: "amerar@iwc.net" <amerar@iwc.net>
Subject: Re: Creating Word Documents with Perl
Message-Id: <1175876502.016777.313630@n59g2000hsh.googlegroups.com>

On Apr 6, 10:32 am, Michele Dondi <bik.m...@tiscalinet.it> wrote:
> On 6 Apr 2007 07:49:51 -0700, "ame...@iwc.net" <ame...@iwc.net> wrote:
>
> >I need to create a Microsoft Word document on Perl.   This will be on
> >a Linux Box.
>
> >The actual process is, I need the script to query a MySQL database and
> >get the data for the Word document, create the document, and email it.
>
> >The first and last steps are easy.  I am lost on how to create the
> >Word document.  And, as I mentioned, this is on a Linux box, not a
> >Windows box.....
>
> I believe that there are ways to do this, with Windows-specific
> modules. Unfortunately this path won't work from Linux, unless by
> means of some arcane trickery, but I doubt that. However is there any
> compelling reason why the documents *must* be in M$ Word? Are they
> meant to be edited? If not, you may consider e.g. nicely typeset
> pdf's...
>
> Michele
> --
> {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
> (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
> .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
> 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Basically what need is to prepare a nicely formatted document that
will be emailed.  So, I'll need to create a document that will be an
email attachment, and, it needs to have different size fonts and
such.  I can do this with an HTML document........

Perhaps that is the way to do it?  Maybe create an HTML document, and
have them open it in IE or Firefox and print it???




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

Date: Fri, 06 Apr 2007 19:41:36 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Creating Word Documents with Perl
Message-Id: <t11d13lj0cb3bunto02t98ri98v8jl13c4@4ax.com>

On 6 Apr 2007 09:21:42 -0700, "amerar@iwc.net" <amerar@iwc.net> wrote:

>> --

Please don't quote the full message you're replying to. In particular,
not the sig. Unless you want to discuss it, that it. But then you
generally don't. Well except when answering a post by Abigail, of
course!

>Basically what need is to prepare a nicely formatted document that
>will be emailed.  So, I'll need to create a document that will be an
>email attachment, and, it needs to have different size fonts and
>such.  I can do this with an HTML document........

If that is the only reason, then most definitely I would choose quite
about anything but word. Many people even in Redmond's realm do not
have it installed!

>Perhaps that is the way to do it?  Maybe create an HTML document, and
>have them open it in IE or Firefox and print it???

Well, that is an option. Many people under Windows use clients that
can also directly show HTML formatted mail. If you do so, then it will
be very gentle of you to also include a pure text part, for those who
don't. Or they will hate you. If you ask me, I would go the (pdflatex
generated) pdf route as hinted above, probably.


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Fri, 06 Apr 2007 11:50:02 -0600
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: Creating Word Documents with Perl
Message-Id: <abWdnTJrTv-gFYvbnZ2dnUVZ_oernZ2d@comcast.com>

amerar@iwc.net wrote:

> Basically what need is to prepare a nicely formatted document that
> will be emailed.  So, I'll need to create a document that will be an
> email attachment, and, it needs to have different size fonts and
> such.  I can do this with an HTML document........

I'd go with PDF. You can take your question over to comp.text.pdf and 
see if they can give you some pointers.

> Perhaps that is the way to do it?  Maybe create an HTML document, and
> have them open it in IE or Firefox and print it???

It depends on how important the page formatting is.


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

Date: Fri, 06 Apr 2007 16:47:48 GMT
From: Bryan <bcc@nospam.net>
Subject: Hash/array reference question
Message-Id: <UQuRh.6295$Kd3.3711@newssvr27.news.prodigy.net>

If I have a hash reference, and want to assign a reference to a snapshot 
of a changing array, what is the correct syntax?

$hash_ref->{'key'} = @array;
Obviously doesn't work

$hash_ref->{'key'} = \@array;
Doesn't work since my array is changed then reused for different keys, 
the values get hosed.

my @tmp_array = @array;
$hash_ref->{'key'} = \@tmp_array;
Works as I want, but now I have an extra line of code.

Is that really necessary?  Seems a bit clumsy to have to make a local 
copy explicitly like that...

Thanks,
B


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

Date: 6 Apr 2007 09:58:07 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Hash/array reference question
Message-Id: <1175878687.654544.29320@e65g2000hsc.googlegroups.com>

On Apr 6, 12:47 pm, Bryan <b...@nospam.net> wrote:
> If I have a hash reference, and want to assign a reference to a snapshot
> of a changing array, what is the correct syntax?
>
> $hash_ref->{'key'} = @array;
> Obviously doesn't work
>
> $hash_ref->{'key'} = \@array;
> Doesn't work since my array is changed then reused for different keys,
> the values get hosed.
>
> my @tmp_array = @array;
> $hash_ref->{'key'} = \@tmp_array;
> Works as I want, but now I have an extra line of code.
>
> Is that really necessary?  Seems a bit clumsy to have to make a
> local copy explicitly like that...

Create a reference to a new anonymous array, and populate that array
with the current values of @array:

$hash_ref->{key} = [ @array ];

Paul Lalli



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

Date: Fri, 06 Apr 2007 17:11:31 GMT
From: Bryan <bcc@nospam.net>
Subject: Re: Hash/array reference question
Message-Id: <7bvRh.6300$Kd3.5239@newssvr27.news.prodigy.net>

Paul Lalli wrote:
> On Apr 6, 12:47 pm, Bryan <b...@nospam.net> wrote:
>> If I have a hash reference, and want to assign a reference to a snapshot
>> of a changing array, what is the correct syntax?
>>
>> $hash_ref->{'key'} = @array;
>> Obviously doesn't work
>>
>> $hash_ref->{'key'} = \@array;
>> Doesn't work since my array is changed then reused for different keys,
>> the values get hosed.
>>
>> my @tmp_array = @array;
>> $hash_ref->{'key'} = \@tmp_array;
>> Works as I want, but now I have an extra line of code.
>>
>> Is that really necessary?  Seems a bit clumsy to have to make a
>> local copy explicitly like that...
> 
> Create a reference to a new anonymous array, and populate that array
> with the current values of @array:
> 
> $hash_ref->{key} = [ @array ];
> 
> Paul Lalli
> 

Ah, cool, thanks.

B


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

Date: Fri, 06 Apr 2007 19:46:00 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Hash/array reference question
Message-Id: <mp1d139jugekp6pjkfkl0ovnudfeg0t6kv@4ax.com>

On Fri, 06 Apr 2007 16:47:48 GMT, Bryan <bcc@nospam.net> wrote:

>my @tmp_array = @array;
>$hash_ref->{'key'} = \@tmp_array;
>Works as I want, but now I have an extra line of code.

  $hash_ref->{'key'} = [@array];


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Fri, 06 Apr 2007 11:22:15 -0500
From: jbl <jbl02NO@SPAMhotmail.com>
Subject: removing intermediate elements from an array, keeping first & last only
Message-Id: <0fpc13d13d9bqo6qm0qvjoof513irt7prc@4ax.com>


I am reading in a file in paragraph mode (sample DATA included here)
and creating arrays that may contain 2, 3 or 4 elements. I am afraid
the soultion is probably a lot more simple than I am trying to make
it. I apoligize for the amount of data but it takes that much to give
it a good test. There are about 50,000 lines (10,000+ arrays) in a
file for a 1 hour TV show.

I only want to keep the first and last elements, clearing the
intermediate elements.

array element number [0]      00:00:08.500
array element number [1]      WE

array element number [0]      00:00:10.333
array element number [1]      WE INVITE YOU TO VISIT OUR 
array element number [2]      WEBSITE ANYTIME OF THE DAY OR

array element number [0]      00:00:12.667
array element number [1]      WE INVITE YOU TO VISIT OUR 
array element number [2]      WEBSITE ANYTIME OF THE DAY OR 
array element number [3]      NI

Wanted results

array element number [0]      00:00:08.500
array element number [1]      WE

array element number [0]      00:00:10.333
array element number [1]       
array element number [2]      WEBSITE ANYTIME OF THE DAY OR

array element number [0]      00:00:12.667
array element number [1]      
array element number [2]       
array element number [3]      NI

So far my script only  reads the data and does some formating, and
after creating the array outputs a little test information (to be
removed) to the screen. Thanks for any suggestions.   jbl

#!/usr/bin/perl
use warnings;
# this file is to create readable text from a .srt file
use strict;
use Math::Round;
open OUT, ">VTS_01_1.garbage.txt"
         || die "Couldn't open file, $!";
{
local $/ = "\n\n";   #double blank lines delimit paragraphs
while (<DATA>)
{
   #remove the group number
   s/^\d+\s+\n//;
   #remove the first time delimiter
   s/\d+:\d+:\d+,\d+\s*-+>\s+//;
   #change the comma to a decimal
   s/(\d+:\d+:\d+),(\d+)/$1.$2/;
#****************comparison array here****************
# keep first & last elements, clear intermediate elements
my $counter = "0";
my @lines = split(/\n/, $_);

# $lines[0] & $lines[1] will always be there, no need to chk for them
# if array element[3] contains text
if($lines[3])
{
   print "element 3 (line 4) is present\n";

# if array element [2] contains text but not array element [3]

}elsif ($lines[2] and !$lines[3])
{
   print "element 2 is present but not element 3\n\n";   
}
foreach (@lines) {
         if (!$lines[$counter])
      {
         print "array element ",$counter," contains text\n\n";
      }
   print "array emement ",$counter," = ",$_,"\n";
   $counter++;
}
#*****************************************************

print "previous array had ",scalar @lines," elements the last element
number was ",$#lines,"\n\n";
}  # end while loop
}
close OUT;

__DATA__
1       
00:00:08,467 --> 00:00:08,500
WE

2       
00:00:08,500 --> 00:00:08,533
WE I

3       
00:00:08,533 --> 00:00:08,567
WE INV

4       
00:00:08,567 --> 00:00:08,600
WE INVIT

5       
00:00:08,600 --> 00:00:08,633
WE INVITE 

6       
00:00:08,633 --> 00:00:08,667
WE INVITE YO

7       
00:00:08,667 --> 00:00:09,166
WE INVITE YOU 

8       
00:00:09,166 --> 00:00:09,200
WE INVITE YOU TO

9       
00:00:09,200 --> 00:00:09,233
WE INVITE YOU TO V

10      
00:00:09,233 --> 00:00:09,266
WE INVITE YOU TO VIS

11      
00:00:09,266 --> 00:00:09,300
WE INVITE YOU TO VISIT

12      
00:00:09,300 --> 00:00:09,333
WE INVITE YOU TO VISIT O

13      
00:00:09,333 --> 00:00:09,367
WE INVITE YOU TO VISIT OUR

14      
00:00:09,367 --> 00:00:09,967
WE INVITE YOU TO VISIT OUR 

15      
00:00:09,967 --> 00:00:10,166
WE INVITE YOU TO VISIT OUR 

16      
00:00:10,166 --> 00:00:10,200
WE INVITE YOU TO VISIT OUR 
WE

17      
00:00:10,200 --> 00:00:10,233
WE INVITE YOU TO VISIT OUR 
WEBS

18      
00:00:10,233 --> 00:00:10,266
WE INVITE YOU TO VISIT OUR 
WEBSIT

19      
00:00:10,266 --> 00:00:10,300
WE INVITE YOU TO VISIT OUR 
WEBSITE 

20      
00:00:10,300 --> 00:00:10,333
WE INVITE YOU TO VISIT OUR 
WEBSITE AN

21      
00:00:10,333 --> 00:00:10,367
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYT

22      
00:00:10,367 --> 00:00:10,400
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIM

23      
00:00:10,400 --> 00:00:10,567
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME 

24      
00:00:10,567 --> 00:00:10,600
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF

25      
00:00:10,600 --> 00:00:10,633
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF T

26      
00:00:10,633 --> 00:00:10,667
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE

27      
00:00:10,667 --> 00:00:10,700
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE D

28      
00:00:10,700 --> 00:00:10,734
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE DAY

29      
00:00:10,734 --> 00:00:10,767
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE DAY O

30      
00:00:10,767 --> 00:00:11,600
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE DAY OR 

31      
00:00:11,600 --> 00:00:11,800
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE DAY OR 

32      
00:00:11,800 --> 00:00:11,834
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE DAY OR 
NI

33      
00:00:11,834 --> 00:00:11,867
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE DAY OR 
NIGH

34      
00:00:11,867 --> 00:00:11,900
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE DAY OR 
NIGHT 

35      
00:00:11,900 --> 00:00:11,934
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE DAY OR 
NIGHT FO

36      
00:00:11,934 --> 00:00:11,967
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE DAY OR 
NIGHT FOR 

37      
00:00:11,967 --> 00:00:12,000
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE DAY OR 
NIGHT FOR TH

38      
00:00:12,000 --> 00:00:12,500
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE DAY OR 
NIGHT FOR THE 

39      
00:00:12,500 --> 00:00:12,533
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE DAY OR 
NIGHT FOR THE LA

40      
00:00:12,533 --> 00:00:12,567
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE DAY OR 
NIGHT FOR THE LATE

41      
00:00:12,567 --> 00:00:12,600
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE DAY OR 
NIGHT FOR THE LATEST

42      
00:00:12,600 --> 00:00:12,633
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE DAY OR 
NIGHT FOR THE LATEST B

43      
00:00:12,633 --> 00:00:12,667
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE DAY OR 
NIGHT FOR THE LATEST BRE

44      
00:00:12,667 --> 00:00:12,700
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE DAY OR 
NIGHT FOR THE LATEST BREAK

45      
00:00:12,700 --> 00:00:12,734
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE DAY OR 
NIGHT FOR THE LATEST BREAKIN

46      
00:00:12,734 --> 00:00:13,367
WE INVITE YOU TO VISIT OUR 
WEBSITE ANYTIME OF THE DAY OR 
NIGHT FOR THE LATEST BREAKING 

47      
00:00:13,367 --> 00:00:13,567
WEBSITE ANYTIME OF THE DAY OR 
NIGHT FOR THE LATEST BREAKING 

48      
00:00:13,567 --> 00:00:13,600
WEBSITE ANYTIME OF THE DAY OR 
NIGHT FOR THE LATEST BREAKING 
NE

49      
00:00:13,600 --> 00:00:13,633
WEBSITE ANYTIME OF THE DAY OR 
NIGHT FOR THE LATEST BREAKING 
NEWS


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

Date: 6 Apr 2007 10:05:21 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: removing intermediate elements from an array, keeping first & last only
Message-Id: <1175879121.893824.168390@n76g2000hsh.googlegroups.com>

On Apr 6, 12:22 pm, jbl <jbl0...@SPAMhotmail.com> wrote:
> I am reading in a file in paragraph mode (sample DATA included here)
> and creating arrays that may contain 2, 3 or 4 elements. I am afraid
> the soultion is probably a lot more simple than I am trying to make
> it. I apoligize for the amount of data but it takes that much to give
> it a good test. There are about 50,000 lines (10,000+ arrays) in a
> file for a 1 hour TV show.
>
> I only want to keep the first and last elements, clearing the
> intermediate elements.
>
> array element number [0]      00:00:08.500
> array element number [1]      WE
>
> array element number [0]      00:00:10.333
> array element number [1]      WE INVITE YOU TO VISIT OUR
> array element number [2]      WEBSITE ANYTIME OF THE DAY OR
>
> array element number [0]      00:00:12.667
> array element number [1]      WE INVITE YOU TO VISIT OUR
> array element number [2]      WEBSITE ANYTIME OF THE DAY OR
> array element number [3]      NI
>
> Wanted results
>
> array element number [0]      00:00:08.500
> array element number [1]      WE
>
> array element number [0]      00:00:10.333
> array element number [1]      
> array element number [2]      WEBSITE ANYTIME OF THE DAY OR
>
> array element number [0]      00:00:12.667
> array element number [1]      
> array element number [2]      
> array element number [3]      NI
>

I can't parse your code (snipped) from a glance, so I'll just give you
some answers to the questions you actually asked.

If you want to actually *remove* the inner array elements:
@array = @array[0,-1] unless @array < 3;
or
splice(@array, 1, @array - 2) unless @array < 3;

If you want to "blank" the inner array elemenets, so that the array's
size doesn't change, but all elements except the first and last become
the empty string:
@array[1..$#array-1] = ('' x @array - 2) unless @array < 3;
or
if (@array >= 3) { $_ = '' for @array[1..$#array-1] }

Taking that and applying it to your situation is left as an excercise
for you. . .

Paul Lalli



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

Date: Fri, 06 Apr 2007 17:23:20 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: removing intermediate elements from an array, keeping first & last only
Message-Id: <cmvRh.6$Rg2.5@trndny02>

jbl wrote:


Subject: removing intermediate elements from an array, keeping first & last 
only

> I am reading in a file in paragraph mode (sample DATA included here)
> and creating arrays that may contain 2, 3 or 4 elements. I am afraid
> the soultion is probably a lot more simple than I am trying to make
> it. I apoligize for the amount of data but it takes that much to give

Sorry, didn't dig through all that code.
If you want to grab just the first and last element of an array as indicated 
by your subject line then you can use splice() or even just a simple 
tow-element array slice:
    @foo = @arr[0, -1];

jue





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

Date: Fri, 06 Apr 2007 17:11:17 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: removing intermediate elements from an array, keeping first & last only
Message-Id: <VavRh.3129$hO2.2396@edtnps82>

jbl wrote:
> I am reading in a file in paragraph mode (sample DATA included here)
> and creating arrays that may contain 2, 3 or 4 elements. I am afraid
> the soultion is probably a lot more simple than I am trying to make
> it. I apoligize for the amount of data but it takes that much to give
> it a good test. There are about 50,000 lines (10,000+ arrays) in a
> file for a 1 hour TV show.
> 
> I only want to keep the first and last elements, clearing the
> intermediate elements.
> 
> array element number [0]      00:00:08.500
> array element number [1]      WE
> 
> array element number [0]      00:00:10.333
> array element number [1]      WE INVITE YOU TO VISIT OUR 
> array element number [2]      WEBSITE ANYTIME OF THE DAY OR
> 
> array element number [0]      00:00:12.667
> array element number [1]      WE INVITE YOU TO VISIT OUR 
> array element number [2]      WEBSITE ANYTIME OF THE DAY OR 
> array element number [3]      NI
> 
> Wanted results
> 
> array element number [0]      00:00:08.500
> array element number [1]      WE
> 
> array element number [0]      00:00:10.333
> array element number [1]       
> array element number [2]      WEBSITE ANYTIME OF THE DAY OR
> 
> array element number [0]      00:00:12.667
> array element number [1]      
> array element number [2]       
> array element number [3]      NI


my @lines = split /\n/;
my $first = shift @lines;
my $last  = pop   @lines;
@lines = ( $first, ( '' ) x @lines, $last );

Or:

my @lines = split /\n/;
splice @lines, 1, @lines - 2, ( '' ) x ( @lines - 2 );


Substitute '' for whatever value you would normally use to "clear" elements.


> open OUT, ">VTS_01_1.garbage.txt"
>          || die "Couldn't open file, $!";

That will never die because the precedence of || is too high.  You need to
either use parentheses:

open( OUT, ">VTS_01_1.garbage.txt" )
         || die "Couldn't open file, $!";

Or use the lower precedence or operator:

open OUT, ">VTS_01_1.garbage.txt"
         or die "Couldn't open file, $!";




John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall


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

Date: Fri, 06 Apr 2007 17:22:55 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: removing intermediate elements from an array, keeping first & last only
Message-Id: <PlvRh.44270$__3.30447@edtnps90>

Paul Lalli wrote:
> 
> If you want to actually *remove* the inner array elements:
> @array = @array[0,-1] unless @array < 3;
> or
> splice(@array, 1, @array - 2) unless @array < 3;
> 
> If you want to "blank" the inner array elemenets, so that the array's
> size doesn't change, but all elements except the first and last become
> the empty string:
> @array[1..$#array-1] = ('' x @array - 2) unless @array < 3;

$ perl -le'
    use Data::Dumper;
    @array = "a" .. "z";
    @new = ("" x @array - 2);
    print Dumper \@new;
'
$VAR1 = [
          -2
        ];




John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall


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

Date: Fri, 06 Apr 2007 17:32:16 GMT
From: Bryan <bcc@nospam.net>
Subject: Traversing a hash with array refs as keys?
Message-Id: <AuvRh.6302$Kd3.2923@newssvr27.news.prodigy.net>

Another one...

I have a structure of key value pairs, where the key is an array, and 
the value is a scalar.  I would have preferred to use the scalars as the 
keys, but there may be duplicates, whereas I know the arrays do not.

In passing in such a structure to a function by reference, how do I 
assign a local var to the array ref inside the hash ref?

I need to traverse over the thing:

while ((my $array_ref, my $value) = each(%$hash_ref)) {...}
Doesnt work, it thinks $array_ref is a string with "REF(0x1e17594)" as a 
value.

while((my @$array_ref...
Doesnt work, dereferencing error.

Im starting to think I am using the wrong structure, but that is really 
the structure of my data: 1-1 key-value relationships, where the array 
is the unique element and therefore the key.  Right?

Dunno. How do I get my array back?  I create it like so btw:
$hashref->{\[@key_array]} = $value;

Maybe that is messing me up.

Help?

B



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

Date: Fri, 06 Apr 2007 17:48:59 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Traversing a hash with array refs as keys?
Message-Id: <fKvRh.44279$__3.4589@edtnps90>

Bryan wrote:
> Another one...
> 
> I have a structure of key value pairs, where the key is an array, and
> the value is a scalar.  I would have preferred to use the scalars as the
> keys, but there may be duplicates, whereas I know the arrays do not.
> 
> In passing in such a structure to a function by reference, how do I
> assign a local var to the array ref inside the hash ref?
> 
> I need to traverse over the thing:
> 
> while ((my $array_ref, my $value) = each(%$hash_ref)) {...}
> Doesnt work, it thinks $array_ref is a string with "REF(0x1e17594)" as a
> value.

That is because hash keys *are* strings and whatever you use as a hash key
will be converted to a string.  Once a reference is converted to a string it
can not be converted back to a reference.


> while((my @$array_ref...
> Doesnt work, dereferencing error.
> 
> Im starting to think I am using the wrong structure, but that is really
> the structure of my data: 1-1 key-value relationships, where the array
> is the unique element and therefore the key.  Right?
> 
> Dunno. How do I get my array back?  I create it like so btw:
> $hashref->{\[@key_array]} = $value;

You are storing a reference to a reference to an array (an anonymous array is
already a reference.)

Perhaps either of these modules will do what you want:

http://search.cpan.org/~fxn/Hash-MultiKey-0.06/
http://search.cpan.org/~osfameron/Tie-Hash-StructKeyed-0.03/




John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall


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

Date: Fri, 06 Apr 2007 17:53:31 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Traversing a hash with array refs as keys?
Message-Id: <vOvRh.16$FC5.12@trndny06>

Bryan wrote:
> I have a structure of key value pairs, where the key is an array,

That is impossible. Hash keys can only be strings. Any other scalar value is 
converted into its string representation.

> the value is a scalar.  I would have preferred to use the scalars as
> the keys, but there may be duplicates, whereas I know the arrays do
> not.
> In passing in such a structure to a function by reference, how do I
> assign a local var to the array ref inside the hash ref?

So is your key an array or an array reference? Those two things are quite 
different.

> I need to traverse over the thing:
>
> while ((my $array_ref, my $value) = each(%$hash_ref)) {...}
> Doesnt work, it thinks $array_ref is a string with "REF(0x1e17594)"
> as a value.

Correct. As mentioned above keys must be strings.

> while((my @$array_ref...
> Doesnt work, dereferencing error.

Not surprising.

> Im starting to think I am using the wrong structure,

Quite possible.

> but that is
> really the structure of my data: 1-1 key-value relationships, where
> the array is the unique element and therefore the key.  Right?

This smells very much like an x-y problem. Do you actually use that hash as 
a hash, i.e. is there really a logical function, that maps your arrays to 
the value? Hashes are ideal for lookups. Do you really get some array from 
somewhere and frequently need to look up the associated value for that array 
in constant time?

If not then chances are a different data structure would be better suited 
for the job.

jue 




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

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 313
**************************************


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