[28753] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 10117 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jan 4 18:05:47 2007

Date: Thu, 4 Jan 2007 15:05:10 -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           Thu, 4 Jan 2007     Volume: 10 Number: 10117

Today's topics:
        CGI.pm - Upload multiple files using the same query par diegodelima.poa@gmail.com
    Re: CGI.pm - Upload multiple files using the same query usenet@DavidFilmer.com
    Re: CGI.pm - Upload multiple files using the same query <noreply@gunnar.cc>
        Coderefs and objects (Adam Flott)
    Re: Coderefs and objects <noreply@invalid.net>
    Re: Coderefs and objects (Adam Flott)
    Re: Coderefs and objects <noreply@invalid.net>
    Re: Coderefs and objects (Adam Flott)
        Elegant equivalent to this regex? <sherifffruitfly@gmail.com>
    Re: Elegant equivalent to this regex? usenet@DavidFilmer.com
    Re: Elegant equivalent to this regex? <sherifffruitfly@gmail.com>
    Re: Elegant equivalent to this regex? <glex_no-spam@qwest-spam-no.invalid>
    Re: Elegant equivalent to this regex? <wahab@chemie.uni-halle.de>
    Re: Elegant equivalent to this regex? <sherifffruitfly@gmail.com>
    Re: Iterate through words in a string <noreply@gunnar.cc>
    Re: Iterate through words in a string <john@castleamber.com>
    Re: Iterate through words in a string <wahab@chemie.uni-halle.de>
    Re: Iterate through words in a string <mritty@gmail.com>
    Re: Iterate through words in a string <DJStunks@gmail.com>
    Re: Iterate through words in a string <gcastro@mathworks.com>
        OSCON 2006 - Perl programming vs. scripting usenet@DavidFilmer.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 4 Jan 2007 12:11:37 -0800
From: diegodelima.poa@gmail.com
Subject: CGI.pm - Upload multiple files using the same query param
Message-Id: <1167941497.204443.205540@s80g2000cwa.googlegroups.com>

I need to upload multiple files using inputs with the same name, like
an array of files.

I know CGI.pm understands pretty well any number of inputs of type text
with the same name, and i've been using it for a while. This time I
need the same behavior with uploading files.

Is this possible?

I know many will advice me to use file1, file2, fileN, but I have some
platform specific reasons that would meke it lots easier to me if using
the same param names where possible.

Thanks!

Diego de Lima



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

Date: 4 Jan 2007 12:57:04 -0800
From: usenet@DavidFilmer.com
Subject: Re: CGI.pm - Upload multiple files using the same query param
Message-Id: <1167944223.245368.220220@11g2000cwr.googlegroups.com>

diegodelima.poa@gmail.com wrote:
> I need to upload multiple files using inputs with the same name, like
> an array of files.

No problem.  For example:

for (1..5) {
   print p( filefield(-name=>'upload',-size=>60) );
}

That prints out five upload fields with the same parameter name, and
the parameter will act like an ordinary array when you go to process
it:

foreach my $file( param('upload') ) {
   # upload the file and do whatever.
}


--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)



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

Date: Thu, 04 Jan 2007 22:27:43 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: CGI.pm - Upload multiple files using the same query param
Message-Id: <5059u8F1eg61fU1@mid.individual.net>

diegodelima.poa@gmail.com wrote:
> I need to upload multiple files using inputs with the same name, like
> an array of files.
> 
> I know CGI.pm understands pretty well any number of inputs of type text
> with the same name, and i've been using it for a while. This time I
> need the same behavior with uploading files.
> 
> Is this possible?

Yes. One easy way to do it is to make use of the CPAN module 
CGI::UploadEasy.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: 04 Jan 2007 20:02:04 GMT
From: adam@npjh.com (Adam Flott)
Subject: Coderefs and objects
Message-Id: <459d5d3c$0$18902$4c368faf@roadrunner.com>

