[11947] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5547 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon May 3 02:07:14 1999

Date: Sun, 2 May 99 23:00:19 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 2 May 1999     Volume: 8 Number: 5547

Today's topics:
    Re: An attempt to compare the performance of perl vs co <elliotsl@NOSPAMmindspring.com>
        And this? esalmon@packet.net
    Re: And this? <ebohlman@netcom.com>
        Berkeley DB 2.x <idaham@mimos.my>
    Re: Berkeley DB 2.x <ebohlman@netcom.com>
    Re: Create a new file <mhc@Eng.Sun.COM>
    Re: Create a new file (Larry Rosler)
        Exit Code question revisited :> <bennettd@asdi.saic.com>
        Exit code question.. <bennettd@asdi.saic.com>
    Re: Exit code question.. <ebohlman@netcom.com>
    Re: Exit code question.. <bennettd@asdi.saic.com>
    Re: HELP: opening files in perl and streaming them to w (David Efflandt)
    Re: HELP: opening files in perl and streaming them to w <ebohlman@netcom.com>
    Re: I need some socket help! (David Efflandt)
    Re: Perl & ftp problem (David Efflandt)
        perl sorting ? cyberunity@my-dejanews.com
    Re: perl sorting ? <mhc@Eng.Sun.COM>
    Re: Remote access in CGI (David Efflandt)
        sorting data in perl cyberunity@my-dejanews.com
        Sorting on PERL tvn007@my-dejanews.com
    Re: Sorting on PERL <uri@sysarch.com>
    Re: Sorting on PERL (Larry Rosler)
    Re: stuctured data object for export <walton@frontiernet.net>
    Re: Whats wrong? (Sam Holden)
    Re: Whats wrong? <walton@frontiernet.net>
    Re: Whats wrong? <mhc@Eng.Sun.COM>
    Re: writing binary data to a file (Sam Holden)
    Re: writing binary data to a file (Sean McAfee)
    Re: writing binary data to a file <mhc@Eng.Sun.COM>
    Re: writing binary data to a file (Larry Rosler)
    Re: writing binary files in Perl (Larry Rosler)
        Special: Digest Administrivia (Last modified: 12 Dec 98 (Perl-Users-Digest Admin)

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

Date: Mon, 03 May 1999 00:38:45 -0400
From: Steven Elliott <elliotsl@NOSPAMmindspring.com>
Subject: Re: An attempt to compare the performance of perl vs compiled perl vs C
Message-Id: <372D2855.412A29B1@NOSPAMmindspring.com>

David Cassell wrote:

> Steven Elliott wrote:
> >
> > [trim of post]
> >
> > The program I chose (sum_array.{pl,c}) is small, mathematically
> > intensive and makes many array accesses.  This I believe, and as I
> > think my test results demonstrate, strongly favors C.  My intent in
>
> It certainly should.  However, see my notes below.
>
> > choosing an example that strongly favors C was not to be deprecate perl
> > but to determine what the worst case scenario might be in terms of my
> > having to rewrite something into C.  I love perl.
>
> I found your Perl program impossible to access, so I can't evaluate
> it.  Perhaps you might want to post it...

It is embedded in my post as plain text since my news tool (Netscape)
did not use any encoding.  In any case here it is:
-----------------------------
#!/usr/bin/perl -w

# A small benchmark program with emphasis on iterating though an array
# while performing floating point calculations on it's entries.

use strict;

my $index;
my $count;
my $iterations = $ARGV[0] || 100000;
my @tst_array;
my $sum;

for($index = 0; $index < 100; $index++)
{
    $tst_array[$index] = ($index + 1.0)/10.0;
}

for($count = 1; $count <= $iterations; $count++)
{
    for($index = 0; $index < 100; $index++)
    {
        $sum += $tst_array[$index];
        $sum -= 100.0 if($sum > 100.0);
    }
}

print "The sum is $sum\n";-----------------------------

> But these times suggest that there might be some non-optimal
> elements in your Perl code.
>
> Tom Christiansen's [possibly mythological] Perl vs. C bet with
> Rob Kolstad makes me wonder if a factor of 29 [or so] is a little
> high to expect, even for something like this.  The rumor I heard
> said that Tom never had to pay off on his bet.
>
> > # perl uncompiled (sum_array.pl) -> 58 sec:
>
> > # perl compiled to bytecode (from sum_array.pl) -> 58 sec:
>
> > # perl compiled to an executable (from sum_array.pl) -> 22 sec:
>
> I really didn't expect to see this large of a change here.

I probably should have given more info on my testing methods.  I'm using perl

5.005.03.  I compiled sum_array.pl using perlcc which uses the "CC" backend.
I have not tried the "C" backend.  My understanding of the "CC" backed is
that
it attempts to translate perl loosely into a somewhat typical C program
whereas the
"C" backend directly translates the perl script into a C program that makes
the
same function calls that the perl script would have had it been run.

If you are interested I can send you the Linux executable or the .c file
generated
by the "CC" backend.

Also, I'm using a shared libperl.so that I created myself by expanding the
static
libperl.a and then relinking it.  This allows the executable to be 12K.  I
doubt
this would have a significant impact on performance for programs like this
where
the load time is relatively small.

> > # C compiled to an executable (from sum_array.c) -> 2 sec:
>
> If it's easier and faster for you to write efficient C code, then
> that seems like a reasonable choice to me.
>
> David
> --
> David Cassell, OAO                            cassell@mail.cor.epa.gov
> Senior Computing Specialist                      phone: (541) 754-4468
> mathematical statistician                          fax: (541) 754-4716



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

Date: Mon, 03 May 1999 00:41:52 -0400
From: esalmon@packet.net
Subject: And this?
Message-Id: <372D2910.6879@packet.net>

I figured the backtics out but how do I retrive the STDOUT that it
produces?

$userid=`whoami`;


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

Date: Mon, 3 May 1999 05:11:20 GMT
From: Eric Bohlman <ebohlman@netcom.com>
Subject: Re: And this?
Message-Id: <ebohlmanFB552w.BKw@netcom.com>

esalmon@packet.net wrote:
: I figured the backtics out but how do I retrive the STDOUT that it
: produces?

: $userid=`whoami`;

Just as you wrote.  $userid will be set to everything (possibly including 
newlines) that whoami writes to STDOUT (which, of course, could be 
nothing if whoami fails to run, so be sure to account for this possibility).

If anything is still making it out to your console rather than being 
captured into $userid, then whoami is writing it to STDERR rather than 
STDOUT, and there's no quick shortcut for capturing it (but see perlipc 
for rather-more-involved ways of capturing STDERR).



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

