[24072] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 6267 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Mar 16 14:15:43 2004

Date: Tue, 16 Mar 2004 11:15:09 -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, 16 Mar 2004     Volume: 10 Number: 6267

Today's topics:
        hash of array of arrays with hashes <tba_del@softhome.net>
    Re: hash of array of arrays with hashes <ittyspam@yahoo.com>
    Re: hash of array of arrays with hashes <tba_del@softhome.net>
    Re: hash of array of arrays with hashes <ddunham@redwood.taos.com>
    Re: Hash slices and array references <jgibson@mail.arc.nasa.gov>
    Re: Having problems with with the s/// substitution <nobull@mail.com>
    Re: Having problems with with the s/// substitution <tore@aursand.no>
    Re: incrementing a string by a certain value <kirk@strauser.com>
        Returning/Sending Arrays as Parameters (bbaisley)
    Re: Returning/Sending Arrays as Parameters <ittyspam@yahoo.com>
    Re: Returning/Sending Arrays as Parameters <tore@aursand.no>
    Re: SOAP::Lite help with nested XML please <glex_nospam@qwest.invalid>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 16 Mar 2004 17:29:22 GMT
From: Tobias Anderberg <tba_del@softhome.net>
Subject: hash of array of arrays with hashes
Message-Id: <c37dhi$1te06o$1@ID-135332.news.uni-berlin.de>

This is simple:

my %data = (
    'array' => [
	    [
		    {'key1' => 'val1'}
		],
		[
		    {'key1' => 'val1'}
		]
	]
);

Now, I'm having problems with creating the above automatically,
so to speak. It's the populate() function that
bothers me:

my %data = (
    'array' => undef
);

sub populate
{
    if (not defined($data{array})) {
	    $data{array} = [[{@_}]];

        # So far so good...
    }
    else {
        # What about this? I want to do
        #
        # push $data{array}, [{@_}];
        #
        # or something similar but I can't any thing to work.
        # I.e. here I want to add an array containing a hash
        # to the $data{array} array. Doing something like:

        my @d = $data{array};
        push @d, [{@_}];
        $data{array} = [@d];

       # obviously doesn't work the way I want.
    }
}

my %hash1 = ('key1' => 'val1');
my %hash2 = ('key1' => 'val1');

sub populate(%hash1);
sub populate(%hash1);

Does it make any sense? Can anybody give me some hints?

Thanks!

-- 
tba softhome net


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

Date: Tue, 16 Mar 2004 12:39:43 -0500
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: hash of array of arrays with hashes
Message-Id: <20040316123848.E21521@dishwasher.cs.rpi.edu>

On Tue, 16 Mar 2004, Tobias Anderberg wrote:

> This is simple:
>
> my %data = (
>     'array' => [
> 	    [
> 		    {'key1' => 'val1'}
> 		],
> 		[
> 		    {'key1' => 'val1'}
> 		]
> 	]
> );
>
> Now, I'm having problems with creating the above automatically,
> so to speak. It's the populate() function that
> bothers me:
>
> my %data = (
>     'array' => undef
> );
>
> sub populate
> {
>     if (not defined($data{array})) {
> 	    $data{array} = [[{@_}]];
>
>         # So far so good...
>     }
>     else {
>         # What about this? I want to do
>         #
>         # push $data{array}, [{@_}];

$data{array} is an array reference.  Dereference it:
push @{$data{array}}, [{@_}];


>         #
>         # or something similar but I can't any thing to work.
>         # I.e. here I want to add an array containing a hash
>         # to the $data{array} array. Doing something like:
>
>         my @d = $data{array};
>         push @d, [{@_}];
>         $data{array} = [@d];
>
>        # obviously doesn't work the way I want.
>     }
> }
>
> my %hash1 = ('key1' => 'val1');
> my %hash2 = ('key1' => 'val1');
>
> sub populate(%hash1);
> sub populate(%hash1);
>
> Does it make any sense? Can anybody give me some hints?
>

Paul Lalli


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

Date: 16 Mar 2004 18:15:19 GMT
From: Tobias Anderberg <tba_del@softhome.net>
Subject: Re: hash of array of arrays with hashes
Message-Id: <c37g7m$25184v$1@ID-135332.news.uni-berlin.de>

