[28037] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 9401 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jun 28 18:06:02 2006

Date: Wed, 28 Jun 2006 15:05:08 -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           Wed, 28 Jun 2006     Volume: 10 Number: 9401

Today's topics:
        crash on async in taint mode <ynleder@nspark.org>
    Re: How to ignore 1st line in a file when reading <penryu@saiyix.ath.cx>
    Re: How to ignore 1st line in a file when reading <tadmc@augustmail.com>
    Re: How to pass an array and scalar as arguments to a s <penryu@saiyix.ath.cx>
    Re: how to remote a unix server in my cgi script <jgibson@mail.arc.nasa.gov>
    Re: how to remote a unix server in my cgi script <tzz@lifelogs.com>
        Is this a Hash and how can I test a value? <qbert@comcast.net>
    Re: Is this a Hash and how can I test a value? <no@email.com>
    Re: List context versus list context <ced@blv-sam-01.ca.boeing.com>
        Malformed utf8; where's the null byte coming from? bill_mckinnon@interloper.net
    Re: Malformed utf8; where's the null byte coming from? <benmorrow@tiscali.co.uk>
    Re: Malformed utf8; where's the null byte coming from? bill_mckinnon@interloper.net
        Printing Hash of Array of Arrays <pradeep.bg@gmail.com>
    Re: Printing Hash of Array of Arrays <mritty@gmail.com>
    Re: Printing Hash of Array of Arrays <simon.chao@fmr.com>
    Re: Printing Hash of Array of Arrays <mritty@gmail.com>
    Re: Printing Hash of Array of Arrays <someone@example.com>
    Re: Printing Hash of Array of Arrays <penryu@saiyix.ath.cx>
    Re: Printing Hash of Array of Arrays <tzz@lifelogs.com>
    Re: Question about OP anno4000@zrz.tu-berlin.de
    Re: Question about OP <benmorrow@tiscali.co.uk>
    Re: replacement of slow unpack <someone@example.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 28 Jun 2006 23:53:43 +0200
From: Yohan N. Leder <ynleder@nspark.org>
Subject: crash on async in taint mode
Message-Id: <MPG.1f0d18d140d950bf9898ae@news.tiscali.fr>

To replace a fork() which crashes ActivePerl 5.8.8 in taint mode, I've 
tried to go through creation of a thread... And it crashes too !

#!/usr/bin/perl -wT
use threads;
print "Content-type: text/html\n\n";
my $thr = async{print "<p>thread in progress...</p>"};
die "thread failure\n" unless defined($thr);
$thr->detach();
sleep(1);
print "<p>main in progress...</p>";
exit 0;

ActivePerl falls in exception on async{} block.
What's wrong again ?


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

Date: Wed, 28 Jun 2006 19:48:15 GMT
From: Tim Hammerquist <penryu@saiyix.ath.cx>
Subject: Re: How to ignore 1st line in a file when reading
Message-Id: <slrnea5n7v.1h2f.penryu@ruri.saiyix>

Deepu <pradeep.bg@gmail.com> wrote:
[ snip ]
> basically i will take each line and compare with a pattern.
>
> while (<FH>){
>
> if ($_ =~ /^\s*(\S+)\t+pg:\s+(\S+)/) { ## here i need to start from 2nd
> line
>   ## Do something ##
> }
>
> }

Here's my solution, very similar to another poster's:

open FH, "<filename" or die "can't open: $!";
<FH>;   # discards first line
while (<FH>) {
    if ( /pattern/ ) {
        ## Do something ##
    }
}

If you need to store the first line for later use I recommend the
solution elsewhere in this thread.

HTH,
Tim Hammerquist


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

Date: Wed, 28 Jun 2006 15:53:26 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: How to ignore 1st line in a file when reading
Message-Id: <slrnea5r26.udh.tadmc@magna.augustmail.com>

Deepu <pradeep.bg@gmail.com> wrote:

> Now i need to read from 2nd line. Can somebody please help me on this.
> 
> To read the whole file, i just use:
> 
> open (FH, "FileName") || die "Can't open";


   <FH>;   # read a line, and discard it


> while (<FH>) {
>   push (@array, $_);
> }



-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Wed, 28 Jun 2006 20:44:28 GMT
From: Tim Hammerquist <penryu@saiyix.ath.cx>
Subject: Re: How to pass an array and scalar as arguments to a subroutine/
Message-Id: <slrnea5qhc.1h2f.penryu@ruri.saiyix>

TheOrangeRemix <fgchan@gmail.com> wrote:
> I'm writing a Perl script to take in an array and a scalar as two
> arguments and pass them to a subroutine.
> Each element of the array has contents ########### word ########### and
> the scalar being passed is the size of the array.
> The subroutine is supposed to strip the '#" from the contents of each
> element and just report the "word" in one array, and report the size of
> the array in a scalar.
>
> Here is the code:
>
> -------------------------------------------------------------------------------
>
> unhash(@Array_A,$a); # array and size
>
> sub round {
>     my($number) = shift;
>     return int($number + .5);
> }
>
> sub unhash
> {
>         my(@fieldarray) = @_;
>         my($sizearray) = shift;
>
>
> print "$sizearray\n";
>
>         for ($i = 0; $i < $sizearray; $i++)
>         {
>                if ($fieldarray[$i] =~ m/########### (.*?)#/)
>
>                {
>                    $fieldarray[$i] = $1;
>
>                }
>
>
>
>         }
> print @fieldarray;
> }
>
> -------------------------------------------------------------------------------
>
> What ends up happening is that the subroutine returns the new formatted
> array and the size in the first array and returns a blank scalar. What
> has happened here and how do I fix it? Thanks.
>

### untested ###

sub round {
    return sprintf("%.0f", shift);
}

sub unhash {
    my @fieldarray = @_;

    print scalar(@fieldarray), "\n";

    foreach my $rawfield ( @fieldarray ) {
        if ($rawfield =~ m/########### (.*?)#/) {
            $field = $1;
        }
    }
    print @fieldarray;
}

Also, have a look at perlsub, "Prototypes".

HTH,
Tim Hammerquist


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

Date: Wed, 28 Jun 2006 12:36:13 -0700
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: how to remote a unix server in my cgi script
Message-Id: <280620061236139514%jgibson@mail.arc.nasa.gov>

In article <1151497767.332530.76020@y41g2000cwy.googlegroups.com>,
debbie523 <Deb.Fang@gmail.com> wrote:

> Jim Gibson wrote:
> > In article <1151417862.372738.220860@y41g2000cwy.googlegroups.com>,
> > debbie523 <Deb.Fang@gmail.com> wrote:
> >
> > > I have a cgi script stay in one unix server A, and in this cgi script I
> > > have to call a program which sits on another unix server B. so there
> > > should have server lines to login B in my cgi script. Is there somebody
> > > help me figure out what's the specific command I should use.
> >
> > What protocol does your CGI program on server A use to run the program
> > on server B? SSH? RSH? RPC? HTTP/CGI?
> 
> SSH

Then I would search CPAN for SSH modules. Net::SSH::Perl, Net::SSH, and
Bundle::SSH look like good possibilities. I have not used any of these
or used SSH at all from Perl, so cannot advise you further.

Good luck!

-- 
Jim Gibson


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

Date: Wed, 28 Jun 2006 16:26:49 -0400
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: how to remote a unix server in my cgi script
Message-Id: <g693bdpgh6e.fsf@CN1374059D0130.kendall.corp.akamai.com>

On 28 Jun 2006, jgibson@mail.arc.nasa.gov wrote:

> In article <1151497767.332530.76020@y41g2000cwy.googlegroups.com>,
> debbie523 <Deb.Fang@gmail.com> wrote:
>
>> Jim Gibson wrote:
>>> In article <1151417862.372738.220860@y41g2000cwy.googlegroups.com>,
>>> debbie523 <Deb.Fang@gmail.com> wrote:
>>>
>>>> I have a cgi script stay in one unix server A, and in this cgi script I
>>>> have to call a program which sits on another unix server B. so there
>>>> should have server lines to login B in my cgi script. Is there somebody
>>>> help me figure out what's the specific command I should use.
>>>
>>> What protocol does your CGI program on server A use to run the program
>>> on server B? SSH? RSH? RPC? HTTP/CGI?
>>
>> SSH
>
> Then I would search CPAN for SSH modules. Net::SSH::Perl, Net::SSH, and
> Bundle::SSH look like good possibilities. I have not used any of these
> or used SSH at all from Perl, so cannot advise you further.

Often, people find it sufficient to do

system("/usr/bin/ssh", @parameters);

rather than resort to Perl modules to achieve the same results.
Sometimes this is not possible due to implementation details, but
perhaps the OP would find it useful to consider this alternative first
because it's simplest.

Ted


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

Date: Wed, 28 Jun 2006 17:50:42 -0400
From: "Jockser" <qbert@comcast.net>
Subject: Is this a Hash and how can I test a value?
Message-Id: <wtCdneKedZ00ZD_ZnZ2dnUVZ_oGdnZ2d@comcast.com>

Hello I have a XML doc that I'm processing.  I'm down to the point where in 
the for loop I need to check to see if 'type' is set to 'a' or 'b'.
Using print Dumper( $temp ) gives me this:

$VAR1 = {
          'lname' => 'Smith',
          'fname' => 'John',
          'address1' => '2500 Lemon St.',
          'type' => 'a'
        };

1st question:  Is this a Hash?  I haven't been sucessful in accessing it 
like a hash, ofcourse this is the first time I've delt with Hashes.

2nd question:  How can I check to see if 'type' is  = to 'a' or 'b' ?

Code here:

use strict;
use warnings;
use Template;
use XML::Simple;
use Data::Dumper;


my $cust_xml = XMLin('./test2.xml'); #, ForceArray => 1);
my $input = 'temp_a.tt';
my $tt    = Template->new( );

for my $temp (@{$cust_xml->{template}}) {

# Need some code here to test the value of 'type', if it is 'a' or 'b'
# I can then do an if statement to change the $input file
# $input = 'temp_b.tt';

 $tt->process($input, $temp)
 || die $tt->error( );
 print Dumper( $temp );
}
print "\n";
#print XMLout($cust_xml);
print "\n";



Thanks! 




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

Date: Wed, 28 Jun 2006 22:58:02 +0100
From: Brian Wakem <no@email.com>
Subject: Re: Is this a Hash and how can I test a value?
Message-Id: <4ggcbaF1ls6adU1@individual.net>

Jockser wrote:

> Hello I have a XML doc that I'm processing.  I'm down to the point where
> in the for loop I need to check to see if 'type' is set to 'a' or 'b'.
> Using print Dumper( $temp ) gives me this:
> 
> $VAR1 = {
>           'lname' => 'Smith',
>           'fname' => 'John',
>           'address1' => '2500 Lemon St.',
>           'type' => 'a'
>         };
> 
> 1st question:  Is this a Hash?  I haven't been sucessful in accessing it
> like a hash, ofcourse this is the first time I've delt with Hashes.


$temp contains a reference to a hash.


> 2nd question:  How can I check to see if 'type' is  = to 'a' or 'b' ?


print $temp->{'type'};



-- 
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png


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

Date: Wed, 28 Jun 2006 19:12:02 GMT
From: Charles DeRykus <ced@blv-sam-01.ca.boeing.com>
Subject: Re: List context versus list context
Message-Id: <J1L404.72t@news.boeing.com>

Tad McClellan wrote:
> Charles DeRykus <ced@blv-sam-01.ca.boeing.com> wrote:
>> xhoster@gmail.com wrote:
>>> Ch Lamprecht <christoph.lamprecht.no.spam@web.de> wrote:
>>>> Tad McClellan wrote:
>>> ...
>>>
>>> It is starting to look like a bug.
>>> ...
>>>
>>> A slice off the end of a nonempty list is an empty list in an assignment,
>>> but not as the arg to a sub.
>>>
>> The "off-the-end" semantics in the 2 assignment examples below looks 
>> unexpected to me too.
>>
>> I can see that the 2nd example becomes a non-empty list but why should
>> it generate the additional undef's 
> 
> 
> It doesn't generate any "additional" undefs, 4 indexes, 4 values.
> 
> 
>> while the 1st example remains mired
>> in its own emptiness...:)
> 
> 
> Because when it is _all_ undefs (ie. _all_ of the indexes are off
> the end), the special case of returning an empty list kicks in. 
> 
> perldata says
> 
>    This makes it easy to write loops that terminate when a null list
>    is returned
> 
> as a "justification" for this behavior.
 >