Date: Mon, 3 May 1999 12:47:23 +0800
From: "Mohd Idaham" <idaham@mimos.my>
Subject: Berkeley DB 2.x
Message-Id: <7gj99i$aud$1@news6.jaring.my>

Where can I get documentation and sample using this Db with PERL ?
Thanks




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

Date: Mon, 3 May 1999 05:17:26 GMT
From: Eric Bohlman <ebohlman@netcom.com>
Subject: Re: Berkeley DB 2.x
Message-Id: <ebohlmanFB55D2.BtJ@netcom.com>

Mohd Idaham <idaham@mimos.my> wrote:
: Where can I get documentation and sample using this Db with PERL ?

If you already have it installed, look at the POD in the Db-File module.  
If not, get it from CPAN (or ActiveState for Win32 systems) and then, 
guess what, look at the POD.



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

Date: 02 May 1999 21:45:18 -0700
From: Mike Coffin <mhc@Eng.Sun.COM>
Subject: Re: Create a new file
Message-Id: <8p6hfpukbhd.fsf@Eng.Sun.COM>

"Lawrence Brendish" <ghf62@dial.pipex.com> writes:

> Hi,
> 
> Very new to perl (less than 24 hours!), how do you create a new file using a
> perl script?
> 

unless (-e "newfile") {
    open (OUT, ">newfile");
    close OUT;
}

-mike


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

Date: Sun, 2 May 1999 22:36:29 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Create a new file
Message-Id: <MPG.1196d5ae4ca15a7398999d@nntp.hpl.hp.com>

In article <8p6hfpukbhd.fsf@Eng.Sun.COM> on 02 May 1999 21:45:18 -0700, 
Mike Coffin <mhc@Eng.Sun.COM> says...
> "Lawrence Brendish" <ghf62@dial.pipex.com> writes:
 ...
> > Very new to perl (less than 24 hours!), how do you create a new file using a
> > perl script?
> 
> unless (-e "newfile") {
>     open (OUT, ">newfile");
>     close OUT;
> }

That is how you *try* to create a new file.  How do you know whether it 
succeeded or not?

ALWAYS, yes ALWAYS, check the results of an open() before proceeding on 
the assummption that it succeeded.

