Re: assigning list to hash entry

2006-01-16 Thread Anders Stegmann
Thanks for replying! 
 
Actually, I have a highly complex datastructure in which both strings,
lists and hashes are values in a primary hash. 
 
e.g. 
 
$hash{$key}[0] = @list; 
$hash{$key}[1] = $string; 
$hash{$key}[2] = %hash; 
 
I need to assign these values to a specific (number)entry in the primary
hash, so I know which entry has which value. 
 
Anders. 
 


Shawn Corey [EMAIL PROTECTED] 01/15/06 7:16 pm 
Anders Stegmann wrote:
Hi!
 
how do I assign a list to a hash entry like $hash{$key}[0].
 
I mean, something like this:
 
$hash{$key}[0] = @list;
 
must work.
 
Anders.



Close. Try:

use Data::Dumper;
$hash{$key} = [ @list ];
print Dumper( \%hash );


--

Just my 0.0002 million dollars worth,
   --- Shawn

Probability is now one. Any problems that are left are your own.
   SS Heart of Gold, _The Hitchhiker's Guide to the

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is available at http://perldoc.perl.org/

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response






Re: assigning list to hash entry

2006-01-16 Thread Xavier Noria

On Jan 16, 2006, at 9:03, Anders Stegmann wrote:


Actually, I have a highly complex datastructure in which both strings,
lists and hashes are values in a primary hash.

e.g.

$hash{$key}[0] = @list;
$hash{$key}[1] = $string;
$hash{$key}[2] = %hash;


In Perl data estructures can only store scalar values. That's why  
references are used to emulate nested structures:


$hash{$key}[0] = [EMAIL PROTECTED]; # note that @s are arrays, not lists
$hash{$key}[1] = $string;
$hash{$key}[2] = \%hash;

There are some pages in the documentation about nested estructures,  
have a glance at perldsc for instance.


-- fxn


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: about the var's scope

2006-01-16 Thread John Doe
Shawn Corey am Montag, 16. Januar 2006 04.12:
[...]
  Ok, it would be interesting to look deeper into the mess of different
  variables all named with the same name $q, exported across the modules,
  overwritten by several imports...
 
  What do you want to achieve with your code? It looks really strange (hm,
  at least to me).
 
  joe

 All the variables $q in the packages have been shunted aside into the
 deep, dark bit bucket of oblivion.

 As I said before:

$main::q = \*My::HTML::q;
$main::q = \*My::Doc::q;

 $My::HTML::q and $My::Doc::q no longer exist; they are aliases to
 $main::q. In the modules, $q no longer exists; it is an alias for
 $main::q. Whenever you say $q in the modules, you really mean $main::q.
 The modules do not import anything; they export any changes to $main::q;
 via the phrase '$q'.

Hi again Shawn,

I have a question concerning the code presented in the OP. I repeat it for 
better overview:

 Version 1 
  script.pl:
  
  use vars qw($q);
  use CGI;
  use lib qw(.); 
  use My::HTML qw($q); # My/HTML.pm is in the same dir as script.pl
  use My::Doc  qw($q); # Ditto
  $q = new CGI;

  My::HTML::printmyheader();  
  
  My/HTML.pm
  
  package My::HTML;
  use strict;

  BEGIN {
use Exporter ();
@My::HTML::ISA = qw(Exporter);
@My::HTML::EXPORT  = qw();
@My::HTML::EXPORT_OK   = qw($q);
  }
  use vars qw($q);
  use My::Doc  qw($q);
  sub printmyheader{
# Whatever you want to do with $q... e.g.
print $q-header();
My::Doc::printtitle('Guide');
  }
  1;  
  
  My/Doc.pm
  
  package My::Doc;
  use strict;

  BEGIN {
use Exporter ();
@My::Doc::ISA = qw(Exporter);
@My::Doc::EXPORT  = qw();
@My::Doc::EXPORT_OK   = qw($q);
  }
  use vars qw($q);
  sub printtitle{
my $title = shift || 'None';
print $q-h1($title);
  }
  1;
 END Version 1

The code demonstrates the usage of the use vars pragma and the Exporter.

However, my personal feeling ist that in a bigger project it is eventually bad 
style to use globals this way?!?

Do you agree? Or do I  - again - overlook something?