Thanks, I read that earlier and it went in one ear and breezed straight 
through..

> 
> 
>> $ perl -MData::Dumper -le 'my @x = (0)[2,4,6]; print Dumper \@x'
>> $VAR1 = [];
>>
>> $ perl -MData::Dumper -le 'my @x = (0)[0, 2,4,6];print Dumper \@x'
>> $VAR1 = [
>>            0,
>>            undef,
>>            undef,
>>            undef
>>          ];
> 
> 
> Neither of these display the bug that is under discussion.
> 
> The 1st one generates the empty list rather than the non-empty list
> that the bug gives.
> 
> The 2nd one is not _all_ off of the end, so a non-empty list is expected.
> 
> The bug is when all of the indexes are off the end and it returns
> a non-empty list.
> 
> 

Got it now.  The bug seems to manifest only as an argument call rather 
than a direct assignment.


-- 
Charles DeRykus


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

Date: 28 Jun 2006 13:31:09 -0700
From: bill_mckinnon@interloper.net
Subject: Malformed utf8; where's the null byte coming from?
Message-Id: <1151526669.128826.325810@m73g2000cwd.googlegroups.com>

   I've spent some time trying to understand Perl's Unicode support and
its nuances, and I think I actually understand some amount of it. But
the behavior of this snippet of code is puzzling me at the moment:

--
#!/usr/local/bin/perl -w

use Encode qw(decode);

$s = decode('utf8', "Version");  # String w/utf8 flag set
$s =~ s/v\xc3\x83//i;
--

Running this with Perl 5.8.6 on Linux (and Windows) produces this
warning:

$ ./test.pl
Malformed UTF-8 character (unexpected non-continuation byte 0x00,
immediately after start byte 0xc3) in substitution (s///) at ./test.pl
line 7.
$

   Granted, what I'm trying to do is to match the literal utf8 bytes
for a Unicode character against a Unicode string, which may not be a
reasonable thing to do. But the way this fails doesn't make any sense
to me; I don't have a null byte after (or before) the \xc3 byte in my
regex. Also, if the regex string was being upgraded to Unicode
(presumably from iso-latin-1) I can see it not doing what I intended,
but this shouldn't cause this error; it should just not match the way I
want. And then if the \x sequences were taken to be code points instead
of literal bytes then that's fine...it may not do what I want, but it
still shouldn't cause this warning.
   Does anyone know why this warning is coming up? It makes me think
there's more going on under the surface than just an extra iso-latin-1
-> utf8 conversion. Thanks in advance for any insight. :)

- Bill

P.S. - I can do the match I want by using the results of
         encode('utf8', $s) to do the match; since it's a byte
         string everything works fine.  But I want to understand
         what the issue was with the warning. :)



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

Date: Wed, 28 Jun 2006 22:30:29 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: Malformed utf8; where's the null byte coming from?
Message-Id: <l09bn3-3uk.ln1@osiris.mauzo.dyndns.org>