As a one-liner (of sorts):

-e 'newfile' or do { local *FH; open FH, '>newfile' } or
   die "Couldn't open 'newfile'.  $!\n";

-- 
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Sun, 2 May 1999 22:50:24 -0700
From: "Darren Bennett" <bennettd@asdi.saic.com>
Subject: Exit Code question revisited :>
Message-Id: <86149256@NEWS.SAIC.COM>

Is this true of NT as well??

                        (how can I get around it??)

                                    -Darren

Eric Bohlman wrote in message ...

>:         The following script should return a 'expression (non-zero) and a
>: 'zero' correct?? it instead just returns the following..
>[script is running on Win32 system]
>At least on Win95, the command interpreter returns with an exit status of
>0 (i.e. lies that everything is OK) if it can't find a command.  Not very
>useful behavior, but it explains the results you're getting.
>
>





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

Date: Sun, 2 May 1999 21:42:07 -0700
From: "Darren Bennett" <bennettd@asdi.saic.com>
Subject: Exit code question..
Message-Id: <86141504@NEWS.SAIC.COM>

        The following script should return a 'expression (non-zero) and a
'zero' correct?? it instead just returns the following..

> cd c:\perl\scripts
> perl strange.pl


C:\Perl\scripts>perl strange.pl
Bad command or file name

 Volume in drive C has no label
 Volume Serial Number is 206D-1AD5


Result: 0



Result: 0


C:\Perl\scripts>

can anyone shed some light on what I'm screwing up?? thnkx..


##
### Run a system command (IE: bla *bad command*,vol)
##

system("bla");

print "\n\nResult: $?\n\n";

system("vol");

print "\n\nResult: $?\n\n";





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

Date: Mon, 3 May 1999 05:16:06 GMT
From: Eric Bohlman <ebohlman@netcom.com>
Subject: Re: Exit code question..
Message-Id: <ebohlmanFB55Au.Bs5@netcom.com>

Darren Bennett <bennettd@asdi.saic.com> wrote:
:         The following script should return a 'expression (non-zero) and a
: 'zero' correct?? it instead just returns the following..

[script is running on Win32 system]

: system("bla");

: print "\n\nResult: $?\n\n";

: system("vol");

: print "\n\nResult: $?\n\n";

At least on Win95, the command interpreter returns with an exit status of 
0 (i.e. lies that everything is OK) if it can't find a command.  Not very 
useful behavior, but it explains the results you're getting.




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

Date: Sun, 2 May 1999 22:36:05 -0700
From: "Darren Bennett" <bennettd@asdi.saic.com>
Subject: Re: Exit code question..
Message-Id: <86147281@NEWS.SAIC.COM>

        Is this true of NT as well??

                        (how can I get around it??)

                                    -Darren

Eric Bohlman wrote in message ...

>:         The following script should return a 'expression (non-zero) and a
>: 'zero' correct?? it instead just returns the following..
>[script is running on Win32 system]
>At least on Win95, the command interpreter returns with an exit status of
>0 (i.e. lies that everything is OK) if it can't find a command.  Not very
>useful behavior, but it explains the results you're getting.
>
>




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

Date: 3 May 1999 04:54:55 GMT
From: efflandt@xnet.com (David Efflandt)
Subject: Re: HELP: opening files in perl and streaming them to web pages
Message-Id: <slrn7iqatm.3bd.efflandt@efflandt.xnet.com>

On Thu, 22 Apr 1999 14:12:04 GMT, Matt Evans <stats123@my-dejanews.com> wrote:
>Hi,
>
>I'm trying to open files in perl which are outside the web tree being served,
>and stream themstraight to a web page in the hope it will pop up a "save as"
>box and people will be able to download these files.
>I have sort of got it working but it's very buggy.
>I'm running apache on Win95 with active state perl.
>Heres the code I have written:
>
>#!C:/perl/bin/perl.exe
>
>open(DFILE, "C:\\My Documents\\webmaster\\stds_down\\files.txt")||&errprn
>("Couldn't open Download file: $dfile");

$len = -s $dfile;	# file size in bytes
print "Content-type: application/octet-stream\n";
print "Content-length: $len\n\n";	# recommended
binmode DFILE;	# you need this for binary files in Windows (not unix)
print <DFILE>;	# this prints the whole file at once.
close DFILE;

Somewhat simpler than the following, don't you think?  Also you don't
really have lines in a binary file.