I'm not entirely sure how code references work within objects and talking to
other objects.

From the Net::Telnet documentation:

  When mode is a coderef, then when an error is encountered coderef is called
  with the error message as its first argument. Using this mode you may have
  your own subroutine handle errors. If coderef itself returns then the method
  generating the error returns undefined or an empty list depending on context
  .

  When mode is an arrayref, the first element of the array must be a coderef.
  Any elements that follow are the arguments to coderef. When an error is
  encountered, the coderef is called with its arguments. Using this mode you 
  may have your own subroutine handle errors. If the coderef itself returns 
  then the method generating the error returns undefined or an empty list 
  depending on context.

The way I'm trying to use it is this:

---
# yes, strict and warnings are on, this is only for brevity
package A;

use Net::Telnet;

sub new {
    # usual new() stuff is there
}

sub open {
    my ($self) = @_;

    $th = Net::Telnet->new(Host => "someplace", Errmode => [
         $self->err_handler, "arg1", "arg2" ]);

    return $th;
}

sub err_handler {
    my ($self, @args) = @_;

    print(Dumper \@args);
}

1;
---

I get the error "ignoring bad Errmode argument: first list item isn't a code
ref", but it still calls it since (in my real version, I had it print
something).

I've gone through perlmod, perlsub, perlref, etc, but I still don't understand
using code references within objects to call other functions within that same
object. I do however understand them at the non-object level, $subref = sub
{};

A good thorough example or link would be greatly appreciated.


Thanks,

Adam


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

Date: Thu, 04 Jan 2007 20:21:09 GMT
From: Ala Qumsieh <noreply@invalid.net>
Subject: Re: Coderefs and objects
Message-Id: <Vkdnh.42332$wc5.1308@newssvr25.news.prodigy.net>

Adam Flott wrote:

> I'm not entirely sure how code references work within objects and talking
> to other objects.

[snip]

> # yes, strict and warnings are on, this is only for brevity
> package A;
> 
> use Net::Telnet;
> 
> sub new {
>     # usual new() stuff is there
> }
> 
> sub open {
>     my ($self) = @_;
> 
>     $th = Net::Telnet->new(Host => "someplace", Errmode => [
>          $self->err_handler, "arg1", "arg2" ]);

This actually calls the err_handler() method, and replaces it with its
return value, which is most probably 1 (the return value of print()).

What you want is something like:

   my $th = Net::Telnet->new(Host => "someplace", Errmode => [
            \&A::err_handler, "arg1", "arg2"]);

Note that using it this way, err_handler is not really an object method
anymore, but a function defined in package A. This means that its first
argument will not be the object ($self).

> 
>     return $th;
> }
> 
> sub err_handler {
>     my ($self, @args) = @_;
> 
>     print(Dumper \@args);
> }
> 
> 1;

--Ala



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

Date: 04 Jan 2007 20:44:10 GMT
From: adam@npjh.com (Adam Flott)
Subject: Re: Coderefs and objects
Message-Id: <459d671a$0$18906$4c368faf@roadrunner.com>

Ala Qumsieh <noreply@invalid.net> wrote:
> Adam Flott wrote:
> 
>> I'm not entirely sure how code references work within objects and talking
>> to other objects.
> 
> [snip]
> 
>> # yes, strict and warnings are on, this is only for brevity
>> package A;
>> 
>> use Net::Telnet;
>> 
>> sub new {
>>     # usual new() stuff is there
>> }
>> 
>> sub open {
>>     my ($self) = @_;
>> 
>>     $th = Net::Telnet->new(Host => "someplace", Errmode => [
>>          $self->err_handler, "arg1", "arg2" ]);
> 
> This actually calls the err_handler() method, and replaces it with its
> return value, which is most probably 1 (the return value of print()).
> 
> What you want is something like:
> 
>   my $th = Net::Telnet->new(Host => "someplace", Errmode => [
>            \&A::err_handler, "arg1", "arg2"]);
> 
> Note that using it this way, err_handler is not really an object method
> anymore, but a function defined in package A. This means that its first
> argument will not be the object ($self).
> 

This is close, but in err_handler() I will be accessing data specific to
that object (I have multiple of these telnet based objects). 

Could I use your method above, but make $self the first argument to the
function?

>> 
>>     return $th;
>> }
>> 
>> sub err_handler {
>>     my ($self, @args) = @_;
>> 
>>     print(Dumper \@args);
>> }
>> 
>> 1;