Quoth bill_mckinnon@interloper.net:
>    I've spent some time trying to understand Perl's Unicode support and
> its nuances, and I think I actually understand some amount of it. But
> the behavior of this snippet of code is puzzling me at the moment:
> 
> --
> #!/usr/local/bin/perl -w
> 
> use Encode qw(decode);
> 
> $s = decode('utf8', "Version");  # String w/utf8 flag set
> $s =~ s/v\xc3\x83//i;
> --
> 
> Running this with Perl 5.8.6 on Linux (and Windows) produces this
> warning:
> 
> $ ./test.pl
> Malformed UTF-8 character (unexpected non-continuation byte 0x00,
> immediately after start byte 0xc3) in substitution (s///) at ./test.pl
> line 7.
> $

Some more data points: 5.8.7 i686-linux

1. There is no need for Encode.

my $s = "foo";
utf8::upgrade($s);

works fine (in the sense that it fails).

2. It only fails if the first character matches. This makes sense...

3. It only fails if there are zero-or-one characters after the \xc3.
Putting a second stops the warning.

4. It still fails if the \xc3 is the first character (and the string is
modified to match, obviously).

5. The match does not have to be at the start of the string.

6. \xf3 behaves the same way (the number of expected continuation bytes
doesn't matter).

I believe this is a bug: anyone else?

Of course, what you are trying to do is completely wrong :). /v\xf3\x83/
is a regex which matches three characters, not two. The fact that those
three, if expressed as bytes in iso8859-1, happen to look like the utf8
for two characters is irrelevant. It seems perl is having something of
the same confusion you are :)

Ben

-- 
        I must not fear. Fear is the mind-killer. I will face my fear and
        I will let it pass through me. When the fear is gone there will be 
        nothing. Only I will remain.
benmorrow@tiscali.co.uk                                   Frank Herbert, 'Dune'


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

Date: 28 Jun 2006 15:01:44 -0700
From: bill_mckinnon@interloper.net
Subject: Re: Malformed utf8; where's the null byte coming from?
Message-Id: <1151532104.885286.145450@b68g2000cwa.googlegroups.com>

Ben Morrow wrote:
> Of course, what you are trying to do is completely wrong :). /v\xf3\x83/
> is a regex which matches three characters, not two. The fact that those
> three, if expressed as bytes in iso8859-1, happen to look like the utf8
> for two characters is irrelevant. It seems perl is having something of
> the same confusion you are :)

   Yep, agreed...I was initially feeding the s/// data that DIDN'T have
the utf8 flag set even though it was real utf8 data, and this of course
works ok. At some point the regex got string data that did have the
utf8 flag set, and then it didn't work right and got this warning...and
I wondered what was up with the warning. : )
   Also, interestingly enough the regex was trying to match a utf8 byte
stream that had been incorrectly interpreted as iso-8859-1 and then
re-encoded as utf8. : ) Funny how these things happen...

- Bill



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

Date: 28 Jun 2006 12:17:20 -0700
From: "Deepu" <pradeep.bg@gmail.com>
Subject: Printing Hash of Array of Arrays
Message-Id: <1151522240.528043.51050@75g2000cwc.googlegroups.com>

Hi All,

How to print hash of array of arrays without using Data::Dumper.

I am trying to print