>while ($line = <DFILE>)
> 	{
>		print $line;
>   	}
>close(DFILE);
>
>
>this seems to work but if I change the file to open from a text file to a jpg
>say, and I change the Content-type to image/jpeg te whole thing stops working.
>
>Please someone help me.
>
>Cheers,
>
>Matt
>
>-----------== Posted via Deja News, The Discussion Network ==----------
>http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


-- 
David Efflandt    efflandt@xnet.com
http://www.xnet.com/~efflandt/


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

Date: Mon, 3 May 1999 05:19:08 GMT
From: Eric Bohlman <ebohlman@netcom.com>
Subject: Re: HELP: opening files in perl and streaming them to web pages
Message-Id: <ebohlmanFB55Fx.BvB@netcom.com>

David Efflandt <efflandt@xnet.com> wrote:
: $len = -s $dfile;	# file size in bytes
: print "Content-type: application/octet-stream\n";
: print "Content-length: $len\n\n";	# recommended
: binmode DFILE;	# you need this for binary files in Windows (not unix)

You also need to binmode STDOUT.  Note that on Unix, binmode is simply a 
no-op.

: print <DFILE>;	# this prints the whole file at once.
: close DFILE;


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

Date: 3 May 1999 05:22:56 GMT
From: efflandt@xnet.com (David Efflandt)
Subject: Re: I need some socket help!
Message-Id: <slrn7iqci8.3bd.efflandt@efflandt.xnet.com>

On Sun, 02 May 1999 13:37:01 -0400, esalmon@packet.net
<esalmon@packet.net> wrote:
>I am playing around with learning the inner workings of UNIX sockets,
>with Perl, and have had some luck here and there. I have been able to do
>some edits to adapt and reverse engineer a http-get request script
>successfully and would like to see it readapted for FTP. I know it is
>easier to just use Net::FTP but I like to hack and learn.
>
>I am on a UNIX BSDI BSD/OS 3.1 running the Apache HTTPD Server v1.2.6
>and mainly programming in Perl v5.x.

The easy way:

#!/usr/local/bin/perl
use Net::FTP;
$ftpsite = 'ftp.packet.net';
$ftp = Net::FTP->new($ftpsite);
if ($ftp->login("anonymous", "esalmon\@packet.net")) {
    print "FTP site $ftpsite is alive\n";
    # Do something useful
    $ftp->quit;
} else {
    print "Can't connect to $ftpsite\n";
}


The hard way (may not be most efficient, but was available,
based on wwwgrab):

#!/usr/local/bin/perl
use Socket;
# Parse ftp address from command line or STDIN
($_) = @ARGV;
if (!$_) {
  die "Usage:  $0 ftp://ftp.any.site/\n";
}
/ftp:\/\/(\S+)/;
$site = $1;
if (!$site) {
  die "You appear to have munged your ftp address.\n" .
      "It must be in the form of ftp://www.any.site\n";
}
$_ = $site;
/^([^:]*):*([^ ]*)/;
$site = $1;
$port = $2;
$port = 21 unless $port;
# Open a socket
($sockaddr,$there,$response,$tries) = ("Snc4x8");
$there = pack($sockaddr, 2, $port, &getaddress($hostname));
$proto = (getprotobyname ('tcp'))[2];
if (!socket(S,AF_INET,SOCK_STREAM,$proto)) 
    { die "Socket Error: $!\n"; }
# Attempt to connect
if (connect(S,$there)) {
    print "FTP server appears to be up.\n";
    # Do something useful
    close(S);
} else {
    print "Connect Error: $!\n";
}

sub getaddress {
  local($host) = @_;
  local(@ary);
  @ary = gethostbyname($host);
  return(unpack("C4",$ary[4]));
}

If you want to learn more look through the Net::FTP module.

-- 
David Efflandt    efflandt@xnet.com
http://www.xnet.com/~efflandt/


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

Date: 3 May 1999 05:35:39 GMT
From: efflandt@xnet.com (David Efflandt)
Subject: Re: Perl & ftp problem
Message-Id: <slrn7iqda7.3bd.efflandt@efflandt.xnet.com>