Adam


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

Date: Thu, 04 Jan 2007 22:42:38 GMT
From: Ala Qumsieh <noreply@invalid.net>
Subject: Re: Coderefs and objects
Message-Id: <ypfnh.60253$wP1.18263@newssvr14.news.prodigy.net>

Adam Flott wrote:

> Ala Qumsieh <noreply@invalid.net> wrote:
>> Adam Flott wrote:
>> 
>>> I'm not entirely sure how code references work within objects and
>>> talking to other objects.
>> 
>> [snip]
>> 
>>> # yes, strict and warnings are on, this is only for brevity
>>> package A;
>>> 
>>> use Net::Telnet;
>>> 
>>> sub new {
>>>     # usual new() stuff is there
>>> }
>>> 
>>> sub open {
>>>     my ($self) = @_;
>>> 
>>>     $th = Net::Telnet->new(Host => "someplace", Errmode => [
>>>          $self->err_handler, "arg1", "arg2" ]);
>> 
>> This actually calls the err_handler() method, and replaces it with its
>> return value, which is most probably 1 (the return value of print()).
>> 
>> What you want is something like:
>> 
>>   my $th = Net::Telnet->new(Host => "someplace", Errmode => [
>>            \&A::err_handler, "arg1", "arg2"]);
>> 
>> Note that using it this way, err_handler is not really an object method
>> anymore, but a function defined in package A. This means that its first
>> argument will not be the object ($self).
>> 
> 
> This is close, but in err_handler() I will be accessing data specific to
> that object (I have multiple of these telnet based objects).
> 
> Could I use your method above, but make $self the first argument to the
> function?

From the docs (the paragraph directly below the one you quoted in your OP):

       When mode is an arrayref, the first element of the array must be a
       coderef.  Any elements that follow are the arguments to coderef.
       When an error is encountered, the coderef is called with its argu-
       ments.  Using this mode you may have your own subroutine handle
       errors.  If the coderef itself returns then the method generating
       the error returns undefined or an empty list depending on context.

so:

   my $th = Net::Telnet->new(Host => "someplace", Errmode => [
            \&A::err_handler, $self, "arg1", "arg2"]);

I never used Net::Telnet, but this seems to satisfy its docs.

--Ala



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

Date: 04 Jan 2007 22:55:45 GMT
From: adam@npjh.com (Adam Flott)
Subject: Re: Coderefs and objects
Message-Id: <459d85f1$0$18881$4c368faf@roadrunner.com>

Ala Qumsieh <noreply@invalid.net> wrote:
> Adam Flott wrote:
> 
>> Ala Qumsieh <noreply@invalid.net> wrote:
>>> Adam Flott wrote:
>>> 
>>>> I'm not entirely sure how code references work within objects and
>>>> talking to other objects.
>>> 
>>> [snip]
>>> 
>>>> # yes, strict and warnings are on, this is only for brevity
>>>> package A;
>>>> 
>>>> use Net::Telnet;
>>>> 
>>>> sub new {
>>>>     # usual new() stuff is there
>>>> }
>>>> 
>>>> sub open {
>>>>     my ($self) = @_;
>>>> 
>>>>     $th = Net::Telnet->new(Host => "someplace", Errmode => [
>>>>          $self->err_handler, "arg1", "arg2" ]);
>>> 
>>> This actually calls the err_handler() method, and replaces it with its
>>> return value, which is most probably 1 (the return value of print()).
>>> 
>>> What you want is something like:
>>> 
>>>   my $th = Net::Telnet->new(Host => "someplace", Errmode => [
>>>            \&A::err_handler, "arg1", "arg2"]);
>>> 
>>> Note that using it this way, err_handler is not really an object method
>>> anymore, but a function defined in package A. This means that its first
>>> argument will not be the object ($self).
>>> 
>> 
>> This is close, but in err_handler() I will be accessing data specific to
>> that object (I have multiple of these telnet based objects).
>> 
>> Could I use your method above, but make $self the first argument to the
>> function?
> 
> From the docs (the paragraph directly below the one you quoted in your OP):
> 
>       When mode is an arrayref, the first element of the array must be a
>       coderef.  Any elements that follow are the arguments to coderef.
>       When an error is encountered, the coderef is called with its argu-
>       ments.  Using this mode you may have your own subroutine handle
>       errors.  If the coderef itself returns then the method generating
>       the error returns undefined or an empty list depending on context.
> 
> so:
> 
>   my $th = Net::Telnet->new(Host => "someplace", Errmode => [
>            \&A::err_handler, $self, "arg1", "arg2"]);
> 

In case anyone else comes across this post, here is another way to do
it.

    my $coderef = sub { $self->err_handler(@_); };

    my $th = Net::Telnet->new(Host => "someplace", Errmode => [
             $coderef, "arg1", "arg2"]);

It does look a little strange, but it does work.


> I never used Net::Telnet, but this seems to satisfy its docs.

The source is certainly unique and it has an interesting style to it.

Thanks for the help!


Adam


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

Date: 4 Jan 2007 13:47:16 -0800
From: "sherifffruitfly" <sherifffruitfly@gmail.com>
Subject: Elegant equivalent to this regex?
Message-Id: <1167947236.200604.209890@v33g2000cwv.googlegroups.com>

Hi all,

I'm new to regex, and hacked this one together. It seems awfully
redundant to me, but it does have the virtue at least of wearing its
meaning on its sleeve.

The task:

(1) match all quoted-comma'd numbers consisting of either 2 or 3
"sections". That is, critters of either form:

" "ddd,ddd,ddd" "
or
" "ddd,ddd" "

(2) Capture all of the digits, leaving the quotes and commas for the
garbage man. For example:

" "123, 456, 789" "
should in some fashion capture
"123456789"

Here's the regex I came up with:

(?<whole>\"(?<one>\d{1,3}),(?<two>\d{1,3}),(?<three>\d{1,3})\"|\"(?<one>\d{1,3}),(?<two>\d{1,3})\")

This works fine for me, and getting the desired complete "clean" number
from it is a
triviality.

But I get the feeling that this is the regex-equivalent of baby-talk.
I'd like to know if there's a simpler, more elegant regex matching the
same class of strings, and capturing essentially the same substrings.


Thanks for any insights,

cdj



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

Date: 4 Jan 2007 13:56:27 -0800
From: usenet@DavidFilmer.com
Subject: Re: Elegant equivalent to this regex?
Message-Id: <1167947787.513330.198880@s80g2000cwa.googlegroups.com>

sherifffruitfly wrote:
> (2) Capture all of the digits, leaving the quotes and commas for the
> garbage man.

If you just want to strip out the non-numerics why futz with regexps?
Why not just use s/// to get rid of the non-numerics?

   my $original = " 123, 456, 789 ' ";
   (my $numbers = $original) =~ s/\D//g ;
   print $numbers;


--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)



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

Date: 4 Jan 2007 14:13:55 -0800
From: "sherifffruitfly" <sherifffruitfly@gmail.com>
Subject: Re: Elegant equivalent to this regex?
Message-Id: <1167948835.774665.48830@i15g2000cwa.googlegroups.com>


usenet@DavidFilmer.com wrote:
> sherifffruitfly wrote:
> > (2) Capture all of the digits, leaving the quotes and commas for the
> > garbage man.
>
> If you just want to strip out the non-numerics why futz with regexps?
> Why not just use s/// to get rid of the non-numerics?
>
>    my $original = " 123, 456, 789 ' ";
>    (my $numbers = $original) =~ s/\D//g ;
>    print $numbers;

Because I want to use regex, please.

If you don't wish to help me with my question, but prefer to answer
only your own instead, that's perfectly fine, of course.



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

Date: Thu, 04 Jan 2007 16:28:58 -0600
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: Elegant equivalent to this regex?
Message-Id: <459d7f1b$0$10297$815e3792@news.qwest.net>

sherifffruitfly wrote:
> usenet@DavidFilmer.com wrote:
>> sherifffruitfly wrote:
>>> (2) Capture all of the digits, leaving the quotes and commas for the
>>> garbage man.
>> If you just want to strip out the non-numerics why futz with regexps?
>> Why not just use s/// to get rid of the non-numerics?
>>
>>    my $original = " 123, 456, 789 ' ";
>>    (my $numbers = $original) =~ s/\D//g ;
>>    print $numbers;
> 
> Because I want to use regex, please.
> 
> If you don't wish to help me with my question, but prefer to answer
> only your own instead, that's perfectly fine, of course.

Provide examples of your data, expected results, and what you've
tried. If you post a short example with all of those, it'll
be much easier to help.

The requirements and regular expression you posted don't coincide.


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

Date: Thu, 04 Jan 2007 23:30:34 +0100
From: Mirco Wahab <wahab@chemie.uni-halle.de>
Subject: Re: Elegant equivalent to this regex?
Message-Id: <enk01d$sds$2@mlucom4.urz.uni-halle.de>

Thus spoke sherifffruitfly (on 2007-01-04 22:47):

> Here's the regex I came up with:
> 
> (?<whole>\"(?<one>\d{1,3}),(?<two>\d{1,3}),(?<three>\d{1,3})\"|\"(?<one>\d{1,3}),(?<two>\d{1,3})\")
> 
> This works fine for me, and getting the desired complete "clean" number
> from it is a
> triviality.
> 
> But I get the feeling that this is the regex-equivalent of baby-talk.
> I'd like to know if there's a simpler, more elegant regex matching the
> same class of strings, and capturing essentially the same substrings.

You didn't specify how *exact* is your matching requirement,
eg. if you have data like this:

 "323, 432, 5" "123, 456, 789" " 888 , 999" " " "1234, 456, 789" "333, 444, 333, 444"

we want to extract *only* sequences with 2 or 3 fields
(comma delimited) *and* exactly 3 digits per number(!),
so only group #2 and #3 would match. And how the whitespace
convention is going to be ...

This would (worst case and highest specification)
look almost like:

  ...
  my $stuff = q'
   "323, 432, 5" "123, 456, 789" " 888 , 999" " " "1234, 456, 789" "333, 444, 333, 444"
  ';

  my $rexp = qr/ \"\s*      # first quote
                   \d{3}\s*   # first number

                    ,\s*      # first comma
                   \d{3}\s*   # second number

                   (?:           # prepare optional third thingy
                      ,\s*       # second comma
                     \d{3}\s*    # third number
                   )?
                 \"/x;      # second quote

  my @hits =
     map s/\D+//g && $_,
        $stuff =~ /$rexp/g;

  print join "\n", @hits;
  ...


Regards

M.


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

Date: 4 Jan 2007 15:04:40 -0800
From: "sherifffruitfly" <sherifffruitfly@gmail.com>
Subject: Re: Elegant equivalent to this regex?
Message-Id: <1167951880.330534.221960@i15g2000cwa.googlegroups.com>

J. Gleixner wrote:

> Provide examples of your data, expected results, and what you've
> tried. If you post a short example with all of those, it'll
> be much easier to help.

Just in case I typo'd my regex, here it is again, copy/pasted straight
from Expresso (regex testing/analysis tool):

\"(?<one>\d{1,3}),(?<two>\d{1,3}),(?<three>\d{1,3})\"|\"(?<one>\d{1,3}),(?<two>\d{1,3})\"

This is intended for use against csv files - there may be stuff in the
regex that exploits this assumption - I forget.

Sample text:

Oct
2005,6.02,211.9,"1,573,958",31.9,"135,191",722.8676,67.3,19.1,18.1,19.2,18.4,18.4,,
Nov
2005,6.02,212.8,"1,573,958",32.2,"135,191",722.8676,67.3,19.2,18.2,19.2,15.7,15.7,,
Dec
2005,6.02,213.6,"1,570,805",32.5,"136,005",723.228,66.2,19.2,18.2,19.3,13.3,13.3,,
Jan
2006,6.02,215.6,"1,573,483",32.9,"137,032",723.36,67.1,18.9,17.9,19.2,9.9,9.9,,
Feb
2006,6.02,216.7,"1,577,319",33.2,"137,413",723.4165,67.0,18.8,17.9,19.2,10.3,10.3,,
Mar
2006,6.02,217.2,"1,579,222",33.5,"137,519",723.5606,66.9,18.6,17.6,19.2,12.9,12.9,,
Apr
2006,6.02,218.6,"1,579,587",33.8,"138,393",723.63,66.8,18.5,17.5,19.2,10.8,10.8,,
May
2006,6.02,218.9,"1,578,357",34.2,"138,669",723.687,66.8,18.4,17.3,19.3,12.0,12.0,,
Jun
2006,6.02,218.5,"1,572,273",34.5,"138,963",725.0399,66.7,18.4,17.2,19.3,11.8,11.8,,
Jul
2006,6.03,218.0,"1,563,849",35.1,"139,379",725.1364,66.6,18.3,17.2,19.3,10.1,10.1,,
Aug
2006,6.03,217.3,"1,557,949",35.4,"139,467",725.205,66.5,18.4,17.1,19.4,11.2,11.2,,
Sep
2006,6.04,216.7,"1,549,354",35.8,"139,867",725.3182,66.3,18.4,17.2,19.4,9.5,9.5,,
Oct
2006,6.04,215.6,"1,541,800",35.8,"139,826",725.3855,66.2,18.5,17.2,19.4,10.6,10.6,,

Representative sample of matches/captures:

match               <whole>               <one>               <two>
          <three>
"1,573,958"       "1,573,958"              1                     573
               958
"135,191"          "135,191"                 135                 191
               (empty)
etc.

In my *ideal* regex, there would be just 1 capture from a given match:
the concatenation of the numerically named capture group values
(1573958 or 135191, in the above). Achieving th effect of this ideal
result is trivial from what my regex *does* provide, however.


> The requirements and regular expression you posted don't coincide.

I included "qualifier-words" (e.g., "essentially") in my OP that were
intended to make your statement false. I may have failed. Also I didn't
explicitly state an obvious limitation of my regex: that it only
"works" for quoted-comma'd numbers consisting of either 2 or 3 blocks
(e.g., it fails for numbers in the billions); that suffices for my
needs

A regex that satisfies my ideal situation would be great. But if that's
not possible, simply a more elegant more-or-less-equivalent of my own
would be greatly appreciated. There are many possible ways to
de-pretty-print - I just want an elegant one.

Does that help?

Thanks for responding,

cdj



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

Date: Thu, 04 Jan 2007 20:08:39 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Iterate through words in a string
Message-Id: <5051phF1e6nv2U1@mid.individual.net>

Gonzalo Castro wrote:
> I'm have a string I am constructing with space delimited words. I would like
> to iterate through each word later. I thought making an array from the
> contents of the string would be the convenient way to approach this but I'm
> not having any luck. Any ideas?
> 
> I construct a string in code similar to this:
>     $accumulator = ""
>     while (some condition)
>     {
>         $anotherString = getMeAnotherString();
>         $accumulator = $accumulator . " $anotherString"
>     }
> 
> 
> Then I try to make an array but this is clearly not right. What's the right
> way to do this?
>     @array = qw($accumulator);

     my @array = split ' ', $accumulator;

     perldoc -f split

> You might ask, "Why not simply add/push $anotherString onto an array first
> instead of concatenating into a string?" And I would answer, "Well, because
> I want the list of words to be the value part of a key/value hash and values
> have to be scalar. Am I right?"

No.

     my @array = qw(green red blue);
     my %hash;
     @hash{ qw/w1 w2 w3/ } = @array;

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: 4 Jan 2007 19:15:02 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: Iterate through words in a string
Message-Id: <Xns98AE86CA8AEFEcastleamber@130.133.1.4>

Gonzalo Castro <gcastro@mathworks.com> wrote:

 
> When I iterate
>     for $element (@array)
>     {
>         print "element = $element";
>     }
> 
> I get this: 
>     element = $accumulator

Yup,

perldoc -f qw (which sends you somewhere else)
perldoc -f split (to answer your q)

> You might ask, "Why not simply add/push $anotherString onto an array

Nah, I ask: why you're using camelCase in Perl? Try not to use a style 
that's common in one language into another (in Perl _ seems to be 
preferred).

-- 
John                Experienced Perl programmer: http://castleamber.com/

          Perl help, tutorials, and examples: http://johnbokma.com/perl/


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

Date: Thu, 04 Jan 2007 20:32:25 +0100
From: Mirco Wahab <wahab@chemie.uni-halle.de>
Subject: Re: Iterate through words in a string
Message-Id: <enjkum$ov1$1@mlucom4.urz.uni-halle.de>

Thus spoke Gonzalo Castro (on 2007-01-04 19:57):

> I'm have a string I am constructing with space delimited words. I would like
> to iterate through each word later. I thought making an array from the
> contents of the string would be the convenient way to approach this but I'm
> not having any luck. Any ideas?

Better done like this (using your style and making a working example):

  use strict;
  use warnings;

  sub getMeAnotherString { time }

  my $accumulator;
  my $done = 5;

  while (not !$done--) {
     my $anotherString = getMeAnotherString();
     $accumulator .=  $anotherString . ' ';
     print $anotherString,"\n";
  }

  my @array = split /\s+/, $accumulator;
  for my $element (@array) {
     print "element = $element\n";
  }

> You might ask, "Why not simply add/push $anotherString onto an array first
> instead of concatenating into a string?" And I would answer, "Well, because
> I want the list of words to be the value part of a key/value hash and values
> have to be scalar. Am I right?"

No, try:

  ... (continuing source code from above)

  my %hash;

  $hash{ SOMEKEY } = \@array;

  for my $key (%hash) {
     my $aref = $hash{ $key };

     for my $element (@$aref) {
        print "key = $key, element = $element\n";
     }

  }


Regards

M.


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

Date: 4 Jan 2007 11:40:10 -0800
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Iterate through words in a string
Message-Id: <1167939609.999848.4570@11g2000cwr.googlegroups.com>

Gonzalo Castro wrote:
> You might ask, "Why not simply add/push $anotherString onto an array first
> instead of concatenating into a string?" And I would answer, "Well, because
> I want the list of words to be the value part of a key/value hash and values
> have to be scalar. Am I right?"

Your words are correct, but your meaning is not.  Hash values must be
scalars, yes.  They do not, however, have to be strings.  References
(such as array references) are also scalars.

Please read up on references and multi-dimensional structures in:
perldoc perlreftut
perldoc perldsc

#!/usr/bin/perl
use strict;
use warnings;
my @words = ("foo", "bar", "baz");
my %hash;
$hash{alpha} = \@words;
print "First word: $hash{alpha}[0];
print "All words: @{$hash{alpha}}\n";
__END__

Paul Lalli



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

Date: 4 Jan 2007 12:11:16 -0800
From: "DJ Stunks" <DJStunks@gmail.com>
Subject: Re: Iterate through words in a string
Message-Id: <1167941476.074045.100820@s34g2000cwa.googlegroups.com>


Gonzalo Castro wrote:
> I'm have a string I am constructing with space delimited words. I would like
> to iterate through each word later. I thought making an array from the
> contents of the string would be the convenient way to approach this but I'm
> not having any luck. Any ideas?
>
> I construct a string in code similar to this:
>     $accumulator = ""
>     while (some condition)
>     {
>         $anotherString = getMeAnotherString();
>         $accumulator = $accumulator . " $anotherString"
>     }
>
>
> Then I try to make an array but this is clearly not right. What's the right
> way to do this?
>     @array = qw($accumulator);
>
> When I iterate
>     for $element (@array)
>     {
>         print "element = $element";
>     }
>
> I get this:
>     element = $accumulator
>
> You might ask, "Why not simply add/push $anotherString onto an array first
> instead of concatenating into a string?" And I would answer, "Well, because
> I want the list of words to be the value part of a key/value hash and values
> have to be scalar. Am I right?"

I think what everyone is saying is that yes, there is definitely a
better way to do what you're trying to do, but since we don't know what
your ultimate goal is we can't make specific suggestions.

Why don't you try showing us your sample input and desired output and
your current approach and we can help you see that there's a better way
to accomplish your goal.

-jp



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

Date: Thu, 04 Jan 2007 16:19:30 -0500
From: Gonzalo Castro <gcastro@mathworks.com>
Subject: Re: Iterate through words in a string
Message-Id: <C1C2D992.4971%gcastro@mathworks.com>

perldoc perlreftut described exactly what I was trying to accomplish; use
arrays as hash values. After learning I could use references to refer to the
arrays I didn't need to concatenate and split a string as a workaround.

Thanks to you and others for getting me out of the fog.

Gonzalo



On 1/4/07 2:40 PM, in article
1167939609.999848.4570@11g2000cwr.googlegroups.com, "Paul Lalli"
<mritty@gmail.com> wrote:

> Gonzalo Castro wrote:
>> You might ask, "Why not simply add/push $anotherString onto an array first
>> instead of concatenating into a string?" And I would answer, "Well, because
>> I want the list of words to be the value part of a key/value hash and values
>> have to be scalar. Am I right?"
> 
> Your words are correct, but your meaning is not.  Hash values must be
> scalars, yes.  They do not, however, have to be strings.  References
> (such as array references) are also scalars.
> 
> Please read up on references and multi-dimensional structures in:
> perldoc perlreftut
> perldoc perldsc
> 
> #!/usr/bin/perl
> use strict;
> use warnings;
> my @words = ("foo", "bar", "baz");
> my %hash;
> $hash{alpha} = \@words;
> print "First word: $hash{alpha}[0];
> print "All words: @{$hash{alpha}}\n";
> __END__
> 
> Paul Lalli
> 



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

Date: 4 Jan 2007 13:49:29 -0800
From: usenet@DavidFilmer.com
Subject: OSCON 2006 - Perl programming vs. scripting
Message-Id: <1167947369.713078.197100@q40g2000cwq.googlegroups.com>

This may be a little bit OT, and probably a long shot...

I attended the Lightning Talks session of OSCON 2006 (in July).  My
memory is a little fuzzy...

A well-known Perl guru (I think it might have been Peter Scott) did a
brief presentation encouraging folks to describe of Perl as a
programming language rather than a scripting language.  He used a whole
slew of examples to show why (closures, methods, etc).

Now, months later, I'm interested in getting a copy of that
presentation.  I don't suppose anybody here would know where I might
get it?

Thanks!

--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)



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

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


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