See the equivalent code below as an alternative:

 Version 2 
  script.pl:
  
  use CGI;
  use lib qw(.); 
  use My::HTML;
  my $q = new CGI;

  My::HTML::printmyheader($q);  
  
  My/HTML.pm
  
  package My::HTML;
  use strict;

  use My::Doc;
  sub printmyheader{
   my $q=shift;
   ### DO CHECKS HERE
   # Whatever you want to do with $q... e.g.
   print $q-header();
   My::Doc::printtitle($q, 'Guide');
  }
  1;  
  
  My/Doc.pm
  
  package My::Doc;
  use strict;

  sub printtitle{
my $q=shift;
### DO CHECKS HERE
my $title = shift || 'None';
print $q-h1($title);
  }
  1;
 END Version 2

This version does the same, is shorter, is easier to understand for some 
people, does not require Exporter, and does not need use'ing My::Doc in the 
main script (it's more modular).

Does this 2nd version lack any features/magic present in the 1st?
(Apart from demonstrating use vars / Exporter of course)

Any comments are very appreciated!

greetings 
joe

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: about the var's scope

2006-01-16 Thread Jeff Pang
I think the only difference between the two is Stat's code do the things of 
sharing vars across modules really.
Under mod_perl,the situation is very different from common CGI environment,and 
the vars sharing sometimes is useful and needed.
I hope I'm correct.If not,the criticism are welcome.

-Original Message-
From: John Doe [EMAIL PROTECTED]
Sent: Jan 16, 2006 4:09 AM
To: beginners@perl.org
Subject: Re: about the var's scope

Shawn Corey am Montag, 16. Januar 2006 04.12:
[...]
  Ok, it would be interesting to look deeper into the mess of different
  variables all named with the same name $q, exported across the modules,
  overwritten by several imports...
 
  What do you want to achieve with your code? It looks really strange (hm,
  at least to me).
 
  joe

 All the variables $q in the packages have been shunted aside into the
 deep, dark bit bucket of oblivion.

 As I said before:

$main::q = \*My::HTML::q;
$main::q = \*My::Doc::q;

 $My::HTML::q and $My::Doc::q no longer exist; they are aliases to
 $main::q. In the modules, $q no longer exists; it is an alias for
 $main::q. Whenever you say $q in the modules, you really mean $main::q.
 The modules do not import anything; they export any changes to $main::q;
 via the phrase '$q'.

Hi again Shawn,

I have a question concerning the code presented in the OP. I repeat it for 
better overview:

 Version 1 
  script.pl:
  
  use vars qw($q);
  use CGI;
  use lib qw(.); 
  use My::HTML qw($q); # My/HTML.pm is in the same dir as script.pl
  use My::Doc  qw($q); # Ditto
  $q = new CGI;

  My::HTML::printmyheader();  
  
  My/HTML.pm
  
  package My::HTML;
  use strict;

  BEGIN {
use Exporter ();
@My::HTML::ISA = qw(Exporter);
@My::HTML::EXPORT  = qw();
@My::HTML::EXPORT_OK   = qw($q);
  }
  use vars qw($q);
  use My::Doc  qw($q);
  sub printmyheader{
# Whatever you want to do with $q... e.g.
print $q-header();
My::Doc::printtitle('Guide');
  }
  1;  
  
  My/Doc.pm
  
  package My::Doc;
  use strict;

  BEGIN {
use Exporter ();
@My::Doc::ISA = qw(Exporter);
@My::Doc::EXPORT  = qw();
@My::Doc::EXPORT_OK   = qw($q);
  }
  use vars qw($q);
  sub printtitle{
my $title = shift || 'None';
print $q-h1($title);
  }
  1;
 END Version 1

The code demonstrates the usage of the use vars pragma and the Exporter.

However, my personal feeling ist that in a bigger project it is eventually bad 
style to use globals this way?!?

Do you agree? Or do I  - again - overlook something?

See the equivalent code below as an alternative:

 Version 2 
  script.pl:
  
  use CGI;
  use lib qw(.); 
  use My::HTML;
  my $q = new CGI;

  My::HTML::printmyheader($q);  
  
  My/HTML.pm
  
  package My::HTML;
  use strict;

  use My::Doc;
  sub printmyheader{
   my $q=shift;
   ### DO CHECKS HERE
   # Whatever you want to do with $q... e.g.
   print $q-header();
   My::Doc::printtitle($q, 'Guide');
  }
  1;  
  
  My/Doc.pm
  
  package My::Doc;
  use strict;

  sub printtitle{
my $q=shift;
### DO CHECKS HERE
my $title = shift || 'None';
print $q-h1($title);
  }
  1;
 END Version 2

This version does the same, is shorter, is easier to understand for some 
people, does not require Exporter, and does not need use'ing My::Doc in the 
main script (it's more modular).

Does this 2nd version lack any features/magic present in the 1st?
(Apart from demonstrating use vars / Exporter of course)

Any comments are very appreciated!

greetings 
joe

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




--
http://home.earthlink.net/~pangj/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Combine multiple lines into one line

2006-01-16 Thread Andrej Kastrin

Hi all,

I have the file, which looks like:

*RECORD*
*ID*
001
*TITLE*
Here is title number one.
*ABSTRACT*
First sentence of the abstract. Second sentence of the abstract...
Second line of the abstract.

*RECORD*
*ID*
002
*TITLE*
Here is title number one.
*ABSTRACT*
First sentence of the abstract. Second sentence of the abstract...
Second line of the abstract.

Is there any simple way to transform this file to look like:
*RECORD*
*ID* 001
*TITLE* Here is title number one.
*ABSTRACT* First sentence of the abstract. Second sentence of the 
abstract. Second line of the abstract.


Thanks in advance for any pointers or notes.

Cheers, Andre


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Combine multiple lines into one line

2006-01-16 Thread Bjørge Solli
On Monday 16 January 2006 14:32, Andrej Kastrin wrote:
 Hi all,

Hi Andrej

 I have the file, which looks like:

 *RECORD*
 *ID*
 001
 *TITLE*
 Here is title number one.
 *ABSTRACT*
 First sentence of the abstract. Second sentence of the abstract...
 Second line of the abstract.

 *RECORD*
 *ID*
 002
 *TITLE*
 Here is title number one.
 *ABSTRACT*
 First sentence of the abstract. Second sentence of the abstract...
 Second line of the abstract.

 Is there any simple way to transform this file to look like:
 *RECORD*
 *ID* 001
 *TITLE* Here is title number one.
 *ABSTRACT* First sentence of the abstract. Second sentence of the
 abstract. Second line of the abstract.

 Thanks in advance for any pointers or notes.

I'm also new to this game, but I'll try:

#!/usr/bin/perl

open FILEHANDLE, yourfile.txt or die die\n;

#optional
use strict;

#declare array
my @line;

# go trough each line
# shortcut for:
#   my $_;
#   while ( defined ( $_ = FILEHANDLE ) ) {
while ( FILEHANDLE ) {

# cut off trailing newline
# shortcut for:
#   chomp $_;
chomp;

#push the line to the array
push ( @line, $_ );
}

# join the elements of the array and print them
print join( , @line);
close FILEHANDLE;

I guess this should give you some hints about how to manipulate those files of 
yours, since you don't want to join *all* the file in one line. One way can 
be to check for lines beginning with '*' (using regexp /^\*/ or something 
similar) and print those lines with a newline-prefix and the others with a 
single-space-prefix.

There is probably some fancy shortcut for doing all of this in one line, but I 
don't know it. Yet...

Enjoy!

-- 
Bjørge Solli - Office:+47 55205847 cellph.:+47 91614343
Nansen Environmental and Remote Sensing Center - Bergen, Norway
http://www.nersc.no Reception: +47 55205800
Dept.: Mohn-Sverdrup Center for Global Ocean Studies 
   and Operational Oceanography

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




perl-module ParseLex-2.15 install fails on make test

2006-01-16 Thread Bjørge Solli
System: Fedora Core 4 on a AMD64.

I've tried using the cpan-command
install Parse::Lex
and the manual 'perl Makefile.pl  make  make test'(it fails on the test)

Let me know if this is the wrong list, but I really am a beginner when it 
comes to perl. I am trying to install a Live Access Server made in perl.

Here is the output:

###

[EMAIL PROTECTED] ParseLex-2.15]# perl Makefile.PL
Checking if your kit is complete...
Looks good
Writing Makefile for Parse::Lex
[EMAIL PROTECTED] ParseLex-2.15]# make
cp lib/Parse/ALex.pm blib/lib/Parse/ALex.pm
cp lib/Parse/Token.pm blib/lib/Parse/Token.pm
cp lib/Parse/Token-t.pm blib/lib/Parse/Token-t.pm
cp lib/Parse/CLex.pm blib/lib/Parse/CLex.pm
cp lib/Parse/YYLex.pm blib/lib/Parse/YYLex.pm
cp lib/Parse/LexEvent.pm blib/lib/Parse/LexEvent.pm
cp lib/Parse/Template.pm blib/lib/Parse/Template.pm
cp lib/Parse/Lex.pm blib/lib/Parse/Lex.pm
cp lib/Parse/Trace.pm blib/lib/Parse/Trace.pm
Manifying blib/man3/Parse::Token.3pm
Manifying blib/man3/Parse::Template.3pm
Manifying blib/man3/Parse::CLex.3pm
Manifying blib/man3/Parse::YYLex.3pm
Manifying blib/man3/Parse::Lex.3pm
Manifying blib/man3/Parse::LexEvent.3pm
[EMAIL PROTECTED] ParseLex-2.15]# make test
PERL_DL_NONLAZY=1 /usr/bin/perl -MExtUtils::Command::MM -e 
test_harness(0, 'blib/lib', 'blib/arch') t/*.t
t/test1FAILED test 1
Failed 1/1 tests, 0.00% okay
t/test2FAILED test 1
Failed 1/1 tests, 0.00% okay
t/test3FAILED test 1
Failed 1/1 tests, 0.00% okay
t/test4FAILED test 1
Failed 1/1 tests, 0.00% okay
t/test5FAILED test 1
Failed 1/1 tests, 0.00% okay
t/test6FAILED test 1
Failed 1/1 tests, 0.00% okay
t/test7FAILED test 1
Failed 1/1 tests, 0.00% okay
Failed Test Stat Wstat Total Fail  Failed  List of Failed
---
t/test1.t  11 100.00%  1
t/test2.t  11 100.00%  1
t/test3.t  11 100.00%  1
t/test4.t  11 100.00%  1
t/test5.t  11 100.00%  1
t/test6.t  11 100.00%  1
t/test7.t  11 100.00%  1
Failed 7/7 test scripts, 0.00% okay. 7/7 subtests failed, 0.00% okay.
make: *** [test_dynamic] Error 255
[EMAIL PROTECTED] ParseLex-2.15]#

###

Any ideas?

-- 
Bjørge Solli - Office:+47 55205847 cellph.:+47 91614343
Nansen Environmental and Remote Sensing Center - Bergen, Norway
http://www.nersc.no Reception: +47 55205800
Dept.: Mohn-Sverdrup Center for Global Ocean Studies 
   and Operational Oceanography

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




How to take a reference in line

2006-01-16 Thread Bill Gradwohl
Using the example below (partially taken from Learning PERL Objects), I
can't seem to figure out how to take a reference to an array in-line.
See the last 2 lines:

#!/usr/bin/perl

use Data::Dumper;

sub check_required_items {
  my $who = shift;
  my $items = shift;

  my @required = qw(preserver sunscreen water_bottle jacket);
  print \n\n;
  print Dumper $items;
  for my $item (@required) {
print Checking $item:;
unless (grep $item eq $_, @{$items}) { # not found in list?
  print $who is missing $item.\n;
} else {
  print OK!\n;
}
  }  
}

my $arrayPointer;

# This works:
@{$arrayPointer}=qw(Money preserver sunscreen);
check_required_items(Mr. Howell, $arrayPointer);

# These don't work:
check_required_items(Mr. Howell, @{$arrayPointer}=qw(Money preserver 
sunscreen));
check_required_items(Mr. Howell, qw(Money preserver sunscreen));


How do I tell Perl to give me a reference to an array in the last 2
statements? There's got to be a way to pass a reference without having
to explicitly name a variable. Right?

Thank You


-- 
Bill Gradwohl



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Combine multiple lines into one line

2006-01-16 Thread John Doe
Andrej Kastrin am Montag, 16. Januar 2006 14.32:
 Hi all,

 I have the file, which looks like:

 *RECORD*
 *ID*
 001
 *TITLE*
 Here is title number one.
 *ABSTRACT*
 First sentence of the abstract. Second sentence of the abstract...
 Second line of the abstract.

 *RECORD*
 *ID*
 002
 *TITLE*
 Here is title number one.
 *ABSTRACT*
 First sentence of the abstract. Second sentence of the abstract...
 Second line of the abstract.

 Is there any simple way to transform this file to look like:
 *RECORD*
 *ID* 001
 *TITLE* Here is title number one.
 *ABSTRACT* First sentence of the abstract. Second sentence of the
 abstract. Second line of the abstract.

 Thanks in advance for any pointers or notes.

 Cheers, Andre

The following does what you want in a somehow generic way (except from the 
additional empty line at the very end), but it is more short than simple, and 
there must exist an easier regex I have not found:

use strict; use warnings;
local $/=;
while(DATA) {
  s/((?=\*)|(?!\*))\n(?!\*|$)/ /gms;
  print;
}
__DATA__
*RECORD*
*ID*
001
*TITLE*
Here is title number one.
*ABSTRACT*
First sentence of the abstract. Second sentence of the abstract...
Second line of the abstract.

*RECORD*
*ID*
002
*TITLE*
Here is title number one.
*ABSTRACT*
First sentence of the abstract. Second sentence of the abstract...
Second line of the abstract.

### prints:

*RECORD*
*ID* 001
*TITLE* Here is title number one.
*ABSTRACT* First sentence of the abstract. Second sentence of the abstract... 
Second line of the abstract.

*RECORD*
*ID* 002
*TITLE* Here is title number one.
*ABSTRACT* First sentence of the abstract. Second sentence of the abstract... 
Second line of the abstract.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: about the var's scope

2006-01-16 Thread Shawn Corey

Jeff Pang wrote:

I think the only difference between the two is Stat's code do the things of 
sharing vars across modules really.
Under mod_perl,the situation is very different from common CGI environment,and 
the vars sharing sometimes is useful and needed.
I hope I'm correct.If not,the criticism are welcome.


The OP was asking about understanding the code, not what are the best 
practices. First of all, I would never call an object by a single 
letter. Something like $CGI_Obj would be better. And being a global, it 
starts with a capital.


Yes, I would be more inclined to pass the object to the subroutines than 
use it as a global. I would use Exporter only to export subroutines, not 
variables because, as discussed, it is difficult to understand what is 
happening.


I don't use mod_perl but my understanding it that it doesn't, or didn't, 
reset the globals to the default state (which is undef). Of course, 
setting all your variables to a known state before using them is 
consider good practice; so you should never encounter this problem ;)



--

Just my 0.0002 million dollars worth,
   --- Shawn

Probability is now one. Any problems that are left are your own.
   SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is available at http://perldoc.perl.org/

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: assigning list to hash entry

2006-01-16 Thread Shawn Corey

Xavier Noria wrote:
In Perl data estructures can only store scalar values. That's why  
references are used to emulate nested structures:


$hash{$key}[0] = [EMAIL PROTECTED]; # note that @s are arrays, not lists
$hash{$key}[1] = $string;
$hash{$key}[2] = \%hash;

There are some pages in the documentation about nested estructures,  
have a glance at perldsc for instance.


An alternative would be:
  $hash{$key}[0] = [ @array ];
and
  $hash{$key}[2] = { %hash };

Here you are making a copy of the the array and hash. In the above, if 
you change @array or %hash, then the contents of $hash{$key}[0] and 
$hash{$key}[2] also change. It depends in what you want; sometimes you 
want it one way, sometimes the other.


Also you could have written:
  $hash{$key}[1] = \$string;
So that when $string changes, $hash{$key}[1] also changes.


--

Just my 0.0002 million dollars worth,
   --- Shawn

Probability is now one. Any problems that are left are your own.
   SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is available at http://perldoc.perl.org/

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Combine multiple lines into one line

2006-01-16 Thread Bob Showalter

Andrej Kastrin wrote:

Hi all,

I have the file, which looks like:

*RECORD*
*ID*
001
*TITLE*
Here is title number one.
*ABSTRACT*
First sentence of the abstract. Second sentence of the abstract...
Second line of the abstract.

*RECORD*
*ID*
002
*TITLE*
Here is title number one.
*ABSTRACT*
First sentence of the abstract. Second sentence of the abstract...
Second line of the abstract.

Is there any simple way to transform this file to look like:
*RECORD*
*ID* 001
*TITLE* Here is title number one.
*ABSTRACT* First sentence of the abstract. Second sentence of the 
abstract. Second line of the abstract.




A one-liner:

  $ perl -lp0e 's/\n(?!\*[A-Z]+\*)/ /g' myfile.dat

How it works:

1. Read input in paragraph mode (-0) option

2. Join any line that does not begin with *LETTERS* with the preceding line.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: interpolation

2006-01-16 Thread Shawn Corey

John Doe wrote:

The Ghost am Montag, 16. Januar 2006 06.34:


I am storing text stings in a database.  when I have the string:

'some perl $variable'

which would print as:

some perl $variable

how can I force interpolation of '$variable'?

one idea I thought of was:
#!/usr/bin/perl
my $var='variable';
$string='some $var';
$string=~s/\$(\w+)/${$1}/gi;
print $string\n;

But it doesn't work.  I want it to print some variable.



One way is to change the regex a bit:

#!/usr/bin/perl
use strict;
use warnings;

my $var='variable';
my $other_var='another';
my ($string1, $string2, $string3)=map 'some $var $other_var', 1..3; 


# test1, test2, final version:
#
$string1=~s/(\$\w+)/$1/g;
$string2=~s/(\$\w+)/$1/ge;
$string3=~s/(\$\w+)/$1/gee;

print join \n, $string1, $string2, $string3;


greetings
joe



Usually it is considered a bad idea to interpolate external strings. You 
could have your users printing any variable. Consider using sprintf 
instead (see `perldoc -f sprintf`).


my $format = 'some perl %s';
my $string = sprintf $format, $variable;
print $string\n;


--

Just my 0.0002 million dollars worth,
   --- Shawn

Probability is now one. Any problems that are left are your own.
   SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is available at http://perldoc.perl.org/

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Array of hashes

2006-01-16 Thread balan.ranganathan


Hi All

Can anyone tell me whether there is any way for declaring an array of
hashes similar to creating array of structure variables in C
programming?

Thanks

Best regards
Bala


The information contained in this electronic message and any attachments to 
this message are intended for the exclusive use of the addressee(s) and may 
contain proprietary, confidential or privileged information. If you are not the 
intended recipient, you should not disseminate, distribute or copy this e-mail. 
Please notify the sender immediately and destroy all copies of this message and 
any attachments.

WARNING: Computer viruses can be transmitted via email. The recipient should 
check this email and any attachments for the presence of viruses. The company 
accepts no liability for any damage caused by any virus transmitted by this 
email.

www.wipro.com

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: How to take a reference in line

2006-01-16 Thread Shawn Corey

Bill Gradwohl wrote:

my $arrayPointer;

# This works:
@{$arrayPointer}=qw(Money preserver sunscreen);
check_required_items(Mr. Howell, $arrayPointer);

# These don't work:
check_required_items(Mr. Howell, @{$arrayPointer}=qw(Money preserver 
sunscreen));
check_required_items(Mr. Howell, qw(Money preserver sunscreen));


How do I tell Perl to give me a reference to an array in the last 2
statements? There's got to be a way to pass a reference without having
to explicitly name a variable. Right?


Try:
  check_required_items(Mr. Howell, $arrayPointer=[qw(Money preserver 
sunscreen)] );

  check_required_items(Mr. Howell, [qw(Money preserver sunscreen)] );


--

Just my 0.0002 million dollars worth,
   --- Shawn

Probability is now one. Any problems that are left are your own.
   SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is available at http://perldoc.perl.org/

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Array of hashes

2006-01-16 Thread Shawn Corey

[EMAIL PROTECTED] wrote:


Hi All

Can anyone tell me whether there is any way for declaring an array of
hashes similar to creating array of structure variables in C
programming?


There is a module, Class::Struct, that might be what you want. See 
`perldoc Class:Struct`.


However, I would simply build the array of hashes as I go:

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my @ArrayOfHashes = ();

for my $count ( 1 .. 10 ){

  my %hash = ();
  for my $key ( 'a' .. 'z' ){
my $value = rand;
$hash{$key} = $value;
  }

  push @ArrayOfHashes, { %hash };
}

print Dumper [EMAIL PROTECTED];

__END__


--

Just my 0.0002 million dollars worth,
   --- Shawn

Probability is now one. Any problems that are left are your own.
   SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is available at http://perldoc.perl.org/

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: [Win32] Basic I/O Question

2006-01-16 Thread Timothy Johnson

The diamond operator works fine without escaping your backslashes, but I
recommend putting quotes around your arguments.  The following works
just fine for me:

 c:\ while.pl c:\documents and settings\username\desktop\file.txt


If you want to use the less-than operator (or a pipe), then you have to
explicitly state that you are using perl.exe.  cmd.exe can't derive
implicitly the program you are invoking via the extension.

For example:

 c:\ perl.exe while.pl  ..\myfile.txt





-Original Message-
From: Hardly Armchair [mailto:[EMAIL PROTECTED] 
Sent: Sunday, January 15, 2006 6:41 PM
To: beginners@perl.org
Subject: [Win32] Basic I/O Question

Hello List,

I am running Perl for Win32 and have been executing my
programs through the 'cmd.exe' shell.

I am confused about how to input paths to files in a
command-line context so that perl will understand.

Using the diamond operator () in my programs allows
me to type my program at the command line followed by
the filename to get the program to work on the file:

foo.plx filename.txt#works

However, when I use standard input (STDIN) and use
the less than character to do the same thing, the
file is ignored:

foo.plx  filename.txt  #no dice

snip




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: dprofpp

2006-01-16 Thread Chris Devers
On Fri, 13 Jan 2006, Gerard Robin wrote:

 And, what are exactly Elapsed Time and User+System Time ?
 
 If i run time ./dprof1.pl the outputs are:
 
 real0m0.033s
 user0m0.011s
 sys 0m0.002s
 
 If i run time ./dprof3.pl the outputs are:
 
 real0m0.059s
 user0m0.018s
 sys 0m0.004s
 
 What relation exists between the times of time an te times of dprofpp ?

The `times` manpage may help, as might `getrusage`:

http://www.hmug.org/man/3/times.php
http://www.hmug.org/man/2/getrusage.php
http://publib16.boulder.ibm.com/pseries/en_US/libs/basetrf1/getrusage_64.htm

The way that IBM version of the manpage is more oriented towards C 
programmers using the call, but the basic idea still gets through:

This information is read from the calling process as well as from each 
completed child process for which the calling process executed a wait 
subroutine.

tms_utime   The CPU time used for executing instructions in the
user space of the calling process
tms_stime   The CPU time used by the system on behalf of the calling
process.
tms_cutime  The sum of the tms_utime and the tms_cutime values for
all the child processes.
tms_cstime  The sum of the tms_stime and the tms_cstime values for
all the child processes.

Note:
The system measures time by counting clock interrupts. The precision
of the values reported by the times subroutine depends on the rate
at which the clock interrupts occur.

Helpful? 


-- 
Chris Devers
DO NOT LEAVE IT IS NOT REAL

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: interpolation

2006-01-16 Thread John Doe
Shawn Corey am Montag, 16. Januar 2006 16.55:
 John Doe wrote:
  The Ghost am Montag, 16. Januar 2006 06.34:
 I am storing text stings in a database.  when I have the string:
 
 'some perl $variable'
 
 which would print as:
 
 some perl $variable
 
 how can I force interpolation of '$variable'?
 
 one idea I thought of was:
 #!/usr/bin/perl
 my $var='variable';
 $string='some $var';
 $string=~s/\$(\w+)/${$1}/gi;
 print $string\n;
 
 But it doesn't work.  I want it to print some variable.
 
  One way is to change the regex a bit:
 
  #!/usr/bin/perl
  use strict;
  use warnings;
 
  my $var='variable';
  my $other_var='another';
  my ($string1, $string2, $string3)=map 'some $var $other_var', 1..3;
 
  # test1, test2, final version:
  #
  $string1=~s/(\$\w+)/$1/g;
  $string2=~s/(\$\w+)/$1/ge;
  $string3=~s/(\$\w+)/$1/gee;
 
  print join \n, $string1, $string2, $string3;
 
 
  greetings
  joe

 Usually it is considered a bad idea to interpolate external strings. You
 could have your users printing any variable. Consider using sprintf
 instead (see `perldoc -f sprintf`).

 my $format = 'some perl %s';
 my $string = sprintf $format, $variable;
 print $string\n;

As I understood The Ghost, the starting point string already contains a 
literal '$variable': 'some perl $variable'.

I do not know the bigger picture of his problem, but I think an extensible way 
to replace a set of known names within a string by values would be (mirco 
templating system):

===
#!/usr/bin/perl
use strict;
use warnings;

my %lookup=( 
   var='variable',
   other_var='another',
);

sub err {warn q(Unkown name '), shift, qq(' found\n)}

my $string='some $var $other_var $unknown';
$string=~s
   { \$([a-zA-Z_]+) }
   { exists $lookup{$1} ? $lookup{$1} : do {err($1), ''} }gxe;
print $string;
===

# prints: 
Unkown name 'unknown' found
some variable another

No interpolation involved, and the '$' sign preceding the names could be 
another one.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: [Win32] Basic I/O Question

2006-01-16 Thread Hardly Armchair
--- Timothy Johnson [EMAIL PROTECTED] wrote:
 
 The diamond operator works fine without escaping
 your backslashes, but I
 recommend putting quotes around your arguments.  The
 following works
 just fine for me:
 
  c:\ while.pl c:\documents and
 settings\username\desktop\file.txt
 
 
 If you want to use the less-than operator (or a
 pipe), then you have to
 explicitly state that you are using perl.exe. 
 cmd.exe can't derive
 implicitly the program you are invoking via the
 extension.
 
 For example:
 
  c:\ perl.exe while.pl  ..\myfile.txt


Thank you, Timothy.  I tested a simple type program
on a test file located on c: and another drive and it
behaved exactly as you said.  I couldn't find any
documentation mentioning that adding perl.exe before
the program and file name is a necessary condition to
get pipes and command-line files to work with STDIN.
 This seems like it would be an important thing to
know for anyone as green as myself.

Adam



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: perl-module ParseLex-2.15 install fails on make test

2006-01-16 Thread Tom Phoenix
On 1/16/06, Bjørge Solli [EMAIL PROTECTED] wrote:
 install Parse::Lex

 t/test1FAILED test 1
 Failed 1/1 tests, 0.00% okay

I get the same as you do; all tests fail. I recommend contacting the
module's author, or using a different module. Cheers!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Combine multiple lines into one line

2006-01-16 Thread Dr.Ruud
Andrej Kastrin:

 I have the file, which looks like:
 
 *RECORD*
 *ID*
 001
 *TITLE*
 Here is title number one.
 *ABSTRACT*
 First sentence of the abstract. Second sentence of the abstract...
 Second line of the abstract.
 [...]
 
 Is there any simple way to transform this file to look like:
 *RECORD*
 *ID* 001
 *TITLE* Here is title number one.
 *ABSTRACT* First sentence of the abstract. Second sentence of the
 abstract. Second line of the abstract.


$ perl -0pe 's/\n([^*])/ $1/g'  infile

-- 
Grtz, Ruud

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: [Win32] Basic I/O Question

2006-01-16 Thread Timothy Johnson
(response below)

-Original Message-
From: Hardly Armchair [mailto:[EMAIL PROTECTED] 
Sent: Monday, January 16, 2006 3:19 PM
To: Timothy Johnson; beginners@perl.org
Subject: RE: [Win32] Basic I/O Question

snip

 If you want to use the less-than operator (or a
 pipe), then you have to
 explicitly state that you are using perl.exe. 
 cmd.exe can't derive
 implicitly the program you are invoking via the
 extension.
 
 For example:
 
  c:\ perl.exe while.pl  ..\myfile.txt


Thank you, Timothy.  I tested a simple type program
on a test file located on c: and another drive and it
behaved exactly as you said.  I couldn't find any
documentation mentioning that adding perl.exe before
the program and file name is a necessary condition to
get pipes and command-line files to work with STDIN.
 This seems like it would be an important thing to
know for anyone as green as myself.

Adam

I don't think the documentation exists, or at least I haven't been able
to find it.  Windows people don't tend to use pipes and redirection as
much as their xNIX counterparts, in part because virtually all
command-line tools for the Win32 platform that take input from a file
allow you to specify the file as a parameter.

The best documentation I've found is here:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs
/en-us/redirection.mspx  but it doesn't say anything about invoking
applications by association.





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response