%hash = (
                 FILE1 => [
                                  [
                                    STATE1
                                    STATE2
                                    STATE3
                                  ],
                                  [
                                   STATEX
                                   STATEA
                                   STATE4
                                  ]
                                ],

                  FILE2 => [
                                   [
                                     STATE2
                                     STATE5
                                   ],

and so on.

I tried:

foreach my $key (sort keys %hash) {
  print "$key\n";    ## - It prints the file names correctly as keys

  for $i (0 .. $#{$hash{$key}}) {
    print "$i: $hash{$key}[$i]";
  }
}

But now i get output like:

0: ARRAY(0x5fdbf0)
and so on.

Please help me on this.

Thanks
Deep



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

Date: 28 Jun 2006 12:40:10 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Printing Hash of Array of Arrays
Message-Id: <1151523610.421380.301990@j72g2000cwa.googlegroups.com>

Deepu wrote:
> How to print hash of array of arrays without using Data::Dumper.
>
> I am trying to print
>
> %hash = (
>                  FILE1 => [
>                                   [
>                                     STATE1
>                                     STATE2
>                                     STATE3
>                                   ],
>                                   [
>                                    STATEX
>                                    STATEA
>                                    STATE4
>                                   ]
>                                 ],
>
>                   FILE2 => [
>                                    [
>                                      STATE2
>                                      STATE5
>                                    ],
>
> and so on.
>
> I tried:
>
> foreach my $key (sort keys %hash) {
>   print "$key\n";    ## - It prints the file names correctly as keys
>
>   for $i (0 .. $#{$hash{$key}}) {
>     print "$i: $hash{$key}[$i]";
>   }
> }

You said you have a hash of arrays of arrays.  So %hash is the hash,
$hash{$key} is a reference to the first-level array, and
$hash{$key}[$i] is a reference to the second level array.  You need to
dereference this and loop through the results, just as you did for the
first level:

foreach my $key (sort keys %hash) {
   print "$key\n";
   for my $i (0 .. $#{$hash{$key}) {
      print "  Array $i\n";
      foreach my $elem (@{$hash{$key}[$i]}) {
         print "    $elem\n";
      }
   }
}

Note that the above syntax is just to demonstrate how to do it
following your lead.  Were I to write this, I would probably not use a
loop for the final array level, and just print all the array elements
directly:
foreach my $key (sort keys %hash) {
   print "$key\n";
   for my $i (0 .. $#{$hash{$key}) {
      print "  Array $i: @{$hash{$key}[$i]}\n";
   }
}

Hope this helps,
Paul Lalli



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

Date: 28 Jun 2006 12:40:20 -0700
From: "it_says_BALLS_on_your forehead" <simon.chao@fmr.com>
Subject: Re: Printing Hash of Array of Arrays
Message-Id: <1151523620.378533.110330@p79g2000cwp.googlegroups.com>


Deepu wrote:
> Hi All,
>
> How to print hash of array of arrays without using Data::Dumper.
>
> I am trying to print
>
> %hash = (
>                  FILE1 => [
>                                   [
>                                     STATE1
>                                     STATE2
>                                     STATE3
>                                   ],
>                                   [
>                                    STATEX
>                                    STATEA
>                                    STATE4
>                                   ]
>                                 ],
>
>                   FILE2 => [
>                                    [
>                                      STATE2
>                                      STATE5
>                                    ],
>
> and so on.
>
> I tried:
>
> foreach my $key (sort keys %hash) {
>   print "$key\n";    ## - It prints the file names correctly as keys
>
>   for $i (0 .. $#{$hash{$key}}) {
>     print "$i: $hash{$key}[$i]";
>   }
> }
>
> But now i get output like:
>
> 0: ARRAY(0x5fdbf0)
> and so on.


use Data::Dumper;

print Dumper( \%hash );



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

Date: 28 Jun 2006 12:43:08 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Printing Hash of Array of Arrays
Message-Id: <1151523788.203796.148850@d56g2000cwd.googlegroups.com>

it_says_BALLS_on_your forehead wrote:
> Deepu wrote:

> > How to print hash of array of arrays without using Data::Dumper.

> use Data::Dumper;
>
> print Dumper( \%hash );

I can't tell if you're trying to be funny or not. . .  

Paul Lalli



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

Date: Wed, 28 Jun 2006 19:46:22 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Printing Hash of Array of Arrays
Message-Id: <i0Bog.83371$I61.47405@clgrps13>

Deepu wrote:
> 
> How to print hash of array of arrays without using Data::Dumper.
> 
> I am trying to print
> 
> %hash = (
>                  FILE1 => [
>                                   [
>                                     STATE1
>                                     STATE2
>                                     STATE3
>                                   ],
>                                   [
>                                    STATEX
>                                    STATEA
>                                    STATE4
>                                   ]
>                                 ],
> 
>                   FILE2 => [
>                                    [
>                                      STATE2
>                                      STATE5
>                                    ],
> 
> and so on.
> 
> I tried:
> 
> foreach my $key (sort keys %hash) {
>   print "$key\n";    ## - It prints the file names correctly as keys
> 
>   for $i (0 .. $#{$hash{$key}}) {
>     print "$i: $hash{$key}[$i]";
>   }
> }
> 
> But now i get output like:
> 
> 0: ARRAY(0x5fdbf0)
> and so on.

You have to dereference the array reference:

for my $key ( sort keys %hash ) {
    print "$key\n";
    for my $array_ref ( @{ $hash{ $key } } ) {
        print "@$array_ref";
        }
    }



John
-- 
use Perl;
program
fulfillment


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

Date: Wed, 28 Jun 2006 20:05:45 GMT
From: Tim Hammerquist <penryu@saiyix.ath.cx>
Subject: Re: Printing Hash of Array of Arrays
Message-Id: <slrnea5o8p.1h2f.penryu@ruri.saiyix>

Deepu <pradeep.bg@gmail.com> wrote:
> Hi All,
>
> How to print hash of array of arrays without using Data::Dumper.
>
> I am trying to print
[ snip ]
> and so on.

Why you want to mimic Data::Dumper's behaviour without actually using
it is confusing at best.  It actually smells of homework.  Perhaps
a lesson in recursive solutions?

Without having *looked* at the Data::Dumper code (which should be an
obvious tactic for one attempting to copy its behaviour), I'd suggest
exactly that: recursion.

HTH,
Tim Hammerquist


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

Date: Wed, 28 Jun 2006 16:33:38 -0400
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Printing Hash of Array of Arrays
Message-Id: <g69y7vhf2al.fsf@CN1374059D0130.kendall.corp.akamai.com>

On 28 Jun 2006, pradeep.bg@gmail.com wrote:

> How to print hash of array of arrays without using Data::Dumper.

It's useful here to classify things into hashes, arrays, and
everything else.

dump_container($hash_of_arrays_of_arrays);

sub dump_container
{
 my $ref = shift @_;

 if (ref $ref eq 'ARRAY')
 {
  ... do something with @$ref and call dump_container on each element ...
 }
 elsif (ref $ref eq 'HASH')
 {
  ... do something with %$ref and call dump_container on each value ...
 }
 else
 {
  print "Saw element of type ", ref $ref, "and data ", "$ref\n";
 }
}

Note that this will handle any combination of container data
structures, as long as they are arrays and hashes.  Look at "perldoc
-f ref" for more information.

Avoiding Data::Dumper is weird, but I'm sure you have good reasons...

Ted


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

Date: 28 Jun 2006 18:07:59 GMT
From: anno4000@zrz.tu-berlin.de
Subject: Re: Question about OP
Message-Id: <4gfurvF1mk6aoU1@news.dfncis.de>

Ferry Bolhar <bol@adv.magwien.gv.at> wrote in comp.lang.perl.misc:
> Hi folks,
> 
> can someone explain the purpose and behaviour of PADOPs,
> SVOPs and PVOPs? Why are there OPs (like gvsv) which
> are sometime PADOPs and sometime SVOPs?

Sorry, no.

> Maybe this will be still remain an unanswered question, like
> the one about cached and cloned CVs, but I ask nevertheless.
> Perhaps...
> 
> I would also very appreciate if someone could me point to
> any link or book relating this theme. It seems this topic is
> too deep/down/low to be explained anywhere in the perl
> docs.

Where else, if at all?  A printed book would probably be lagging
against the source by the time it is printed.

> perlguts & friends are just a start (a good start!), but
> these details are not mentioned there.

The source, brother, the source.

Anno


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

Date: Wed, 28 Jun 2006 21:38:40 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: Question about OP
Message-Id: <gv5bn3-n9k.ln1@osiris.mauzo.dyndns.org>


Quoth "Ferry Bolhar" <bol@adv.magwien.gv.at>:
> 
> can someone explain the purpose and behaviour of PADOPs,
> SVOPs and PVOPs? 

PADOPs operate on an index into a pad.
SVOPs  operate on a SV.
PVOPs  operate on a string.

:)

SVOPs are ops which operate on a single compile-time SV, rather than
the result of another op (those are UNOPs). So in

    ~% perl -MO=Concise -e'print $x'
    6  <@> leave[1 ref] vKP/REFC ->(end)
    1     <0> enter ->2
    2     <;> nextstate(main 1 -e:1) v ->3
    5     <@> print vK ->6
    3        <0> pushmark s ->4
    -        <1> ex-rv2sv sK/1 ->5
    4           <$> gvsv(*x) s ->5
    -e syntax OK

the gvsv is a SVOP as the *x is determined at compile time. The
(optimised away) rv2sv was an UNOP as the SV to operate on was given by
the result of the gv (which has been optimised into a gvsv).

PVOPs are ops which operate on a compile-time string. The only ones,
AFAICT, are tr/// and the loop control ops; e.g. in

    ~% perl -MO=Concise -e'LINE: { last LINE }'
    7  <@> leave[1 ref] vKP/REFC ->(end)
    1     <0> enter ->2
    2     <;> nextstate(LINE: main 2 -e:1) v ->3
    6     <2> leaveloop vK/2 ->7
    3        <{> enterloop(next->6 last->6 redo->4) v ->4
    -        <@> lineseq vKP ->6
    4           <;> nextstate(main 1 -e:1) v ->5
    5           <"> last("LINE") v ->6
    -e syntax OK

the last is a PVOP as it has a compile-time string (the loop label) to
operate on. tr/// similarly has a compile-time string built from the
translations.

> Why are there OPs (like gvsv) which
> are sometime PADOPs and sometime SVOPs?

PADOPs are only used in threading perls. There is a fake op class called
GVOP, used for ops which need a GV (like gv and gvsv): in non-threaded
perls these are SVOPs, and access the real symbol table; in threaded
perls they are PADOPs, which get at the symbol table through some sort
of indirection stuffed into the sub's lexical pad. I admit I don't fully
understand this... :)

> I would also very appreciate if someone could me point to
> any link or book relating this theme. It seems this topic is
> too deep/down/low to be explained anywhere in the perl
> docs. perlguts & friends are just a start (a good start!), but
> these details are not mentioned there.

As Anno said, read the source. I worked this out in about half-an-hour
of playing with B::Concise (which has docs well worth reading, btw) and
grepping op.c/op.h/pp_hot.c.

Ben

-- 
It will be seen that the Erwhonians are a meek and long-suffering people,
easily led by the nose, and quick to offer up common sense at the shrine of
logic, when a philosopher convinces them that their institutions are not based 
on the strictest morality. [Samuel Butler, paraphrased] benmorrow@tiscali.co.uk


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

Date: Wed, 28 Jun 2006 19:38:16 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: replacement of slow unpack
Message-Id: <IUAog.83370$I61.12122@clgrps13>

cyl wrote:
> this block of code took about 25 seconds in my computer
> 
> open A,"a_500mb_file";
> binmode A;
> while(sysread(A,$x,256)){
>         #do nothing
> }
> close A;
> 
> and this took 215 seconds
> 
> open A,"a_500mb_file";
> binmode A;
> while(sysread(A,$x,256)){
>         my @c=unpack('C*',$x);
> }
> close A;
> 
> So why is unpack so slow here? I thought it very fast before. Do I use
> it in a wrong way or is there any replacement? Thanks.

Yes it is slow, but it is faster then the alternatives.  The lesson here is
try to use the contents of $x without dividing it up into individual characters.



John
-- 
use Perl;
program
fulfillment


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

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


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