Paul Lalli wrote:
>> my %data = (
>>     'array' => undef
>> );
>>
>> sub populate
>> {
>>     if (not defined($data{array})) {
>> 	    $data{array} = [[{@_}]];
>>
>>         # So far so good...
>>     }
>>     else {
>>         # What about this? I want to do
>>         #
>>         # push $data{array}, [{@_}];
>
> $data{array} is an array reference.  Dereference it:
> push @{$data{array}}, [{@_}];

Ah! Of course! Thanks!

-- 
tba softhome net


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

Date: Tue, 16 Mar 2004 18:58:01 GMT
From: Darren Dunham <ddunham@redwood.taos.com>
Subject: Re: hash of array of arrays with hashes
Message-Id: <Z6I5c.10852$LI3.167@newssvr27.news.prodigy.com>

Tobias Anderberg <tba_del@softhome.net> wrote:
> This is simple:

> my %data = (
>     'array' => [
> 	    [
> 		    {'key1' => 'val1'}
> 		],
> 		[
> 		    {'key1' => 'val1'}
> 		]
> 	]
> );

> Now, I'm having problems with creating the above automatically,
> so to speak. It's the populate() function that
> bothers me:

> my %data = (
>     'array' => undef
> );

my %data = ( 'array' => [] );

> sub populate
> {
>     if (not defined($data{array})) {
> 	    $data{array} = [[{@_}]];

>         # So far so good...
>     }
>     else {
>         # What about this? I want to do
>         #
>         # push $data{array}, [{@_}];

The first argument to push must be an array.
push @{$data{array}}, 

>         # or something similar but I can't any thing to work.
>         # I.e. here I want to add an array containing a hash
>         # to the $data{array} array. Doing something like:

>         my @d = $data{array};

That doesn't do anything useful.  It just puts the array reference as
the first and only item in the array @d.  

>         push @d, [{@_}];
>         $data{array} = [@d];

Probably...

          push @{$data{array}}, [{@_}];

Assuming @_ contains and even number of elements...


push @

>        # obviously doesn't work the way I want.
>     }
> }

> my %hash1 = ('key1' => 'val1');
> my %hash2 = ('key1' => 'val1');

> sub populate(%hash1);
> sub populate(%hash1);

I assume you don't want those subs there...


My version...
my %data = ( 'array' => [] );

sub populate
{
   push @{$data{array}}, [{@_}];
}


my %hash1 = ('key1' => 'val1');
my %hash2 = ('key2' => 'val2');

populate (%hash1);
populate (%hash2);



-- 
Darren Dunham                                           ddunham@taos.com
Senior Technical Consultant         TAOS            http://www.taos.com/
Got some Dr Pepper?                           San Francisco, CA bay area
         < This line left intentionally blank to confuse you. >


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

Date: Tue, 16 Mar 2004 09:08:21 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Hash slices and array references
Message-Id: <160320040908219272%jgibson@mail.arc.nasa.gov>

In article <3bef037b.0403152122.23ddf9c6@posting.google.com>, David
<davidol@hushmail.com> wrote:

> Dear all,
> 
> I pass an array of hashes by reference into a function and dereference
> it like so:
> 
> my $ret_val = func ( \@table );
> 
> sub func {
>   my @table = @{ $_[0] };
>   ...
>   ...
> }
> 
> I can then modify hash elements directly using a loop like this:
> 
>   my $num = 0;
>   for (my $i = 0; $i < $#table; $i++) {  

$#table is the largest index of @table, not the number of elements, so
you want:

   for (my $i = 0; $i <= $#table; $i++) {

or

   for (my $i = 0; $i < @table; $i++) ]

or you will skip the last element.

>       $table[$i]{seq_no} = ++$num;
>        ...
>        ...
>   }


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

Date: 16 Mar 2004 17:42:46 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: Having problems with with the s/// substitution
Message-Id: <u9ish4n0x5.fsf@wcl-l.bham.ac.uk>

hbacakoglu@hotmail.com (Hakan Bacakoglu) writes:
> Please help me out with this: I am reading a file WEBINP and trying to
> replace a pattern which exists multiple times in each line. The
> replacement ($1) is called out of a subroutine.
> 
> The problem is when I try to do substitution at line 5, the second
> while loop (line 3) gets stuck at the first accurance of the pattern.
> If I comment out line 5, I see that while loop iterates and the finds
> the pattern as expected.
> 
> What am I missing here? Any suggestions?

Scalars in Perl have a special property, a regex cursor, accessible
via pos that is used, amongst other things, to implement the scalar m//g.

If you change a scalar using s/// the cursor for that string is reset.

> line 3     while (m{(\D)(\d{4})(\D)}g){
> line 4        $l = find($2,@list);
> line 5        s/$2/$l/; } 

I suspect you meant 

s/(?<=\D)(\d{4})(?=\D)/find("$1",@list)/eg;

Or maybe you meant

s/(?<!\d)(\d{4})(?!\d)/find("$1",@list)/eg;

(Note: passing $digit as an argument is a situation where the normal
useless use of quotes rule does _not_ apply).

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: Tue, 16 Mar 2004 19:20:06 +0100
From: Tore Aursand <tore@aursand.no>
Subject: Re: Having problems with with the s/// substitution
Message-Id: <pan.2004.03.16.17.53.55.19413@aursand.no>

On Tue, 16 Mar 2004 08:01:44 -0800, Hakan Bacakoglu wrote:
> Please help me out with this:
> [...]

It's extremely helpful if you post an extract of what you're trying to
parse.

> line 1 while (<WEBINP>){
> line 2
> line 3     while (m{(\D)(\d{4})(\D)}g){
> line 4        $l = find($2,@list);
> line 5        s/$2/$l/; }
> line 6
> line 7     print WEBOUT $_;
> line 8  }

Why do you set $1?  From the code above, $1 will always be set by the
script to a non-digit value.


-- 
Tore Aursand <tore@aursand.no>
"Omit needless words. Vigorous writing is concise. A sentence should
 contain no unnecessary words, a paragraph no unnecessary sentences,
 for the same reason that a drawing should have no unnecessary lines
 and a machine no unnecessary parts." -- William Strunk Jr.


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

Date: Tue, 16 Mar 2004 16:15:07 GMT
From: Kirk Strauser <kirk@strauser.com>
Subject: Re: incrementing a string by a certain value
Message-Id: <874qsoojnq.fsf@strauser.com>

=2D----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

At 2004-03-16T14:21:46Z, Tore Aursand <tore@aursand.no> writes:

>          $ord =3D ( $ord < 97  ) ? 122 : $ord;
>          $ord =3D ( $ord > 122 ) ?  97 : $ord;

So much for "wrapping".  :)
=2D --=20
Kirk Strauser
The Strauser Group
Open. Solutions. Simple.
http://www.strausergroup.com/
=2D----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAVyd55sRg+Y0CpvERAsH0AJ4gnxYqDvT1euq0isr1MG0PFs3SAgCfRQvp
pTuuXPelLelpgVetaxcLCbM=3D
=3DBOdn
=2D----END PGP SIGNATURE-----


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

Date: 16 Mar 2004 08:54:30 -0800
From: baisley@hotmail.com (bbaisley)
Subject: Returning/Sending Arrays as Parameters
Message-Id: <e530512e.0403160854.7e4f6a8a@posting.google.com>

Hello,

I am trying to write a simple Perl app to learn how to pass arrays as
paramaters. I think I figured out how to send in an array, but how to
I return it? This following example only returns one value and nothing
else:


my @a1 = (1,2,3);
my @a2 = test(@a1);

foreach $row (@a2) {
  print "ROW: $row /n";
}


sub test() {
my @inner = @_;
my @b1 = ();

for ($i=0; $i<3; $i++){
  $b1[i] = "hi there";
}  
  return (@b2)
}

Any ideas how to fix this?


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

Date: Tue, 16 Mar 2004 12:05:59 -0500
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: Returning/Sending Arrays as Parameters
Message-Id: <20040316120045.M21521@dishwasher.cs.rpi.edu>

On Tue, 16 Mar 2004, bbaisley wrote:

> Hello,
>
> I am trying to write a simple Perl app to learn how to pass arrays as
> paramaters. I think I figured out how to send in an array, but how to
> I return it? This following example only returns one value and nothing
> else:
>
>
> my @a1 = (1,2,3);
> my @a2 = test(@a1);
>
> foreach $row (@a2) {
>   print "ROW: $row /n";
> }
>
>
> sub test() {
> my @inner = @_;
> my @b1 = ();
>
> for ($i=0; $i<3; $i++){
>   $b1[i] = "hi there";
> }
>   return (@b2)
> }
>
> Any ideas how to fix this?

Please do not retype your code when posting a message.  Always copy and
paste.  The code you posted here will not do anything.  Also, you should
be using warnings and strict.

A list of problems with this code:
You've explicitly assigned a prototype to take no parameters, yet passed
an array of parameters.  Do not include () in your subroutine declaration
if you don't know what that means.
You're trying to fill up an array named @b1 within the for loop, but
you're then returning an array named @b2
You have a bareword 'i' inside the array indices where you meant $i.
There's no semi-colon after the return statement (not technically a syntax
error, but a really bad idea).
(Also, what the heck is the point of passing (1,2,3) into the function,
and assigning them to @inner?  You're not using them anywhere!)

If you correct these errors, you should see the output you expect.

Paul Lalli


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

Date: Tue, 16 Mar 2004 19:20:07 +0100
From: Tore Aursand <tore@aursand.no>
Subject: Re: Returning/Sending Arrays as Parameters
Message-Id: <pan.2004.03.16.17.59.50.921767@aursand.no>

On Tue, 16 Mar 2004 08:54:30 -0800, bbaisley wrote:
> I am trying to write a simple Perl app to learn how to pass arrays as
> paramaters. I think I figured out how to send in an array, but how to
> I return it? This following example only returns one value and nothing
> else:
> 
> my @a1 = (1,2,3);
> my @a2 = test(@a1);
> 
> foreach $row (@a2) {
>   print "ROW: $row /n";
> }

Don't retype the code, and please 'use strict' and 'use warnings';

  foreach my $row ( @a2 ) {
      print "ROW: $row\n";
  }

Or - even better;

  print "ROW: $_\n" foreach ( @a2 );

> sub test() {
> my @inner = @_;
> my @b1 = ();
> 
> for ($i=0; $i<3; $i++){
>   $b1[i] = "hi there";
> }  
>   return (@b2)
> }
> 
> Any ideas how to fix this?

I suggest that you learn how to deal with references;

  my @a1 = (1,2,3);
  my $a2 = test( \@a1 ); # Pass test() the reference of @a1, and receive
                         # a reference

  print "ROW: $_\n" foreach ( @$a2 ); # $a2 is being dereferenced here

  sub test {
      my $inner = shift; # Receive a reference

      my @b1;
      for ( 0..2 ) {
          $b1[$_] = 'hi there'; # Omit needles double-quotes
      }

      return \@b1; # Return a reference
  }


-- 
Tore Aursand <tore@aursand.no>
"Out of missiles.  Out of bullets.  Down to harsh language." -- Unknown


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

Date: Tue, 16 Mar 2004 11:00:26 -0600
From: "J. Gleixner" <glex_nospam@qwest.invalid>
Subject: Re: SOAP::Lite help with nested XML please
Message-Id: <LoG5c.28$P32.39608@news.uswest.net>

ItNerd wrote:
> use SOAP::Lite +autodispatch =>
> uri => 'http://www.allconsuming.net/AllConsumingAPI',
> proxy => 'http://www.allconsuming.net/soap.cgi';
> 
> my $AllConsumingObject = AllConsumingAPI->new ();
> 
> 
> my $BookFile = $AllConsumingObject->GetWeeklyList();
> 
> print "Content-type: text/html\n\n";
> 
>     foreach my $item (@{$BookFile->{'asins'}}) {
>         print "<font size=2
> face='arial'><b>$item->{'asin'}></b><br>$item->{'title'}</font><p>";
>     }
> 
> 
> .....................
> For the above code, I will be accessing a nested xml feed as in this link:
> 
> http://www.allconsuming.net/rest.cgi?action=GetWeeklyList
> 
> How do I get the next level of 'urls'?  I can get the //asins to print
> out, but my foreach doesn't seem to work right for the next level inward
> to the urls list.

The best way to go about this is to use Data::Dumper and display the
data structure, then you can access the data structure.

my $BookFile = $AllConsumingObject->GetWeeklyList();

use Data::Dumper;

print Dumper($BookFile);



$VAR1 = {
           'asins' => [
                      {
                        'mentions' => '13',
                        'product_group' => 'Book',
                        'author' => 'Dan Brown',
                        'asin' => '0385504209',
                        'title' => 'The Da Vinci Code',
                        'score' => {
                                   'today' => '0',
                                   'week_ago' => '2',
                                   'total' => '35',
                                   'yesterday' => '12'
                                 },
                        'urls' => [
                                  {
                                    'url' => 'http://www.eamonn.com/'
                                  },
                                  {
                                    'url' => 
'http://s94746624.onlinehome.us/chasingwind/mt/'
                                  },

etc....

To access the url values, it's:

	foreach my $urls (@ { $item->{urls} } ) {
		#url is a hash reference:
		print $urls->{url}, "\n";
	}

For more info on accessing various data structures:

	perldoc perldsc


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

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


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