On 24 Apr 1999 17:53:39 GMT, Nikola Paljetak <paljetak@student.math.hr> wrote:
>
>
>I am just a beginner in Perl-programming and I would appreciate if someone
>could help me to solve this problem:
>
>There is an input_file of given format:
>
>file_name_to_get
>URL_of_1st_ftp_server
>URL_of_2nd_ftp_server
>.
>.
>.
>URL_of_Nth_ftp_server
>
>
>I should write a Perl-script which somehow, USING STANDARD UNIX FTP PROGRAM,
>does the following:
>
>Script should read from input_file the file_name_to_get and the
>URL_of_1st_ftp_server and then try to connect to that server. If the
>connection with server is not established in 60 sec (this is problematic - I
>don't know how to do it), program gives up that host and tryes to connect to
>the following one from the input_file!
>
>If the connection is established, program gets the file from that host.
>If the transfer is succesfully ended, the program quits, but if not, it
>should try to connect again after 60 sec and continue with transfer.
>
>If transfer does not succeed after the third try, program has to go to the
>next host from input_file, etc...

Why does it have to use STANDARD UNIX FTP PROGRAM?  There is a perl module
that can do it easier.  I think the default timeout is 2 minutes, but it
can be changed.  See 'perldoc Net::FTP' and if that is blank, download it
from CPAN (it didn't come in RedHat Linux, but it is on my ISP).

-- 
David Efflandt    efflandt@xnet.com
http://www.xnet.com/~efflandt/


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

Date: Mon, 03 May 1999 04:20:48 GMT
From: cyberunity@my-dejanews.com
Subject: perl sorting ?
Message-Id: <7gj86t$3fg$1@nnrp1.dejanews.com>

Hi,

I am new to perl and would like to ask a question.

If I have the file as shown below:

A1  100
A3  97
A2  200
A8  195

and I would like to have the output sorted in descending order as shown below:

A3  97
A1  100
A8  195
A2  200

Can someone help me on this ?

Thanks in advance.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


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

Date: 02 May 1999 22:39:17 -0700
From: Mike Coffin <mhc@Eng.Sun.COM>
Subject: Re: perl sorting ?
Message-Id: <8p6btg2k8ze.fsf@Eng.Sun.COM>

cyberunity@my-dejanews.com writes:

> Hi,
> 
> I am new to perl and would like to ask a question.
> 
> If I have the file as shown below:
> 
> A1  100
> A3  97
> A2  200
> A8  195
> 
> and I would like to have the output sorted in descending order 
> as shown below:
> 
> A3  97
> A1  100
> A8  195
> A2  200
> 
> Can someone help me on this ?

That's ascending order.  Try

  perl -e 'print sort {substr($a,4) <=> substr($b,4)} <STDIN>' < filename

-mike


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

Date: 3 May 1999 05:49:34 GMT
From: efflandt@xnet.com (David Efflandt)
Subject: Re: Remote access in CGI
Message-Id: <slrn7iqe49.3bd.efflandt@efflandt.xnet.com>

On Sat, 24 Apr 1999 22:24:58 GMT, multilinks@my-dejanews.com
<multilinks@my-dejanews.com> wrote:
>
>I have a perl CGI script that I want to do one task when someone visiting my
>web site uses it and do something else when accessed from a remote host or
>refer.	I am using :
>
>$remote = $ENV{'REMOTE_ADDR'};
>
>$refer = $ENV{'HTTP_REFERER'};
>
>$ENV{'REMOTE_ADDR'} does not help and other hosts are able to cloak themselves
>and make $ENV{'HTTP_REFERER'} return my web site's address.
>
>So I have no sure way to identify who is accessing my web site from a browser
>and who is using a script to access my script from another server.
>
>Can someone help me.  Thanks, Vladimir.

You *MIGHT* be able to tell where somebody is from by there IP, but not
how they got to your site.  HTTP_REFERER is unreliable, not passed by all
browsers, and easy to fake.  The web by its nature is public unless you
password protect it, so the answer to your question is no.

-- 
David Efflandt    efflandt@xnet.com
http://www.xnet.com/~efflandt/


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

Date: Mon, 03 May 1999 04:13:09 GMT
From: cyberunity@my-dejanews.com
Subject: sorting data in perl
Message-Id: <7gj7ok$30g$1@nnrp1.dejanews.com>

Hi,

I am a new to PERL and would like to ask the following question:

If I have an array as follow:

A1    200
BC    305
EF    100
DE    210

and I would like to sort this array so that the output should be like:

EF     100
A1     200
DE     210
BC     305

Thanks in advance,

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


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

Date: Mon, 03 May 1999 04:39:32 GMT
From: tvn007@my-dejanews.com
Subject: Sorting on PERL
Message-Id: <7gj9a3$4aa$1@nnrp1.dejanews.com>

Hi,

I am new to perl and would like to ask a quick question.

If I have the follwing data in a file as shown below:
(the data has two columns)

A1  100
A3  96
A8  200
A9  105

and would like to have the output sorted in descending order as shown below:

A3 96
A1 100
A9 105
A8 200

Would someone help me on this.

Thanks in advance.



-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


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

Date: 03 May 1999 00:56:38 -0400
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Sorting on PERL
Message-Id: <x790b64upl.fsf@home.sysarch.com>

>>>>> "t" == tvn007  <tvn007@my-dejanews.com> writes:

  t> I am new to perl and would like to ask a quick question.

why did you post this at least 3 times? why not wait for a response
which may take hours or even days? that is simple usenet netiquette.

as for the sort question, read perlfaq4, how do i sort an array by
anything. a numeric sort in a field is covered. in the future you could
use the module i am currently developing for release this summer.

uri

-- 
Uri Guttman  -----------------  SYStems ARCHitecture and Software Engineering
uri@sysarch.com  ---------------------------  Perl, Internet, UNIX Consulting
Have Perl, Will Travel  -----------------------------  http://www.sysarch.com
The Best Search Engine on the Net -------------  http://www.northernlight.com


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

Date: Sun, 2 May 1999 22:55:54 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Sorting on PERL
Message-Id: <MPG.1196da4712d9363698999e@nntp.hpl.hp.com>

In article <7gj9a3$4aa$1@nnrp1.dejanews.com> on Mon, 03 May 1999 
04:39:32 GMT, tvn007@my-dejanews.com <tvn007@my-dejanews.com> says...
> I am new to perl and would like to ask a quick question.

Are you also new to the Usenet?

What is going on here?  Three posts, three subjects, two email
addresses.  Sheesh!

> If I have the follwing data in a file as shown below:
> (the data has two columns)
> 
> A1  100
> A3  96
> A8  200
> A9  105
> 
> and would like to have the output sorted in descending order as shown below:
> 
> A3 96
> A1 100
> A9 105
> A8 200

That looks like ascending order!

Go read perlfaq4:  "How do I sort an array by (anything)?", after you 
have learned about Usenet protocol.

-- 
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Mon, 03 May 1999 00:50:21 -0400
From: Bob Walton <walton@frontiernet.net>
To: "R. David Whitlock" <ryandav@u.washington.edu>
Subject: Re: stuctured data object for export
Message-Id: <372D2B0C.4B981398@frontiernet.net>

Dave, you will want to learn about references -- read perlref.  You could
package up your arrays and string into one scalar reference as follows:

sub mysub{
#....setting up arrays part of sub
$ref=[\@Array1,\@Array2,\@Array3,\@Array4,\@Array5,\$string];
return $ref;
}

Then, in the calling routine, pick up the reference:

$ref=&mysub('whatever');

and get the values like:

$$ref[0][0]   returns value of $Array1[0]
$$ref[0][1]   returns value of $Array1[1]
$$ref[1][2]   returns value of $Array2[2]
@{$$ref[1]}returns @Array2
${$$ref[2]}  returns value of $string
etc etc

$ref is a reference to an array of references, the first five of which are
references to arrays, and the last of which is a reference to a scalar.

"R. David Whitlock" wrote:

> I'm having some troubles with my move down the path of perl tao, namely my
> comprehension of the construction techniques sometimes used to create
> happy little structured data objects suitable for returning to other
> programs to be accessed in nice readable ways....
>
> I would like to ask the collective assistance of a perl guru or two to
> help me get past this minor setback, fully acknowledging the ever present
> tenet of all perly knowledge, There's More Than One Way To Do It...
>
> I have a subroutine which calls other subroutines to generate several
> data objects and which needs to then return some sort of data structure
> which contains all of the data it has collected.  I have three arrays of
> variable length and a long string that I need to give back to any program
> calling my function, and I need to return a single item ( a hash I guess)
> so that someone could easily extract the info they seek from the object.
>
> I am visualizing something like the following:
>
>         my @Array1 = &Func1(...);
>
>         my @Array2 = &Func2(...);
>
>         my @Array3 = &Func3(...);
>
>         my $String = &Func4(...);
>
>         foreach $foo (@Array1) {
>
>                 assignment into my hash or whatnot
>
>         }
>
>         # repeat addition to hash or whatnot until I have a final %hash
>         # and then...
>
>         return %hash
>
> So that someone can then call the program, receive the %hash from the
> return, and then look at the elements, like say, all the $foo's in
> @Array1.
>
> The part I am unable to comprehend would seem to be the heart of this
> function, spooling all of this data into some nice object to ship off to
> elsewhere.
>
> [snip]
>
> Thanks.
>
> David (aKa, "Slave of the layer2 devices")



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

Date: 3 May 1999 04:02:35 GMT
From: sholden@pgrad.cs.usyd.edu.au (Sam Holden)
Subject: Re: Whats wrong?
Message-Id: <slrn7iq7ur.aue.sholden@pgrad.cs.usyd.edu.au>

On 02 May 1999 23:23:22 -0400, esalmon@packet.net <esalmon@packet.net> wrote:
>How do I make the following systen command work? And possibly return the
>results?
>
>$userid = 'whoami';

You read the documentation in perlop and use the the quotes it says to use.


-- 
Sam

Perl was designed to be a mess (though in the nicest of possible ways). 
	--Larry Wall


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

Date: Mon, 03 May 1999 00:06:24 -0400
From: Bob Walton <walton@frontiernet.net>
To: esalmon@packet.net
Subject: Re: Whats wrong?
Message-Id: <372D20BF.F8E77F7A@frontiernet.net>

Use backticks (`) instead of apostrophes ('), like:

$userid=`whoami`;

On many keyboards, the backtick is in the upper lefthand corner of the main
part of the keyboard.

esalmon@packet.net wrote:

> How do I make the following systen command work? And possibly return the
> results?
>
> $userid = 'whoami';



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

Date: 02 May 1999 21:48:39 -0700
From: Mike Coffin <mhc@Eng.Sun.COM>
Subject: Re: Whats wrong?
Message-Id: <8p6g15ekbbs.fsf@Eng.Sun.COM>

esalmon@packet.net writes:

> How do I make the following systen command work? And possibly return the
> results?
> 
> $userid = 'whoami';

$userid = `whoami`;


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

Date: 3 May 1999 04:04:28 GMT
From: sholden@pgrad.cs.usyd.edu.au (Sam Holden)
Subject: Re: writing binary data to a file
Message-Id: <slrn7iq82c.aue.sholden@pgrad.cs.usyd.edu.au>

03 May 1999 00:01:55 -0400, Frank Hale <frankhale@worldnet.att.net> wrote:
>
>This produces a normal text file with the following data
>
>123
>
>When in fact I want the data in binary for the numbers 1,2,3.

You could use pack()

Documented in man perlfunc.

-- 
Sam

In case you hadn't noticed, Perl is not big on originality.
	--Larry Wall


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

Date: Mon, 03 May 1999 04:22:51 GMT
From: mcafee@waits.facilities.med.umich.edu (Sean McAfee)
Subject: Re: writing binary data to a file
Message-Id: <vw9X2.427$OZ6.17428@news.itd.umich.edu>

In article <372D1FB3.E1ED6788@worldnet.att.net>,
Frank Hale  <frankhale@worldnet.att.net> wrote:
>I'm on RedHat Linux 5.2. The man pages says that binmode has no effect
>on Unix systems since it doesn't distinguish between binary and text
>files.

Yes, thank all that's holy.

>I tried the following script

>#!/usr/bin/perl
>$a = 0x01;
>$b = 0x02;
>$c = 0x03;

Do you realize that this is identical to "$a = 1; $b = 2; $c = 3;" as far
as the interpreter is concerned?  If you really want the variables to
contain one-byte strings with the given ordinal values, either do this:

$a = chr(1);  $b = chr(2);  $c = chr(3);

Or:

$a = "\x1";  $b = "\x2";  $c = "\x3";

>open (BIN, ">bin.dat");
>print BIN $a,$b,$c;

As an alternative to the above, you can leave the "$a = 1; etc." and here
say:

printf BIN "%c%c%c", $a, $b, $c;

>This produces a normal text file with the following data
>123

People want to print string representations of numbers much more often
than they want to print binary data, so it's natural that the former is the
default.  You have to do a little bit of extra work to do the latter.

-- 
Sean McAfee                                                mcafee@umich.edu
print eval eval eval eval eval eval eval eval eval eval eval eval eval eval
q!q@q#q$q%q^q&q*q-q=q+q|q~q:q? Just Another Perl Hacker ?:~|+=-*&^%$#@!


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

Date: 02 May 1999 21:55:07 -0700
From: Mike Coffin <mhc@Eng.Sun.COM>
Subject: Re: writing binary data to a file
Message-Id: <8p6emkykb10.fsf@Eng.Sun.COM>

Frank Hale <frankhale@worldnet.att.net> writes:

> I didn't know this was such a funny subject. Please I am not a perl guru
> as most of you here. 
> 
> I'm on RedHat Linux 5.2. The man pages says that binmode has no effect
> on Unix systems since it doesn't distinguish between binary and text
> files.
> 
> I tried the following script
> 
> #!/usr/bin/perl
> 
> $a = 0x01;
> $b = 0x02;
> $c = 0x03;
> 
> open (BIN, ">bin.dat");
> print BIN $a,$b,$c;
> close (BIN);
> 
> This produces a normal text file with the following data
> 
> 123
> 
> When in fact I want the data in binary for the numbers 1,2,3.

You are assigning $a, $b, and $c the integers 1, 2, and 3,
respectively, and then printing them.  Try the pack and unpack
functions. E.g., 

  perl -e 'print unpack("B32", pack("n", 7)), "\n"'
  0000000000000111

-mike




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

Date: Sun, 2 May 1999 22:12:12 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: writing binary data to a file
Message-Id: <MPG.1196cfffb186593098999c@nntp.hpl.hp.com>

[Posted and a courtesy copy sent.]

In article <372D1FB3.E1ED6788@worldnet.att.net> on Mon, 03 May 1999 
00:01:55 -0400, Frank Hale <frankhale@worldnet.att.net> says...
 ... 
> #!/usr/bin/perl
> 
> $a = 0x01;
> $b = 0x02;
> $c = 0x03;
> 
> open (BIN, ">bin.dat");
> print BIN $a,$b,$c;
> close (BIN);
> 
> This produces a normal text file with the following data
> 
> 123
> 
> When in fact I want the data in binary for the numbers 1,2,3.

No matter whether you write them in decimal or hex, the values of the 
variables are the integers 1, 2, and 3, which print() converts to the 
strings '1', '2', and '3'.

What you need are the characters whose binary values are 1, 2, and 3.  
There are several ways to accomplish that.

Sam Holden suggested pack(), which is fine (the format is 'C*', or 'C3' 
in this case).  Other ways are to use chr(1), chr(2), and chr(3); or to 
use "\x01", "\x02", and "\x03" or "\x01\x02\x03".  Your choice, 
depending on how you get your data, and your taste.

-- 
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Sun, 2 May 1999 20:59:43 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: writing binary files in Perl
Message-Id: <MPG.1196bf0c9277180098999b@nntp.hpl.hp.com>

In article <1dr798z.rv09cfkd8qlaN@p54.tc1.state.ma.tiac.com> on Sun, 2 
May 1999 23:02:20 -0400, Ronald J Kimball <rjk@linguist.dartmouth.edu> 
says...
> Frank Hale <frankhale@worldnet.att.net> wrote:
> > This method produces a text file.
> > 
> > open (BIN, ">binary.dat");
> 
> binmode(BIN);
> 
> > print BIN @binary_data;
> > close BIN;
> 
> On operating systems which distinguish between text and binary files,
> binmode() will prevent line endings from being translated on input and
> output.

Pedantry alert!

Tad McLellan wrote (several times today; thank you, Tad):

   Precise terminology is important when discussing technical topics :-)

Binary files don't have lines, so they don't have 'line endings'.  ITYM 
'character sequences that -- in text files -- would represent line 
endings'.

Shoot me now!  :-)

-- 
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: 12 Dec 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Special: Digest Administrivia (Last modified: 12 Dec 98)
Message-Id: <null>


Administrivia:

Well, after 6 months, here's the answer to the quiz: what do we do about
comp.lang.perl.moderated. Answer: nothing. 

]From: Russ Allbery <rra@stanford.edu>
]Date: 21 Sep 1998 19:53:43 -0700
]Subject: comp.lang.perl.moderated available via e-mail
]
]It is possible to subscribe to comp.lang.perl.moderated as a mailing list.
]To do so, send mail to majordomo@eyrie.org with "subscribe clpm" in the
]body.  Majordomo will then send you instructions on how to confirm your
]subscription.  This is provided as a general service for those people who
]cannot receive the newsgroup for whatever reason or who just prefer to
]receive messages via e-mail.

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.  

To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.

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.

The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.

The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.

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 V8 Issue 5547
**************************************

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