Re: array output

2004-05-24 Thread DBSMITH
this will work!  thanks!
one more question...

I am setting an array up to be the output of a system app command like 
this

my @ftapes = system (evmvol -w label_state=3|grep barcode");
print $ftapes[0];

it prints everything...meaning 5 lines. Or when I say print $ftapes[0,1] 
it errors saying 

"my" variable @ftapes masks earlier declaration in same scope at 
foreign_tape_ck.pl
Multidimensional syntax $ftapes[0,1] not supported at foreign_tape_ck.pl 
line 2.
Use of uninitialized value in print at foreign_tape_ck.pl line 26.
foreign_tape_ck.pl syntax OK


 
what I want is for each element to hold 1 line.  Will the diamond operator 
work here? 


derek





"Paul D. Kraus" <[EMAIL PROTECTED]>
05/21/2004 04:39 PM

 
To: [EMAIL PROTECTED]
cc: [EMAIL PROTECTED]
    Subject:Re: array output


Not completely sure what you are trying to do but you can join all the
elements like this 

my $mystring = join " ", @myarray;
this will join each elements into one string seperated by a space. 

Paul
On Fri, May 21, 2004 at 02:07:56PM -0400, [EMAIL PROTECTED] wrote:
> All, 
> 
> I have an array of 40 elements and I want to run a system app command 
> against all the elements simultaneously ...well not line by line as in a 

> for loop.
> I have tried 
> 
> 
> does anyone know how to spit out all 40 elements so the app command does 

> not eject tapes one by one?
> thanks
> 
> Derek B. Smith
> OhioHealth IT
> UNIX / TSM / EDM Teams
> 

-- 
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 Representation

2003-12-23 Thread Tom Kinzer
that says assign elements 12 through 24 of that array these values.

element numbering in perl starts at zero, so 12 is the 13th element in the
array.

-Tom Kinzer

-Original Message-
From: Ursala Luttrell [mailto:[EMAIL PROTECTED]
Sent: Tuesday, December 23, 2003 10:41 AM
To: [EMAIL PROTECTED]
Subject: Array Representation


I need to modify an existing perl script.  Within the code the following
appears:

@unique_array[12..24]=
$first_total,$second_total,$third_total,$fourth_total,undef,$fifth_total,$si
xth_total,undef,1000);

I don't understand what the function of [12..24] is.  Thanks in advance for
an explanation.



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Array Representation

2003-12-23 Thread Rob Dixon
Ursala Luttrell wrote:
>
> I need to modify an existing perl script.  Within the code the following
> appears:
>
> @unique_array[12..24]=
> $first_total,$second_total,$third_total,$fourth_total,undef,$fifth_total,$si
> xth_total,undef,1000);
>
> I don't understand what the function of [12..24] is.  Thanks in advance for
> an explanation.

Hi Ursala.

The '..' operator in Perl returns a list of values starting at the first
operand and ending at the second. 12 .. 24 generates the list

  (12, 13, 14, 15,  ..and so on.. 20, 21, 22, 23, 24)

The expression @unique_array[12..24] is a "slice" consisting of
the 13 elements of @unique_array with indices 12 through 24.

The assignment will copy the list on the right into the array
elements in the slice, but I'm not sure exactly what the code
looks like as it won't even compile without an opening bracket
for the list. If there really are only the nine items in the list
then these will be copied to elements 12 through 20 of @unique_array.
The remaning four elements in the slice will be set to 'undef'.

I hope this helps.

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Array Representation

2003-12-23 Thread John W. Krahn
Ursala Luttrell wrote:
> 
> I need to modify an existing perl script.  Within the code the following
> appears:
> 
> @unique_array[12..24]=
> $first_total,$second_total,$third_total,$fourth_total,undef,$fifth_total,$si
> xth_total,undef,1000);
> 
> I don't understand what the function of [12..24] is.  Thanks in advance for
> an explanation.

@unique_array[12..24] is an array slice.  A slice is a subset of an
array (or list or hash.)  The statement above is equivalent to:

$unique_array[ 12 ] = $first_total;
$unique_array[ 13 ] = $second_total;
$unique_array[ 14 ] = $third_total;
$unique_array[ 15 ] = $fourth_total;
$unique_array[ 16 ] = undef;
$unique_array[ 17 ] = $fifth_total;
$unique_array[ 18 ] = $sixth_total;
$unique_array[ 19 ] = undef;
$unique_array[ 20 ] = 1000;
$unique_array[ 21 ] = undef;
$unique_array[ 22 ] = undef;
$unique_array[ 23 ] = undef;
$unique_array[ 24 ] = undef;


Slices are described in the perldata.pod document in the section
"Slices".

perldoc perldata


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Array Manipulations

2004-01-08 Thread John W. Krahn
[EMAIL PROTECTED] wrote:
> 
> Hello Everyone,

Hello,

> I am a Perl newbie trying to learn as much Perl as I can.  I am trying to
> combine specific array elements into one single array element so that I can
> write
> to an Excel cell, where all the data will fit.
> 
> For instance I have,
> 
> array[0] = "F1: blue";
> array[1] = "F2: green";
> array[2] = "F3: red";
> array[3] = "F1: purple";
> array[4] = "F2: brown";
> array[4] = "F1: red";
> array[5] = "F2: pink";
> array[6] = "F3: blue";
> array[7] = "F4: white";
> 
> With the above information, I want to put all the F's in order before it
> starts over at 1 again.  For instance, I want to store array[0], array[1],
> and array[2] into a specific array element and then store array[3], array[4]
> into another specific array element, then I start over again with array[5].
> Each time I encounter an F1, I will store all the particular elements into
> one single array element.  Is this possible?

Is this what you want?

$ perl -le'
use Data::Dumper;
@array = ( "F1: blue",
   "F2: green",
   "F3: red",
   "F1: purple",
   "F2: brown",
   "F1: red",
   "F2: pink",
   "F3: blue",
   "F4: white" );

my ( $index, @new ) = -1;

push @{ $new[ /^F1:/ ? ++$index : $index ] }, $_ for @array;

print Dumper [EMAIL PROTECTED];
'
$VAR1 = [
  [
'F1: blue',
'F2: green',
'F3: red'
  ],
  [
'F1: purple',
'F2: brown'
  ],
  [
'F1: red',
'F2: pink',
'F3: blue',
'F4: white'
  ]
];



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Array question

2002-02-12 Thread Christopher Solomon


On Tue, 12 Feb 2002, Kevin Butters wrote:

> Beginner question:
>
> ..
>
>
> I have an array that I want to insert elements into. I
> want to insert elements at specific points in  the
> array.
>
> Example:
>
> use strict:
>
> @week = ("Monday", "Wednesday", "Friday");
>
> I want to expand the array to include Tuesday after
> element 0 and Thursday after element 1
>
> I thought that splice was the correct way but
> apparently not.

Well, it might be that a hash is really what you want, even if you don't
know it yet.

But if you insist on arrays:

@week = ("Mon",undef,"Wed",undef,"Fri");

That will create a sort of "placeholder" for values you know you want to
insert later.

But probably, depending on what you want to do, a hash would be a better
solution which begs the question: what do you want to do with this?

Chris



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array question

2002-02-12 Thread Michael Fowler

On Tue, Feb 12, 2002 at 05:07:45PM -0800, Kevin Butters wrote:
[snip]
> @week = ("Monday", "Wednesday", "Friday");
> 
> I want to expand the array to include Tuesday after
> element 0 and Thursday after element 1
> 
> I thought that splice was the correct way but
> apparently not.

It is, what makes you think it isn't?

Consider:

@week = qw(Monday Wednesday Friday);
print "@week\n";

splice(@week, 1, 0, "Tuesday");
print "@week\n";

splice(@week, 3, 0, "Thursday");
print "@week\n";

This prints:

Monday Wednesday Friday
Monday Tuesday Wednesday Friday
Monday Tuesday Wednesday Thursday Friday

See perldoc -f splice.  If you have questions about how and why this works,
feel free to ask.


Michael
--
Administrator  www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array question

2002-02-12 Thread Brett W. McCoy

On Tue, 12 Feb 2002, Kevin Butters wrote:

> I have an array that I want to insert elements into. I
> want to insert elements at specific points in  the
> array.
>
> Example:
>
> use strict:
>
> @week = ("Monday", "Wednesday", "Friday");
>
> I want to expand the array to include Tuesday after
> element 0 and Thursday after element 1

You can still use splice, but recursively:

splice @week, 1, 1, 'Tuesday', splice(@week, 1);
splice @week, 3, 1, 'Thursday', splice(@week, 3);
print join(' ', @week), "\n";

This gives:

Monday Tuesday Wednesday Thursday Friday

-- Brett
  http://www.chapelperilous.net/

Even a hawk is an eagle among crows.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array question

2002-02-12 Thread Brett W. McCoy

On Tue, 12 Feb 2002, Michael Fowler wrote:

> Consider:
>
> @week = qw(Monday Wednesday Friday);
> print "@week\n";
>
> splice(@week, 1, 0, "Tuesday");
> print "@week\n";
>
> splice(@week, 3, 0, "Thursday");
> print "@week\n";

Duh, didn't even consider using a 0 offset in my example.

-- Brett
  http://www.chapelperilous.net/

The two oldest professions in the world have been ruined by amateurs.
-- G.B. Shaw


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: array question

2002-02-13 Thread Pankaj Warade

This is work

my @array = ( 1, 2, 3, 4 );

print $#array; ---> size of array.


p

-Original Message-
From: Chris Zampese [mailto:[EMAIL PROTECTED]]
Sent: Sunday, February 10, 2002 5:19 AM
To: [EMAIL PROTECTED]
Subject: array question


Hi all,
  Just wondering if someone could direct me in the right direction...
I am trying to find the number of elements in an array??  any clues
gratefully accepted. thanks as always,
   Chris.


Sign Up for NetZero Platinum Today
Only $9.95 per month!
http://my.netzero.net/s/signup?r=platinum&refcd=PT97

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: array question

2002-02-13 Thread Jonathan E. Paton

--- Pankaj Warade <[EMAIL PROTECTED]> wrote:
> my @array = ( 1, 2, 3, 4 );
>  
> print $#array; ---> size of array.

No, that is wrong.  $#array gives the index of the last
element, which is one less than the number of elements.

Use this instead:

print scalar @array;

Jonathan Paton

 

__
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: array question

2002-02-13 Thread Briac Pilpré

On Wed, 13 Feb 2002 at 02:29 GMT, Pankaj Warade wrote:
> This is work
> 
> my @array = ( 1, 2, 3, 4 );
> 
> print $#array; ---> size of array.

 Actually, $#array holds the indices of the last element, not the size
 of the array. @array in scalar context returns the number of elements
 in the array

#!/usr/bin/perl -w
use strict;

my @array = qw(1 2 3 4);

print "Number of elements: ", scalar @array, "\n";
print "Last element: ", $#array, "\n";

__END__

-- 
briac
 << dynamic .sig on strike, we apologize for the inconvenience >>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: array question

2002-02-13 Thread Brett W. McCoy

On Tue, 12 Feb 2002, Pankaj Warade wrote:

> This is work
>
> my @array = ( 1, 2, 3, 4 );
>
> print $#array; ---> size of array.

No, $#array is the index of the last element of the array.  To get the
size of an array, just put the array into a scalar context:

my $size = @array;

print scalar(@array);

-- Brett
  http://www.chapelperilous.net/

Technology is dominated by those who manage what they do not understand.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: array references

2002-02-13 Thread Johnathan Kupferer

Jon Serra wrote:

>Greetings,
>
>I have an array, each element will contain a reference to another array. How 
>can I dynamically generate each of those references such that each reference is 
>unique. I am trying to create dynamic 2d arrays.  TIA JON
>
#
# The key is understanding 'my' and how perl's garbage collection works.
#
# Each time we enter a sub, my creates new variables and when we return
# a reference to those variables they are ours to keep (unlike C).
#
# The next time we reach the sub we will get new variables and the old
# will be unaffected.
#
my @array = (['a1','a2','a3'],['b1','b2','b3'],['c1','c2','c3']);
print_array(@array);

my @other_array = make_2d_array;
print_array(@other_array);

sub make_2d_array {
my @array;
# Sorry for using ++ on a string!  I couldn't resist.
# $i = 'a';
# ++$i;
# now $i eq 'b'!!!
push(@array,make_simple_array($i)) for(my $i='a';$i le 'c';++$i);
}

sub make_simple_array {
my $rowlabel = shift;
my @array = ();
push(@array,$rowlabel.$i) for(my $i=1;$i<4;++$i);
return \@array;
}

sub print_array {
for my $arrayref (@_){
for my $element (@$arrayref){
print "$element\t";
}
print "\n";
}   
}




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: array references

2002-02-13 Thread Andrea Holstein

In article <[EMAIL PROTECTED]> wrote "Jon Serra" 
<[EMAIL PROTECTED]>:

> Greetings,
> 
> I have an array, each element will contain a reference to another array. How
> can I dynamically generate each of those references such that each reference is 
>unique. I am
> trying to create dynamic 2d arrays.  TIA JON

Is
my @array = map { [] } (1..$array_size);
or
my @array; push @array, [] for (1..$array_size);
what you want ?

Greetings,
Andrea

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array question

2002-02-13 Thread Kevin Butters

You and Mike showed me my error. I placed a 1 in the
length field. It was inserting the element member I
wanted but also removing one.

Thanks,
K
--- "Brett W. McCoy" <[EMAIL PROTECTED]>
wrote:
> On Tue, 12 Feb 2002, Michael Fowler wrote:
> 
> > Consider:
> >
> > @week = qw(Monday Wednesday Friday);
> > print "@week\n";
> >
> > splice(@week, 1, 0, "Tuesday");
> > print "@week\n";
> >
> > splice(@week, 3, 0, "Thursday");
> > print "@week\n";
> 
> Duh, didn't even consider using a 0 offset in my
> example.
> 
> -- Brett
>  
> http://www.chapelperilous.net/
>

> The two oldest professions in the world have been
> ruined by amateurs.
>   -- G.B. Shaw
> 


__
Do You Yahoo!?
Send FREE Valentine eCards with Yahoo! Greetings!
http://greetings.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Array question...

2002-03-28 Thread John Edwards

try

if (defined @array) {
# do something
} else {
# It's not been created, do something else
}

HTH

John

-Original Message-
From: Michael D. Risser [mailto:[EMAIL PROTECTED]] 
Sent: 28 March 2002 13:55
To: [EMAIL PROTECTED]
Subject: Array question...


OK here's the problem:

I have an array that may or may not have been assigned to, if it has been 
assigned to I need to do one thing, otherwise I need to do something else.

I've tried many variations, but I'm having trouble determining if it has
been 
assigned to.

if(undef @myArray) {
# Do thing1
} elsif(!undef @myArray) {
# Do thing2
}

and

if ($myArray[0] ne "") {
# thing1
} else {
# thing2
}

as well as a few other variations. Printing out the array BEFORE the if
block 
shows nothing in the array, yet it does thing1.

After trying many different methods, I am totally lost, please help me!

TIA

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--Confidentiality--.
This E-mail is confidential.  It should not be read, copied, disclosed or
used by any person other than the intended recipient.  Unauthorised use,
disclosure or copying by whatever medium is strictly prohibited and may be
unlawful.  If you have received this E-mail in error please contact the
sender immediately and delete the E-mail from your system.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array question...

2002-03-28 Thread Michael D. Risser

On Thursday 28 March 2002 08:54 am, you wrote:
> OK here's the problem:
>
> I have an array that may or may not have been assigned to, if it has been
> assigned to I need to do one thing, otherwise I need to do something else.
>
> I've tried many variations, but I'm having trouble determining if it has
> been assigned to.
>
> if(undef @myArray) {
> # Do thing1
> } elsif(!undef @myArray) {
> # Do thing2
> }
>
> and
>
> if ($myArray[0] ne "") {
> # thing1
> } else {
> # thing2
> }
>
> as well as a few other variations. Printing out the array BEFORE the if
> block shows nothing in the array, yet it does thing1.

OOPS I meant it does thing2 :-)

> After trying many different methods, I am totally lost, please help me!
>
> TIA

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array question...

2002-03-28 Thread Chas Owens

On Thu, 2002-03-28 at 08:54, Michael D. Risser wrote:
> OK here's the problem:
> 
> I have an array that may or may not have been assigned to, if it has been 
> assigned to I need to do one thing, otherwise I need to do something else.
> 
> I've tried many variations, but I'm having trouble determining if it has been 
> assigned to.
> 
> if(undef @myArray) {
> # Do thing1
> } elsif(!undef @myArray) {
> # Do thing2
> }
> 
> and
> 
> if ($myArray[0] ne "") {
> # thing1
> } else {
> # thing2
> }
> 
> as well as a few other variations. Printing out the array BEFORE the if block 
> shows nothing in the array, yet it does thing1.
> 
> After trying many different methods, I am totally lost, please help me!
> 
> TIA

Since an array returns the number of elements it contains when 
it is evaluated in scalar context why don't you just say:

#0 is false, non-zero is true so if @myArray has data
#then we do something otherwise we do something else
if (@MyArray) {
local($") = ','; #make the interpolation of @MyArray use ','s
print "my \@MyArray = (@MyArray);\n";
} else {
print "my \@MyArray;\n";
}


-- 
Today is Boomtime the 14th day of Discord in the YOLD 3168
You are what you see.

Missile Address: 33:48:3.521N  84:23:34.786W


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array question

2002-04-01 Thread Glenn Meyer

Allison,  Check out pages 133 - 135 in Learning Perl (3rd Edition) - the 
section "AutoIncrement and Autodecrement" section _ particularly the top of 
page 135 has an example for just this very question!   I just read that last 
week so it popped right up in my mind.

If you don't have the book, holler back to me off-line and I will type out 
the code - it is not too long.

Glenn.

On Monday 01 April 2002 10:08 am, Allison Ogle wrote:
> Hi,
>
> I have a datafile with a list of names like
>
> Ana
> John
> Mike
> Tracy
> John
> Luke
> 
> etc.
>
> I don't know how long the list is and eventually in the list some of the
> names will repeat.  I want to put these names in an array but I don't want
> to repeat any names in the array and I want to keep a count of how many
> times the name appears in the list.  Does anyone have any ideas or
> suggestions?  Thanks,
>
> Allison

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Array question

2002-04-01 Thread Timothy Johnson


You could always put them into a hash and then put them into an array later.

foreach(@names){
   $hash{$_} = 1;
}

my @array = keys %hash;

-Original Message-
From: Allison Ogle [mailto:[EMAIL PROTECTED]]
Sent: Monday, April 01, 2002 8:08 AM
To: a a
Subject: Array question


Hi,

I have a datafile with a list of names like

Ana
John
Mike
Tracy
John
Luke
...
etc.

I don't know how long the list is and eventually in the list some of the
names will repeat.  I want to put these names in an array but I don't want
to repeat any names in the array and I want to keep a count of how many
times the name appears in the list.  Does anyone have any ideas or
suggestions?  Thanks,

Allison


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



This email may contain confidential and privileged 
material for the sole use of the intended recipient. 
If you are not the intended recipient, please contact 
the sender and delete all copies.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array question

2002-04-01 Thread ERIC Lawson - x52010

On Mon, 1 Apr 2002, Allison Ogle wrote:

> I don't know how long the list is and eventually in the list some of
> the names will repeat.  I want to put these names in an array but I
> don't want to repeat any names in the array and I want to keep a count
> of how many times the name appears in the list.  Does anyone have any
> ideas or suggestions?  Thanks,

This is based on one of the FAQ's.  @array1 is your array of names. (See
http://www.perldoc.com/perl5.6.1/pod/perlfaq4.html, Data:Arrays, under
the heading "How do I compute the difference..."):

%count = ();
foreach $element (@array1) { $count{$element}++ }

Your array of unique names is now available from the keys of %count; the
number of occurences of each unique name is available as the value of
%count{$name}.

best,
Eric

-- 

James Eric Lawson
Research Publications Editor
National Simulation Resource

[EMAIL PROTECTED]

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

Whereof one cannot speak, thereof one must be silent. -- Wittgenstein


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array question

2002-04-01 Thread Aman Raheja

Here's my solution.
There will be shorter ways. I am new to perl, so this is how did it.

At the prompt do
$array-prob.pl 

#!/usr/bin/perl

#File name : array-prob.pl

my @arr;
while(<>)
{
chomp($_);
$field{$_}++;
print "$_ $field{$_}\n";
my $set = 0;
my $reset = 1;
foreach $element (@arr) {
if($_ ne $element)  {
$set = 1;
}
else{
$reset = 0;
}
}
if($set == 1 || $arr == 0)  {
if($reset != 0) {
push(@arr,$_);
}
}
}

print "\n array  - is @arr";
print "\n Repeats are : \n";
foreach $element (@arr) {
 print "$element was $field{$element} times\n";
}

c'ya
Aman


>From: "Allison Ogle" <[EMAIL PROTECTED]>
>To: "a a" <[EMAIL PROTECTED]>
>Subject: Array question
>Date: Mon, 1 Apr 2002 11:08:00 -0500
>
>Hi,
>
>I have a datafile with a list of names like
>
>Ana
>John
>Mike
>Tracy
>John
>Luke
>
>etc.
>
>I don't know how long the list is and eventually in the list some of the
>names will repeat.  I want to put these names in an array but I don't want
>to repeat any names in the array and I want to keep a count of how many
>times the name appears in the list.  Does anyone have any ideas or
>suggestions?  Thanks,
>
>Allison
>
>
>--
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]
>

_
Chat with friends online, try MSN Messenger: http://messenger.msn.com


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array question

2002-04-01 Thread ERIC Lawson - x52010

Sorry, there were a couple typos in my earlier response:

> %count = ();
> foreach $element (@array1) { $count{$element}++ }

I should've included the semicolon at the end of the second line.

> Your array of unique names is now available from the keys of %count;
> the number of occurences of each unique name is available as the value
> of %count{$name}.

The "%count{$name}" above should be $count{$name}.

> best,
> Eric

not best,
Eric

-- 

James Eric Lawson
Research Publications Editor
National Simulation Resource

[EMAIL PROTECTED]

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

Whereof one cannot speak, thereof one must be silent. -- Wittgenstein


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array question

2002-04-01 Thread drieux


On Monday, April 1, 2002, at 11:11 , Aman Raheja wrote:

> $array-prob.pl 

p1: I loved the

$field{$_}++;

p2: but since you have stashed it all in a Hash, why not
unpack the hash with

my @arr = keys(%field);

the counter proposal -

http://www.wetware.com/drieux/CS/lang/Perl/Beginners/array-prob.txt

ciao
drieux

---


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Array question

2002-04-01 Thread Allison Ogle

Thanks for your help.  I finally got it to work.
Allison

-Original Message-
From: drieux [mailto:[EMAIL PROTECTED]]
Sent: Monday, April 01, 2002 3:44 PM
To: [EMAIL PROTECTED]
Subject: Re: Array question



On Monday, April 1, 2002, at 11:11 , Aman Raheja wrote:

> $array-prob.pl 

p1: I loved the

$field{$_}++;

p2: but since you have stashed it all in a Hash, why not
unpack the hash with

my @arr = keys(%field);

the counter proposal -

http://www.wetware.com/drieux/CS/lang/Perl/Beginners/array-prob.txt

ciao
drieux

---


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Array problems!

2001-11-13 Thread Bob Showalter

> -Original Message-
> From: Ben Crane [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, November 13, 2001 10:30 AM
> To: [EMAIL PROTECTED]
> Subject: Array problems!
> 
> 
> I create a dummy file with 4 filenames in it...I have
> read these 4 names into @filelist...and when I pass
> the array to &wanted, it only looks for the last
> file..I KNOW this works with arrays, I've done it
> before...why does it keep taking the last value
> only???
> 
> #!usr/bin/perl -w
> use File::Find;
> 
> $match=;
> chomp $match;
> 
> open(MYFILE, $match) || die "Can't open file: $!";
> @filelist=;

You probably need to chomp() these lines:

   chomp(@filelist=);

> 
> %files = map {$_=>1} @filelist;
> find \&wanted, ".";
> 
> sub wanted()
> { 
> print "Match found at : $File::Find::name\n" if
> $files{$_};   
> }
> 
> in addition: how can I put each $b value into an
> array...so far, there should be ove 30 "Open Table"
> lines in my file...I want a array with all 30 so I can
> search for the files. I've tried $filename[$count]
> with an incrementing variable...but no joy...
> 
> while (defined($b=)) 
>   {
>   if ($b =~ /^Open Table/){
>   $c = index($b, "As");
>   $b = substr ($b,12, $c-14); 
>   print "$b\n";   
>   
> }

perldoc -f push


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array problems!

2001-11-13 Thread Greg Meckes

Maybe try:

open(MYFILE, $match) || die "Can't open file: $!";
my @filelist = ;
close(MYFILE);

chomp @filelist;

foreach (@filelist) {
find \&wanted($_), ".";
}

sub wanted {
my $File = shift;
print "Match found at : $File::Find::name\n" if $File;
}

For the $b array just:
 while (defined($b=)) 
{
if ($b =~ /^Open Table/){
$c = index($b, "As");
$b = substr ($b,12, $c-14); 
print "$b\n";
push(@TheNewArray,$b);  

 }



Hope that helps..

Greg
--- Ben Crane <[EMAIL PROTECTED]> wrote:
> I create a dummy file with 4 filenames in it...I have
> read these 4 names into @filelist...and when I pass
> the array to &wanted, it only looks for the last
> file..I KNOW this works with arrays, I've done it
> before...why does it keep taking the last value
> only???
> 
> #!usr/bin/perl -w
> use File::Find;
> 
> $match=;
> chomp $match;
> 
> open(MYFILE, $match) || die "Can't open file: $!";
> @filelist=;
> 
> %files = map {$_=>1} @filelist;
> find \&wanted, ".";
> 
> sub wanted()
> { 
> print "Match found at : $File::Find::name\n" if
> $files{$_};   
> }
> 
> in addition: how can I put each $b value into an
> array...so far, there should be ove 30 "Open Table"
> lines in my file...I want a array with all 30 so I can
> search for the files. I've tried $filename[$count]
> with an incrementing variable...but no joy...
> 
> while (defined($b=)) 
>   {
>   if ($b =~ /^Open Table/){
>   $c = index($b, "As");
>   $b = substr ($b,12, $c-14); 
>   print "$b\n";   
>   
> }
> 
> Does anyone know any good references, beside perl.com
> and cpan.org, etc on the web...when I try to hunt info
> down I just get sites that cover the basics and
> nothing more...if anyone knows of a site that has
> loads of eg's of source code and show
> intermediate-level array functions, please can u let
> me know...
> 
> thanx
> Ben
> 
> __
> Do You Yahoo!?
> Find the one for you at Yahoo! Personals
> http://personals.yahoo.com
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 


__
Do You Yahoo!?
Find the one for you at Yahoo! Personals
http://personals.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array Length.

2001-12-17 Thread Agustin Rivera

print $#array;

Agustin Rivera
Webmaster, Pollstar.com
http://www.pollstar.com



- Original Message - 
From: "Ryan Guy" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, December 17, 2001 12:24 PM
Subject: Array Length.


> I need to know the fastest way to determine a length of an array.  I know
> there is a built in kind of thing somewhere.  I just cant remember it.
> Thanks
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Array Length.

2001-12-17 Thread Stout, Joel R

Assigning the array to a scalar will give you a count:

push ( @initial_array, qw( apple orange bananna ));
$a = @initial_array;
print $a;

prints 3

-Original Message-
From: Ryan Guy [mailto:[EMAIL PROTECTED]]
Sent: Monday, December 17, 2001 12:24 PM
To: '[EMAIL PROTECTED]'
Subject: Array Length.


I need to know the fastest way to determine a length of an array.  I know
there is a built in kind of thing somewhere.  I just cant remember it.
Thanks


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array Length.

2001-12-17 Thread Jenda Krynicky

From:   Ryan Guy <[EMAIL PROTECTED]>
> I need to know the fastest way to determine a length of an array.  I
> know there is a built in kind of thing somewhere.  I just cant
> remember it. Thanks

$length = scalar( @array );

Jenda


=== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ==
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain.
I can't find it.
--- me

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Array Length.

2001-12-17 Thread Chris Spurgeon

A zillion ways.  One is 


$length = scalar(@thearray);



Chris Spurgeon
Senior Design Technologist
[EMAIL PROTECTED]

ELECTRONIC INK
One South Broad Street
19th Floor
Philadelphia, PA 19107
www.electronicink.com

t 215.922.3800 x(233)
f 215.922.3880


-Original Message-
From: Ryan Guy [mailto:[EMAIL PROTECTED]]
Sent: Monday, December 17, 2001 3:24 PM
To: '[EMAIL PROTECTED]'
Subject: Array Length.


I need to know the fastest way to determine a length of an array.  I know
there is a built in kind of thing somewhere.  I just cant remember it.
Thanks


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
This e-mail is intended solely for the above-mentioned recipient and it may
contain confidential or privileged information.  If you have received it in
error, please notify us immediately and delete the e-mail.  You must not
copy, distribute, disclose or take any action in reliance on it.  In
addition, the contents of an attachment to this e-mail may contain software
viruses which could damage your own computer system.  While Electronic Ink,
Inc. has taken every reasonable precaution to minimize this risk, we cannot
accept liability for any damage which you sustain as a result of software
viruses.  You should perform your own virus checks before opening the
attachment.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array Length.

2001-12-17 Thread Etienne Marcotte

just a note to add to this method

$#array gives the highest index, so the number of elements would be
$#array+1

Also, $#array is useful when doing for to go thru to complete array
since the first index is 0 and the last is nb of elements - 1

for(0..$#array) {#code here }
# or 
foreach(@array) {#code here }

Etienne

Agustin Rivera wrote:
> 
> print $#array;
> 
> Agustin Rivera
> Webmaster, Pollstar.com
> http://www.pollstar.com
> 
> - Original Message -
> From: "Ryan Guy" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Monday, December 17, 2001 12:24 PM
> Subject: Array Length.
> 
> > I need to know the fastest way to determine a length of an array.  I know
> > there is a built in kind of thing somewhere.  I just cant remember it.
> > Thanks
> >
> >
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> 
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]

-- 
Etienne Marcotte
Specifications Management - Quality Control
Imperial Tobacco Ltd. - Montreal (Qc) Canada
514.932.6161 x.4001

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array Length.

2001-12-17 Thread Jenda Krynicky

From: "Agustin Rivera" <[EMAIL PROTECTED]>

> print $#array;
> 
> Agustin Rivera

This has been discussed on this list lately I believe.

This prints the max index of that array, not the array size.
Since normaly you index arrays in Perl from 0 the $#array is the 
length-1, but it doesn't have to (if someone was crazy enough to 
fiddle with $[).

Jenda

=== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ==
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain.
I can't find it.
--- me

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array Length.

2001-12-17 Thread Michael R. Wolf

"Agustin Rivera" <[EMAIL PROTECTED]> writes:

> print $#array;

Off by one.  That prints the last index, not the number of
elements.

print scalar @array

If print isn't your use, try:

$length = @array;
$last_index = $#array;


-- 
Michael R. Wolf
All mammals learn by playing!
   [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array Length.

2001-12-17 Thread Michael R. Wolf

Chris Spurgeon <[EMAIL PROTECTED]> writes:

> A zillion ways.  One is 
> 
> $length = scalar(@thearray);

Which reduces to:

$length = @thearray

The assignment operator supplies the scalar context.  The
scalar pseudo-function is therefore not needed.

OTOH

print scalar @thearray;

The scalar *is* necessary since print would otherwise
provide a list context.

-- 
Michael R. Wolf
All mammals learn by playing!
   [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array Length.

2001-12-18 Thread Michael Fowler

On Mon, Dec 17, 2001 at 11:04:10PM -0500, Michael R. Wolf wrote:
> $length = @thearray
> 
> The assignment operator supplies the scalar context.

No, it's not the assignment operator that provides the scalar context, it's
the assignment to a scalar.

Consider:

@anotherarray = @array;

or

($element0) = @array;


Michael
--
Administrator  www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array Problem

2002-01-16 Thread Leon

- Original Message -
From: "maureen" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>

> Currently, the array seems to only be picking up the last name listed in
> the text file.

> @indata = ;
> close(FILE);
> foreach $i (@indata)
> {
> #remove hard return character from each record
> chomp($i);
> #split fields on pipe character
> #assign a variable name to each of the fields
> ($username,$password) = split(/\|/,$i);
> }

> #check for proper password
> if ($username!=~/$in{'username'}/)
> {
> #invalid password--create error message and exit
> print &PrintHeader;

In the foreach loop, after iteration, $username,$password received the last
line of the file. What you really want is to check
$in{'username'} against every line of the file, to do this, you must check
within the foreach loop like this :-

foreach $i (@indata){
 chomp $i;
 ($username,$password) = split(/\|/,$i);
 if ($username !~ /$in{username}/) {
# I would prefer to use ne instead of !~
#invalid password--create error message and exit
print &PrintHeader;
...
 };
};







_
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array Problem

2002-01-21 Thread maureen

Thanks so much for your help on this. I tried this suggestion, but
unfortunately, the array @indata does not seem to contain the usernames
from the file pwdata.txt.

I've been looking at this for hours. I hope someone can help me figure
out what I am missing here. The objectives for this code and the revised
code follow:

Code Objectives:
1)Accept username and password from an HTML page
2)Open a text file and store the username and passwords listed there in
an array
3)Compare the username and password in the array to the username and
password entered on the HTML page.
4)If username and password match, direct the user to another web page.
5) If username and password do not match or fields are left blank on the
HTML form, direct user to an error page.

Revised code:
#!/usr/local/bin/perl  
require "cgi-lib.pl"; 
#process incoming form data  
&ReadParse; 
#open the database in read-only mode  
open(FILE,"pwdata.txt") || die "Can't find database\n"; 
#store database contents in an array and close file  
@indata = ;  
close(FILE); 
foreach $i (@indata)   
{ 
#remove hard return character from each record  
chomp($i); 
#split fields on pipe character   
#assign a variable name to each of the fields  
($username,$password) = split(/\|/,$i);
if ($username ne /$in{username}/) 
{ 
#invalid password--create error message and exit  
print &PrintHeader; 
print <<"PrintTag";  
  
  
Error!  
  
  
Authorization Required  
  
You do not have authorization to enter this website. Please click http://www.worldwidewebstrategies.com";>here
to return to the WWWS web site.
 
  
If you feel you have received this message in error, please return to
the
login screen and try to enter your username and password again.  
   
  
  
PrintTag
exit(0);  
} 
#check for blank form fields  
if ($in{'username'}eq"" || $in{'password'}eq"")  
{ #invalid password--create error message and exit  
print &PrintHeader; 
print <<"PrintTag";  
  
  
Error!  
  
  
Authorization Required  
  
You do not have authorization to enter this website. Please click http://www.worldwidewebstrategies.com";>here
to return to the WWWS web site.
 
  
If you feel you have received this message in error, please return to
the
login screen and try to enter your username and password again.  
   
  
  
PrintTag
exit(0);  
}; 
#check for existence of lock file  
if (-e "lock.fil")   
{ 
#lock file exists! print message & shut down   
print &PrintHeader;
print <<"PrintTag";
  
  
File in use  
  
  
Try again!  
  
The database is in use. Please try again later.  
  
  
  
PrintTag
exit(0);
}
#everything is okay. Create lock file.  
open(LOCK_FILE, ">lock.fil"); 
#open, append record, and close database  
open(FILE,">>data.txt") || die "Can't find database\n";  
print FILE
"$in{'username'}|$in{'password'}\n";
close(FILE); 
#close lock file  
close(LOCK_FILE); 
#delete lock file  
unlink("lock.fil");
#print database contents  
print "Location:http://www.worldwidewebstrategies.com\n\n";;
};

Leon wrote:
> 
> - Original Message -
> From: "maureen" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> 
> > Currently, the array seems to only be picking up the last name listed in
> > the text file.
> 
> > @indata = ;
> > close(FILE);
> > foreach $i (@indata)
> > {
> > #remove hard return character from each record
> > chomp($i);
> > #split fields on pipe character
> > #assign a variable name to each of the fields
> > ($username,$password) = split(/\|/,$i);
> > }
> 
> > #check for proper password
> > if ($username!=~/$in{'username'}/)
> > {
> > #invalid password--create error message and exit
> > print &PrintHeader;
> 
> In the foreach loop, after iteration, $username,$password received the last
> line of the file. What you really want is to check
> $in{'username'} against every line of the file, to do this, you must check
> within the foreach loop like this :-
> 
> foreach $i (@indata){
>  chomp $i;
>  ($username,$password) = split(/\|/,$i);
>  if ($username !~ /$in{username}/) {
> # I would prefer to use ne instead of !~
> #invalid password--create error message and exit
> print &PrintHeader;
> ...
>  };
> };
> 
> _
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com
> 
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]

-- 
Be the change you want to see in the World- Mahatma Ghandi

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array Problem

2002-01-21 Thread Leon

- Original Message -
From: "maureen" <[EMAIL PROTECTED]>
To: "Leon" <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Sent: Monday, January 21, 2002 8:39 AM
Subject: Re: Array Problem

> if ($username ne /$in{username}/)
> {

Anything in between the forward slash are usually used as a regular
expression such as ~s/$/ or m/.../ whereas things like ne, eq, lt are
straight forward matching operators that dont require forward slash.

Try changing the above to the following and see if it works :-
if ($username ne $in{username}) {.};

 end of my msg ###

> Thanks so much for your help on this. I tried this suggestion, but
> unfortunately, the array @indata does not seem to contain the usernames
> from the file pwdata.txt.
>
> I've been looking at this for hours. I hope someone can help me figure
> out what I am missing here. The objectives for this code and the revised
> code follow:
>
> Code Objectives:
> 1)Accept username and password from an HTML page
> 2)Open a text file and store the username and passwords listed there in
> an array
> 3)Compare the username and password in the array to the username and
> password entered on the HTML page.
> 4)If username and password match, direct the user to another web page.
> 5) If username and password do not match or fields are left blank on the
> HTML form, direct user to an error page.
>
> Revised code:
> #!/usr/local/bin/perl
> require "cgi-lib.pl";
> #process incoming form data
> &ReadParse;
> #open the database in read-only mode
> open(FILE,"pwdata.txt") || die "Can't find database\n";
> #store database contents in an array and close file
> @indata = ;
> close(FILE);
> foreach $i (@indata)
> {
> #remove hard return character from each record
> chomp($i);
> #split fields on pipe character
> #assign a variable name to each of the fields
> ($username,$password) = split(/\|/,$i);
> if ($username ne /$in{username}/)
> {
> #invalid password--create error message and exit
> print &PrintHeader;
> print <<"PrintTag";
> 
> 
> Error!
> 
> 
> Authorization Required
> 
> You do not have authorization to enter this website. Please click  href="http://www.worldwidewebstrategies.com";>here
> to return to the WWWS web site.
> 
> 
> If you feel you have received this message in error, please return to
> the
> login screen and try to enter your username and password again.
>  
> 
> 
> PrintTag
> exit(0);
> }
> #check for blank form fields
> if ($in{'username'}eq"" || $in{'password'}eq"")
> { #invalid password--create error message and exit
> print &PrintHeader;
> print <<"PrintTag";
> 
> 
> Error!
> 
> 
> Authorization Required
> 
> You do not have authorization to enter this website. Please click  href="http://www.worldwidewebstrategies.com";>here
> to return to the WWWS web site.
> 
> 
> If you feel you have received this message in error, please return to
> the
> login screen and try to enter your username and password again.
>  
> 
> 
> PrintTag
> exit(0);
> };
> #check for existence of lock file
> if (-e "lock.fil")
> {
> #lock file exists! print message & shut down
> print &PrintHeader;
> print <<"PrintTag";
> 
> 
> File in use
> 
> 
> Try again!
> 
> The database is in use. Please try again later.
> 
> 
> 
> PrintTag
> exit(0);
> }
> #everything is okay. Create lock file.
> open(LOCK_FILE, ">lock.fil");
> #open, append record, and close database
> open(FILE,">>data.txt") || die "Can't find database\n";
> print FILE
> "$in{'username'}|$in{'password'}\n";
> close(FILE);
> #close lock file
> close(LOCK_FILE);
> #delete lock file
> unlink("lock.fil");
> #print database contents
> print "Location:http://www.worldwidewebstrategies.com\n\n";;
> };
>



_
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: array question

2002-02-10 Thread Zhe Hong

The length (number of elements) of an array is returned when an array is
called under a scalar context.

@array = (1,2,3);
$size = @array; # $size gets 3, number of elements in array
print scalar @array; # prints the number 3

Hope this helps!

Zhe

- Original Message -
From: "Chris Zampese" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Sunday, February 10, 2002 6:18 PM
Subject: array question


> Hi all,
>   Just wondering if someone could direct me in the right direction...
> I am trying to find the number of elements in an array??  any clues
gratefully accepted. thanks as always,
>Chris.
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Array problem

2008-06-30 Thread Aruna Goke

Beyza wrote:

Hi,

I would like to know how to insert escape character in front of
special characters in an array.

I have an array which has strings like;

John's House
Bla bla;
etc,

When I use them in an SQL query, perl gives an error. So, I need to
put escape character for every special character. Is there any quick
way to do it?

Thanks from now on,



consider using placeholder in your query.

goksie

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




Re: Array problem

2008-06-30 Thread Rob Dixon
Beyza wrote:
> Hi,
> 
> I would like to know how to insert escape character in front of
> special characters in an array.
> 
> I have an array which has strings like;
> 
> John's House
> Bla bla;
> etc,
> 
> When I use them in an SQL query, perl gives an error. So, I need to
> put escape character for every special character. Is there any quick
> way to do it?

Show us the code you are using and what error you are getting. There are many
possible solutions to this depending on exactly what you are trying to achieve.

Rob

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




Re: Array problem

2008-06-30 Thread Gunnar Hjalmarsson

Beyza wrote:

I have an array which has strings like;

John's House
Bla bla;
etc,

When I use them in an SQL query, perl gives an error. So, I need to
put escape character for every special character. Is there any quick
way to do it?


perldoc -f quotemeta

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

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




Re: Array problem

2008-07-01 Thread Amit Saxena
use

$*dbh*->*quote*($str)

On Tue, Jul 1, 2008 at 4:59 AM, Gunnar Hjalmarsson <[EMAIL PROTECTED]>
wrote:

> Beyza wrote:
>
>> I have an array which has strings like;
>>
>> John's House
>> Bla bla;
>> etc,
>>
>> When I use them in an SQL query, perl gives an error. So, I need to
>> put escape character for every special character. Is there any quick
>> way to do it?
>>
>
>perldoc -f quotemeta
>
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> http://learn.perl.org/
>
>
>


Re: Array problem

2008-07-02 Thread Beyza
Thanks for the answers.

I have tried to use quotemeta but it did not work as expected, DBI's
quote function was exactly what I want.

Thanks again,

On Jul 1, 6:35 pm, [EMAIL PROTECTED] (Amit Saxena) wrote:
> use
>
> $*dbh*->*quote*($str)
>
> On Tue, Jul 1, 2008 at 4:59 AM, Gunnar Hjalmarsson <[EMAIL PROTECTED]>
> wrote:
>
> > Beyza wrote:
>
> >> I have an array which has strings like;
>
> >> John's House
> >> Bla bla;
> >> etc,
>
> >> When I use them in an SQL query, perl gives an error. So, I need to
> >> put escape character for every special character. Is there any quick
> >> way to do it?
>
> >    perldoc -f quotemeta
>
> > --
> > Gunnar Hjalmarsson
> > Email:http://www.gunnar.cc/cgi-bin/contact.pl
>
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >http://learn.perl.org/


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




Re: Array manipulation

2008-11-16 Thread Mr. Shawn H. Corey
On Sun, 2008-11-16 at 08:35 -0800, hotkitty wrote:
> How do I combine the arrays so that the the "newstuff" in array1 gets
> appended only to an item in array2 if the dates match?

Create a hash of lists with the dates as its keys.  Go through Array2
and push each oldstuff on the list stored in the hash.  Then do it for
Array1.  Sort the keys by date, then run through them and create Array3
by joining the stuff from the hash.


-- 
Just my 0.0002 million dollars worth,
  Shawn

The map is not the territory,
the dossier is not the person,
the model is not reality,
and the universe is indifferent to your beliefs.


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




Re: Array manipulation

2008-11-16 Thread John W. Krahn

hotkitty wrote:

Hi,


Hello,


I have two arrays, as follows:

Array1=(
date 11/01/2008 newstuff1,
date 10/27/2008 newstuff2,
date 10/24/2008 newstuff3
)

Array2=(
date 11/01/2008 oldstuff1,
date 10/31/2008 oldstuff2,
date 10/30/2008 oldstuff3,
date 10/29/2008 oldstuff4,
date 10/28/2008 oldstuff5,
date 10/27/2008 oldstuff6,
date 10/26/2008 oldstuff7,
date 10/25/2008 oldstuff8,
date 10/24/2008 oldstuff9,
date 10/23/2008 oldstuff10
)

How do I combine the arrays so that the the "newstuff" in array1 gets
appended only to an item in array2 if the dates match?
In other words:

Array3=(
date 11/01/2008 oldstuff1 and newstuff1,
date 10/31/2008 oldstuff2,
date 10/30/2008 oldstuff3,
date 10/29/2008 oldstuff4,
date 10/28/2008 oldstuff5,
date 10/27/2008 oldstuff6 and newstuff2,
date 10/26/2008 oldstuff7,
date 10/25/2008 oldstuff8,
date 10/24/2008 oldstuff9 and newstuff3,
date 10/23/2008 oldstuff10
)

All I can figure out is how to append array2 onto array1 and vice
versa, but that just doesn't work for my project.


$ perl -le'
my @Array1 = (
"date 11/01/2008 newstuff1",
"date 10/27/2008 newstuff2",
"date 10/24/2008 newstuff3",
);

my @Array2 = (
"date 11/01/2008 oldstuff1",
"date 10/31/2008 oldstuff2",
"date 10/30/2008 oldstuff3",
"date 10/29/2008 oldstuff4",
"date 10/28/2008 oldstuff5",
"date 10/27/2008 oldstuff6",
"date 10/26/2008 oldstuff7",
"date 10/25/2008 oldstuff8",
"date 10/24/2008 oldstuff9",
"date 10/23/2008 oldstuff10",
);

LINE:
for my $line ( @Array2 ) {
for ( @Array1 ) {
if ( substr( $line, 5, 10 ) eq substr( $_, 5, 10 ) ) {
$line .= " and " . substr $_, 16;
next LINE;
}
}
}

print for @Array2;
'
date 11/01/2008 oldstuff1 and newstuff1
date 10/31/2008 oldstuff2
date 10/30/2008 oldstuff3
date 10/29/2008 oldstuff4
date 10/28/2008 oldstuff5
date 10/27/2008 oldstuff6 and newstuff2
date 10/26/2008 oldstuff7
date 10/25/2008 oldstuff8
date 10/24/2008 oldstuff9 and newstuff3
date 10/23/2008 oldstuff10




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

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




Re: Array question

2009-03-30 Thread Rodrick Brown
On Mon, Mar 30, 2009 at 2:20 PM, ANJAN PURKAYASTHA
 wrote:
> Hi,
> Here is my problem;
> I have a series of arrays with 0s and 1s. here is an example: (1, 0, 1, 1).
> I need to parse through this series of arrays and extract the index of the
> 0s in the array.
> Is there any quick way of doing this?

Millions of ways here is one:


#!/usr/bin/perl -w
use strict;

my @arr = (0,1,1,1,0,0);
my $pos = 0;

for my $index (@arr) {
  if ( $index == 0 ) {
 printf ("%d ", $pos );
  }
  $pos++;
}
print "\n";


> TIA,
> Anjan
>
> --
> =
> anjan purkayastha, phd
> bioinformatics analyst
> whitehead institute for biomedical research
> nine cambridge center
> cambridge, ma 02142
>
> purkayas [at] wi [dot] mit [dot] edu
> 703.740.6939
>



-- 
[ Rodrick R. Brown ]
http://www.rodrickbrown.com http://www.linkedin.com/in/rodrickbrown

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array question

2009-03-30 Thread Chas. Owens
On Mon, Mar 30, 2009 at 14:20, ANJAN PURKAYASTHA
 wrote:
> Hi,
> Here is my problem;
> I have a series of arrays with 0s and 1s. here is an example: (1, 0, 1, 1).
> I need to parse through this series of arrays and extract the index of the
> 0s in the array.
> Is there any quick way of doing this?
> TIA,
> Anjan
snip

Hmm, it is an O(n) problem, so there is no quicker way than just going
through the array each element at a time.  That said, there are some
short ways of going through the array.  I would probably use map:
#!/usr/bin/perl

use strict;
use warnings;

#0  1  2  3  4  5  6  7  8  9
my @a = (1, 1, 0, 0, 1, 0, 1, 0, 0, 0);
#so our list should be 2, 3, 5, 7, 8, and 9

my $i = -1;
my @indexes = map { $i++; $_ ? () : $i } @a;

print join(", ", @indexes), "\n";


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array question

2009-03-30 Thread Chas. Owens
On Mon, Mar 30, 2009 at 14:28, Rodrick Brown  wrote:
snip
> Millions of ways here is one:
snip
> my $pos = 0;
>
> for my $index (@arr) {
>  if ( $index == 0 ) {
>     printf ("%d ", $pos );
>  }
>  $pos++;
> }
snip

If you are going to go with a full bore for loop, you might as well
get rid of the index variable in the outer scope:

for my $i (0 .. $#arr) {
push @indexes, $i if $arr[$i] == 0
}
print "@indexes\n";

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array question

2009-03-30 Thread John W. Krahn

ANJAN PURKAYASTHA wrote:

Hi,


Hello,


Here is my problem;
I have a series of arrays with 0s and 1s. here is an example: (1, 0, 1, 1).
I need to parse through this series of arrays and extract the index of the
0s in the array.
Is there any quick way of doing this?


$ perl -le'
my @array = ( 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1 );
print for grep $array[ $_ ] == 0, 0 .. $#array;
'
1
4
8
9


Or instead of using arrays you could store the 1s and 0s in strings:

$ perl -le'
my $string = "10110111001";
print $-[0] while $string =~ /0/g;
'
1
4
8
9


Which would probably be more efficient.



John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array question

2009-03-30 Thread Dave Tang

On Tue, 31 Mar 2009 04:49:17 +1000, John W. Krahn  wrote:


Or instead of using arrays you could store the 1s and 0s in strings:
 $ perl -le'
my $string = "10110111001";
print $-[0] while $string =~ /0/g;
'
1
4
8
9


Hi John,

Could you explain how the above code works please? I looked up perl -l in  
man perl and the argument is for octal. Why is that necessary?


I also looked up $-, which is the variable for the "number of lines left  
on the page". Is this an array, since you use it as $-[0]?


Thank you,

Dave

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array question

2009-03-30 Thread John W. Krahn

Dave Tang wrote:

On Tue, 31 Mar 2009 04:49:17 +1000, John W. Krahn  wrote:


Or instead of using arrays you could store the 1s and 0s in strings:
 $ perl -le'
my $string = "10110111001";
print $-[0] while $string =~ /0/g;
'
1
4
8
9


Could you explain how the above code works please?


while $string =~ /0/g

That matches the contents of $string against the pattern /0/ globally so 
each time through the while loop it will match each different '0' 
character in turn.


print $-[0]

Each time through the loop print the contents of the first element of 
the @- array which contains the starting position of the current match. 
 (The first element of the @+ array contains the ending position of the 
current match.)



I looked up perl -l 
in man perl and the argument is for octal. Why is that necessary?


perldoc perlrun

[ SNIP ]

-l[octnum]
 enables automatic line-ending processing.  It has two separate
 effects.  First, it automatically chomps $/ (the input record
 separator) when used with -n or -p.  Second, it assigns "$\"
 (the output record separator) to have the value of octnum so
 that any print statements will have that separator added back
 on.  If octnum is omitted, sets "$\" to the current value of
  ^^^
 $/.
 ^^^

So instead of writing:

perl -e'print "something\n"'

You can write that as:

perl -le'print "something"'

And it will automatically add the newline for you.


I also looked up $-, which is the variable for the "number of lines left 
on the page". Is this an array, since you use it as $-[0]?


No, the array is @-

perldoc perlvar

[ SNIP ]

@LAST_MATCH_START
@-  $-[0] is the offset of the start of the last successful
match.  "$-["n"]" is the offset of the start of the
substring matched by n-th subpattern, or undef if the
subpattern did not match.


By the way, you could also use index() to find the positions of the 0s 
in the string:


$ perl -le'
my $string = "10110111001";
my $pos= -1;
print $pos while ( $pos = index $string, "0", ++$pos ) >= 0;
'
1
4
8
9



John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array Initialization

2009-05-12 Thread Chas. Owens
On Tue, May 12, 2009 at 14:30, AndrewMcHorney  wrote:
> Hello
>
> I put the strict and warning statements in my perl code. I now need to
> initialize arrays. What is the best way to initialize an array before the
> loop where I will basically recreating the array size in the loop?
>
> my @array = ?
>
> while (more work to do)
> {
> �...@array = split $string;
>
>  # do work on array
> }
snip

The same way you created them before using strict:

my @array = AN_EXPRESSION_THAT_YIELDS_A_LIST;

Also, while loops are a bad way to process arrays, the for loop is
much more natural:

for my $element_of_array (@array) {
}

If this is the same code as before, you really shouldn't be using
arrays like that.  Files should be read using

while (my $line = <$fh>) {
}

not

my @array = <$fh>;
my $i;

while ($i < @array) {
my $line = $array[$i++];
}

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array Initialization

2009-05-12 Thread John W. Krahn

AndrewMcHorney wrote:

Hello


Hello,

I put the strict and warning statements in my perl code. I now need to 
initialize arrays. What is the best way to initialize an array before 
the loop where I will basically recreating the array size in the loop?


my @array = ?

while (more work to do)
{
  @array = split $string;

  # do work on array
}


It is usually best to declare variables in the smallest scope possible so:

while (more work to do)
{
  my @array = split $string;

  # do work on array
}



John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array Initialization

2009-05-12 Thread Bryan Harris


[stuff cut out]


It is usually best to declare variables in the smallest scope possible so:

while (more work to do)
{
  my @array = split $string;

  # do work on array
}



Doesn't that try to re-localize (?) the @array variable every time through 
the loop?  i.e. doesn't it re-run the my() function every time through the 
loop?  For some reason I thought that was a no-no.


- Bryan 




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array Initialization

2009-05-12 Thread John W. Krahn

Bryan Harris wrote:


[stuff cut out]

It is usually best to declare variables in the smallest scope possible 
so:


while (more work to do)
{
  my @array = split $string;

  # do work on array
}



Doesn't that try to re-localize (?) the @array variable every time 
through the loop?  i.e. doesn't it re-run the my() function every time 
through the loop?  For some reason I thought that was a no-no.


my() happens when the code is compiled so it is *not* re-run every time 
through the loop.  The assignment happens when the code is run so it is 
re-run every time.




John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array Initialization

2009-05-12 Thread John W. Krahn

Bryan Harris wrote:



Doesn't that try to re-localize (?) the @array variable every time
through the loop?  i.e. doesn't it re-run the my() function every time
through the loop?  For some reason I thought that was a no-no.


my() happens when the code is compiled so it is *not* re-run every time
through the loop.  The assignment happens when the code is run so it is
re-run every time.


Out of curiosity, how did you know that?  I've mostly been through the 
camel and llama books, but somehow I missed that...


perldoc perlsub

[ snip ]

A "my" has both a compile-time and a run-time effect.  At compile
time, the compiler takes notice of it.  The principal usefulness of
this is to quiet "use strict ’vars’", but it is also essential for
generation of closures as detailed in perlref.  Actual
initialization is delayed until run time, though, so it gets
executed at the appropriate time, such as each time through a loop,
for example.



John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array Initialization

2009-05-12 Thread Gunnar Hjalmarsson

John W. Krahn wrote:
my() happens when the code is compiled so it is *not* re-run every time 
through the loop.  The assignment happens when the code is run so it is 
re-run every time.


$ perl -e '
for (1..5) {
my $count;
$count += 1;
print $count;
last if $count == 3;
}
print "\n";
'
1
$

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

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array Initialization

2009-05-12 Thread Jenda Krynicky
From: "John W. Krahn" 
> Bryan Harris wrote:
> > 
> > [stuff cut out]
> > 
> >> It is usually best to declare variables in the smallest scope possible 
> >> so:
> >>
> >> while (more work to do)
> >> {
> >>   my @array = split $string;
> >>
> >>   # do work on array
> >> }
> >>
> > 
> > Doesn't that try to re-localize (?) the @array variable every time 
> > through the loop?  i.e. doesn't it re-run the my() function every time 
> > through the loop?  For some reason I thought that was a no-no.
> 
> my() happens when the code is compiled so it is *not* re-run every time 
> through the loop.  The assignment happens when the code is run so it is 
> re-run every time.

That's not entirely true. There is compiletime and runtime behaviour 
of my(). It doesn't just silence 'use strict', it also allocates a 
new variable. Each time you enter the loop body. See this:

for (1..10) {
  my $x = 1;
  print "The \$x is at " . \$x . "\n";
}
print "versus\n";
my @arr;
for (1..10) {
  my $x = 1;
  print "The \$x is at " . \$x . "\n";
  push @arr, \$x;
}

As you can see in the first case it allocates the same memory, in the 
second, because I remember the address, it has to allocate a new 
chunk of memory every time.


OTOH, you should not care about this, the allocation is pretty quick, 
especially if it can reallocate the same bit of memory every time.

Jenda
= je...@krynicky.cz === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array Initialization

2009-05-12 Thread John W. Krahn

Gunnar Hjalmarsson wrote:

John W. Krahn wrote:
my() happens when the code is compiled so it is *not* re-run every 
time through the loop.  The assignment happens when the code is run so 
it is re-run every time.


$ perl -e '
for (1..5) {
my $count;
$count += 1;
print $count;
last if $count == 3;
}
print "\n";
'
1


I don't understand what you are trying to demonstrate with that code?


John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array Initialization

2009-05-12 Thread Gunnar Hjalmarsson

John W. Krahn wrote:

Gunnar Hjalmarsson wrote:

John W. Krahn wrote:
my() happens when the code is compiled so it is *not* re-run every 
time through the loop.  The assignment happens when the code is run 
so it is re-run every time.


$ perl -e '
for (1..5) {
my $count;
$count += 1;
print $count;
last if $count == 3;
}
print "\n";
'
1


I don't understand what you are trying to demonstrate with that code?


That my() also happens at run-time, just as the last sentence in the 
docs quote you posted a few minutes ago says. (I hadn't seen that post 
when I posted.)


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

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array Initialization

2009-05-12 Thread John W. Krahn

Gunnar Hjalmarsson wrote:

John W. Krahn wrote:

Gunnar Hjalmarsson wrote:

John W. Krahn wrote:
my() happens when the code is compiled so it is *not* re-run every 
time through the loop.  The assignment happens when the code is run 
so it is re-run every time.


$ perl -e '
for (1..5) {
my $count;
$count += 1;
print $count;
last if $count == 3;
}
print "\n";
'
1


I don't understand what you are trying to demonstrate with that code?


That my() also happens at run-time,


And how does your code demonstrate that?

just as the last sentence in the 
docs quote you posted a few minutes ago says. (I hadn't seen that post 
when I posted.)



John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array Initialization

2009-05-12 Thread Chas. Owens
On Tue, May 12, 2009 at 18:11, John W. Krahn  wrote:
> Bryan Harris wrote:
>>
>> [stuff cut out]
>>
>>> It is usually best to declare variables in the smallest scope possible
>>> so:
>>>
>>> while (more work to do)
>>> {
>>>  my @array = split $string;
>>>
>>>  # do work on array
>>> }
>>>
>>
>> Doesn't that try to re-localize (?) the @array variable every time through
>> the loop?  i.e. doesn't it re-run the my() function every time through the
>> loop?  For some reason I thought that was a no-no.
>
> my() happens when the code is compiled so it is *not* re-run every time
> through the loop.  The assignment happens when the code is run so it is
> re-run every time.
snip

That depends on what you mean by "run".  A new variable is created
each time through the loop and it is assigned a value.  You can prove
this by saying

#!/usr/bin/perl

use strict;
use warnings;

use Scalar::Util qw/refaddr/;

my ($once, @once, @many);
for my $i (1 .. 3) {
my $many = $i;
$once= $i;
push @once, \$once;
push @many, \$many;
print "once ", refaddr \$once, " many ", refaddr \$many, "\n";
}
print "once: ", (map { "$$_ " } @once), " many ", (map {"$$_ "} @many), "\n";

This is not as efficient as declaring it once before the loop and
reusing it, but it is much safer and what you should do unless you can
prove that the inefficiency is costing you real time (premature
optimization is the root of all evil[1]).

once => 55
many => 55

for a loop of 10 items:
 Rate many once
many 293287/s   --  -7%
once 317010/s   8%   --

for a loop of 100 items:
Rate many once
many 38495/s   -- -15%
once 45522/s  18%   --

for a loop of 1000 items:
   Rate many once
many 3980/s   -- -18%
once 4867/s  22%   --

for a loop of 1 items:
  Rate many once
many 399/s   -- -19%
once 492/s  23%   --


#!/usr/bin/perl

use strict;
use warnings;

use Benchmark;

my $n = 10;
my %subs = (
once => sub {
my $once;
my $sum;
for my $i (1 .. $n) {
$once = $i;
$sum += $once;
}
return $sum;
},
many => sub {
my $sum;
for my $i (1 .. $n) {
my $many = $i;
$sum += $many;
}
return $sum;
}
);

for my $sub (keys %subs) {
print "$sub => ", $subs{$sub}(), "\n";
}

for my $i (10, 100, 1_000, 10_000) {
$n = $i;
print "\nfor a loop of $n items:\n";
Benchmark::cmpthese -5, \%subs;
}

1. http://portal.acm.org/citation.cfm?id=356640

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array Initialization

2009-05-12 Thread Gunnar Hjalmarsson

John W. Krahn wrote:

Gunnar Hjalmarsson wrote:

John W. Krahn wrote:

Gunnar Hjalmarsson wrote:

John W. Krahn wrote:
my() happens when the code is compiled so it is *not* re-run every 
time through the loop.  The assignment happens when the code is run 
so it is re-run every time.


$ perl -e '
for (1..5) {
my $count;
$count += 1;
print $count;
last if $count == 3;
}
print "\n";
'
1


I don't understand what you are trying to demonstrate with that code?


That my() also happens at run-time,


And how does your code demonstrate that?


I believe it's self-speaking, provided that you realize what the output 
would have been without the my() statement (or with the my() statement 
placed before the loop). What's your point?


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

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array Initialization

2009-05-12 Thread John W. Krahn

Gunnar Hjalmarsson wrote:

John W. Krahn wrote:

Gunnar Hjalmarsson wrote:

John W. Krahn wrote:

Gunnar Hjalmarsson wrote:

John W. Krahn wrote:
my() happens when the code is compiled so it is *not* re-run every 
time through the loop.  The assignment happens when the code is 
run so it is re-run every time.


$ perl -e '
for (1..5) {
my $count;
$count += 1;
print $count;
last if $count == 3;
}
print "\n";
'
1


I don't understand what you are trying to demonstrate with that code?


That my() also happens at run-time,


And how does your code demonstrate that?


I believe it's self-speaking, provided that you realize what the output 
would have been without the my() statement


Then it wouldn't demonstrate that my() happens at run-time because there 
would be no my() at all.



(or with the my() statement placed before the loop).


Then the variable would be in file scope and it would still be declared 
at compile-time.



What's your point?


I am trying to understand what point you are trying to make.



John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array Initialization

2009-05-12 Thread Chas. Owens
On Wed, May 13, 2009 at 01:59, John W. Krahn  wrote:
snip
>> What's your point?
>
> I am trying to understand what point you are trying to make.
snip

I believe the point is that declaration is only one of the things my
does, so saying that "my() happens when the code is compiled so it is
*not* re-run every time through the loop." is very wrong.  my runs
every time through the loop creating a new variable each time (as
demonstrated by my code and benchmark).

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array Initialization

2009-05-13 Thread Gunnar Hjalmarsson

Chas. Owens wrote:

On Wed, May 13, 2009 at 01:59, John W. Krahn  wrote:
snip

What's your point?

I am trying to understand what point you are trying to make.

snip

I believe the point is that declaration is only one of the things my
does, so saying that "my() happens when the code is compiled so it is
*not* re-run every time through the loop." is very wrong.


Yep, that's it. John, honestly I thought that you had made a 'typo', and 
that my little snippet would be sufficient to call your attention to it.


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

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: array problem

2010-11-15 Thread shawn wilson
too much freaking data. i increased my scroll buffer and found that i do get
data, just not the last 1k lines err

On Mon, Nov 15, 2010 at 12:33 PM, shawn wilson  wrote:

> so, i'm thinking i'm not understanding references here again, but here's
> what i have.
>
> i fill in my array here:
> my $worksheetin = $workbookin->worksheet(0);
>
> my ( $row_min, $row_max ) = $worksheetin->row_range();
> my ( $col_min, $col_max ) = $worksheetin->col_range();
>
> for my $row ( $row_min .. $row_max ) {
>for my $col ( $col_min .. $col_max ) {
>
>   my $cell = $worksheetin->get_cell( $row, $col );
>   next unless $cell;
>
>   $xldata[ $row ][ $col ] = $cell->unformatted() ;
>}
> }
>
> and, i save it here (all works fine at this point):
>
> my $worksheetout = $workbookout->add_worksheet( 'Data' );
> $worksheetout->write_col( 0, 0, \...@xldata );
>
> but, then i go and try to do an query with data in an element of the array,
> and it fails. well, the array appears empty:
>
> while ($year <= $yearnow) {
>my $count = 0;
>my $worksheetout = $workbookout->add_worksheet( '$year' );
>
>for my $row ( 0 .. $#xldata ) {
>   print "@{ $xldata[ $row ] }\n";
>   print "MMSI: $xldata[ $row ][ 13 ]\t YEAR: $year\n";
>   $sth->execute( $xldata[ $row ][ 13 ], $year );
>
>   while (my $sqldata = $sth->fetchrow_arrayref) {
>  $worksheetout->write_row( $count++, 0, \...@{ $sqldata } );
>
>   }
>}
>$year++;
> }
>
>
>


Re: array problem

2010-11-15 Thread Uri Guttman
> "sw" == shawn wilson  writes:

  sw>my $worksheetout = $workbookout->add_worksheet( '$year' );

why are you quoting $year? that doesn't do what you think it does. in
fact it is a bug. you aren't checking if you get results out of that
call which is another problem.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: array problem

2010-11-15 Thread shawn wilson
On Mon, Nov 15, 2010 at 1:54 PM, Uri Guttman  wrote:

> > "sw" == shawn wilson  writes:
>
>  sw>my $worksheetout = $workbookout->add_worksheet( '$year' );
>
> why are you quoting $year? that doesn't do what you think it does. in
> fact it is a bug. you aren't checking if you get results out of that
> call which is another problem.
>

yeah, i noticed that when Spreadsheet::WriteExcel came back and said it
couldn't  recreate the same worksheet :)

though, even posting this is sorta annoying to me since the solution was
right in front of me and had just scrolled off the screen - i didn't even
think to open up the doc to see what it had since i was just seeing errors
:(

it's all working fine now. i'm pretty much just playing around with slices
of arrays now to get only what i want:
while (my $sqldata = $sth->fetchrow_arrayref([ 1 .. -1 ]) ) {
doesn't work. nor does this:
$worksheetout->write_row( $count++, 0, \...@{ $sqldata }[1 .. $#{ $sqldata } ]
);

i'm sure there's a learnable moment in here, but i'm about to just forget
about it, label my first row as 'sql uid' and let them just hide it and call
it a day.


>

uri
>
> --
> Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com--
> -  Perl Code Review , Architecture, Development, Training, Support
> --
> -  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com-
>


Re: Array Question

2010-12-14 Thread Jim Gibson
On 12/14/10 Tue  Dec 14, 2010  9:18 AM, "Matt"  scribbled:

> I am assigning a number of elements to an array like so:
> 
> my @new = `find /home/*/new -cmin 1 -type f`;
> 
> That works fine.  I would also like to append more lines to that array
> from here:
> 
> find /home/*/filed -cmin 1 -type f
> 
> How do I do that without losing whats in the array already?

Use the push function (untested):

  push( @new, `find /home/*/filed -cmin 1 -type f`);

Keep in mind that your array elements will have newlines:

  chomp(@new);

Note also that you can use Perl functions such as the built-in opendir and
readdir functions or the File::Find module to find files without resorting
to the use of separate processes and shell commands.



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array Question

2010-12-14 Thread John W. Krahn

Matt wrote:

I am assigning a number of elements to an array like so:

my @new = `find /home/*/new -cmin 1 -type f`;

That works fine.  I would also like to append more lines to that array
from here:

find /home/*/filed -cmin 1 -type f

How do I do that without losing whats in the array already?


push @new, `find /home/*/filed -cmin 1 -type f`;

Or perhaps:

my @new = `find /home/*/{new,filed} -cmin 1 -type f`;

If your shell supports that syntax.


John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array Question

2010-12-15 Thread Jeff Peng

于 2010-12-15 1:38, Jim Gibson 写道:

or the File::Find module to find files without resorting
to the use of separate processes and shell commands.


Me second.
File::Find is your friend.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Array Question

2010-12-15 Thread shawn wilson
>
>> or the File::Find module to find files without resorting
>> to the use of separate processes and shell commands.
>
>
> Me second.
> File::Find is your friend.
>
Also, since you seem to be familiar with find use find2perl and you barely
have to lift a finger. Its like training wheels for File::Find - actually,
its more akin to a wheel chair. But who am I to say, I use it all the time.


Re: array assignement

2007-07-23 Thread John W. Krahn

jeevs wrote:

Hi forum!


Hello,


I just wanted to know what does the following line do
@{$args{owner}} = qw(hero wierd);


You are assigning a list to the anonymous array in $args{owner}.



lets assume $args{owner} = 'sachin';


'sachin' is a scalar value.



Then it would mean @{sachin} = qw(hero wierd);


No, the scalar value is replaced with an anonymous array.



what would {sachin} stand for does it mean an hash refernce or
something else. I am lost.


'sachin' would not exist after the assignment.



Can someone explain or atleast point me to some documentation, I tried
looking into the perldoc but i am sure i missed something.


perldoc perldsc
perldoc perllol
perldoc perldata
perldoc perlreftut
perldoc perlref



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

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




Re: array assignement

2007-07-23 Thread John W. Krahn

John W. Krahn wrote:

jeevs wrote:


I just wanted to know what does the following line do
@{$args{owner}} = qw(hero wierd);


You are assigning a list to the anonymous array in $args{owner}.



lets assume $args{owner} = 'sachin';


'sachin' is a scalar value.



Then it would mean @{sachin} = qw(hero wierd);


No, the scalar value is replaced with an anonymous array.



what would {sachin} stand for does it mean an hash refernce or
something else. I am lost.


'sachin' would not exist after the assignment.


Correction, the assignment wouldn't happen:

$ perl -le'
use Data::Dumper;
my %args;
@{ $args{ owner } } = qw( hero wierd );
print Dumper \%args;
$args{ owner } = q/sachin/;
print Dumper \%args;
@{ $args{ owner } } = qw( hero wierd );
print Dumper \%args;
'
$VAR1 = {
  'owner' => [
   'hero',
   'wierd'
 ]
};

$VAR1 = {
  'owner' => 'sachin'
};

$VAR1 = {
  'owner' => 'sachin'
};

It would work if you assigned the anonymous array directly:

$ perl -le'
use Data::Dumper;
my %args;
$args{ owner } = [ qw( hero wierd ) ];
print Dumper \%args;
$args{ owner } = q/sachin/;
print Dumper \%args;
$args{ owner } = [ qw( hero wierd ) ];
print Dumper \%args;
'
$VAR1 = {
  'owner' => [
   'hero',
   'wierd'
 ]
};

$VAR1 = {
  'owner' => 'sachin'
};

$VAR1 = {
  'owner' => [
   'hero',
   'wierd'
 ]
};



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

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




Re: array assignement

2007-07-23 Thread Mr. Shawn H. Corey

John W. Krahn wrote:


$ perl -le'
use Data::Dumper;
my %args;
@{ $args{ owner } } = qw( hero wierd );
print Dumper \%args;
$args{ owner } = q/sachin/;
print Dumper \%args;
@{ $args{ owner } } = qw( hero wierd );
print Dumper \%args;


 print Dumper [EMAIL PROTECTED];


Of course, if you `use strict;` this will fail.  The preferred method is to 
place it in a hash:

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my %args = ( owner => 'sachin' );
my %hash = ();
@{ $hash{$args{owner}} } = qw(hero wierd);

print "\%hash: ", Dumper \%hash;

__END__


--
Just my 0.0002 million dollars worth,
  Shawn

"For the things we have to learn before we can do them, we learn by doing them."
 Aristotle

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




Re: array assignement

2007-07-24 Thread Paul Lalli
On Jul 23, 4:35 am, [EMAIL PROTECTED] (Jeevs) wrote:

> I just wanted to know what does the following line do
> @{$args{owner}} = qw(hero wierd);
>
> lets assume $args{owner} = 'sachin';
> Then it would mean @{sachin} = qw(hero wierd);
> what would {sachin} stand for does it mean an hash refernce or
> something else. I am lost.

This is known as a symbolic reference, and is a very very bad idea,
for exactly these reasons.  `use strict;` prevents you from doing
messy stuff like this.  You should always use strict.  If you do not:

perl -MData::Dumper -le'
$args{owner} = "sachin";
@{$args{owner}} = qw(hero wierd);
print Dumper(\%args);
print Dumper([EMAIL PROTECTED]);
'

$VAR1 = {
  'owner' => 'sachin'
};

$VAR1 = [
  'hero',
  'wierd'
];

> Can someone explain or atleast point me to some documentation, I tried
> looking into the perldoc but i am sure i missed something.

perldoc perlref
perldoc perlreftut

Basically, when you use a string as though it was a reference, as you
did above, you create a "symbolic reference".  You are modifying the
variable named by that string.  So if $args{owner} contains the string
'sachin', then @{$args{$owner}} is the same thing as the array
variable @sachin.

Some people try to take advantage of this apparent feature by using it
to dynamically choose which variables they want to access.  To learn
the correct way of going about that, please read:
perldoc -q "variable name"

Paul Lalli



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




Re: array assignement

2007-07-24 Thread Paul Lalli
On Jul 23, 1:51 pm, [EMAIL PROTECTED] (John W. Krahn) wrote:
> John W. Krahn wrote:
> > jeevs wrote:
>
> >> I just wanted to know what does the following line do
> >> @{$args{owner}} = qw(hero wierd);
>
> > You are assigning a list to the anonymous array in $args{owner}.
>
> >> lets assume $args{owner} = 'sachin';
>
> > 'sachin' is a scalar value.
>
> >> Then it would mean @{sachin} = qw(hero wierd);
>
> > No, the scalar value is replaced with an anonymous array.
>
> >> what would {sachin} stand for does it mean an hash refernce or
> >> something else. I am lost.
>
> > 'sachin' would not exist after the assignment.
>
> Correction, the assignment wouldn't happen:

No, the assignment happens just fine.  It's just not assigning to what
you think it's assigning to.

The OP was using symrefs.  By referring to a string as though it was a
reference, he modified an unrelated variable.

>
> $ perl -le'
> use Data::Dumper;
> my %args;
> @{ $args{ owner } } = qw( hero wierd );

This sets $args{owner} to be a reference to an anonymous array
containing('hero', 'wierd');

> print Dumper \%args;
> $args{ owner } = q/sachin/;

This sets $args{owner} to be the string 'sachin';

> print Dumper \%args;
> @{ $args{ owner } } = qw( hero wierd );

This sets the array @sachin to contain ('hero', 'wierd');

It has nothing whatsoever to do with %args.


Paul Lalli


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




Re: Array modification

2007-08-16 Thread Xavier Noria

On Aug 16, 2007, at 11:47 AM, Sayed, Irfan (Irfan) wrote:


I have one array which stores some data after executing specific
command. Depends on situation , command has different output at
different time. sometime array may store 4 values or it may store 5
values.

Now my req. is that I need to assign no. to those values.

for example:

if array is @array1=(data1,data2,data3,data4);

now i need to assign 4 no. as there are four elements are present in
this array. So my array should look like this.

@array1=(1 data1 2 data2 3 data3 4 data4);

basically, i wanted to do indexing or to create hash.


Since a hash is an option, to understand your question better I would  
like to ask whether the already available builtin array indexing


  $array[3]

is enough. If not, why?

-- fxn


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




Re: Array modification

2007-08-16 Thread Chas Owens
On 8/16/07, Sayed, Irfan (Irfan) <[EMAIL PROTECTED]> wrote:
> Hi All,
>
> I have one array which stores some data after executing specific
> command. Depends on situation , command has different output at
> different time. sometime array may store 4 values or it may store 5
> values.
>
> Now my req. is that I need to assign no. to those values.
>
> for example:
>
> if array is @array1=(data1,data2,data3,data4);
>
> now i need to assign 4 no. as there are four elements are present in
> this array. So my array should look like this.
>
> @array1=(1 data1 2 data2 3 data3 4 data4);
>
> basically, i wanted to do indexing or to create hash. so that no. 1 will
> get assigned to first element and 2 will get assigned to sec. element
> and so on..
>
> Can somebody please help.
>
> Regards
> Irfan.

Well, you could always say

my @a = qw;
my $key = 1;
my %h = map { $key++ => $_ } @a;

But why would you want to?  data1 is index 0, data2 is index 1, etc.
Just adjust your numbers by one.  For instance you could print out the
contents of @a above prepended with 1 .. 5 like this

my $i = 1;
for my $item (@a) {
print "$i $tem";
$i++;
}

You could then read a number from the user

my $input = <>;

and then print out the corresponding item

print $a[$input - 1];

There is very little gained by using a hash in this situation.

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




RE: Array modification

2007-08-16 Thread Sayed, Irfan (Irfan)
Thanks Chas but my req. is little bit different.

As I said the data in the array will not be fixed so I don't know how
many elements are present in the array. I don't want to just print the
contents of the array but to use the contents of the array.

For example if user select 3 option then my programme should pick up the
third element of the array 

Please help

Regards
Irfan.


-Original Message-
From: Chas Owens [mailto:[EMAIL PROTECTED] 
Sent: Thursday, August 16, 2007 4:16 PM
To: Sayed, Irfan (Irfan)
Cc: beginners@perl.org
Subject: Re: Array modification

On 8/16/07, Sayed, Irfan (Irfan) <[EMAIL PROTECTED]> wrote:
> Hi All,
>
> I have one array which stores some data after executing specific 
> command. Depends on situation , command has different output at 
> different time. sometime array may store 4 values or it may store 5 
> values.
>
> Now my req. is that I need to assign no. to those values.
>
> for example:
>
> if array is @array1=(data1,data2,data3,data4);
>
> now i need to assign 4 no. as there are four elements are present in 
> this array. So my array should look like this.
>
> @array1=(1 data1 2 data2 3 data3 4 data4);
>
> basically, i wanted to do indexing or to create hash. so that no. 1 
> will get assigned to first element and 2 will get assigned to sec. 
> element and so on..
>
> Can somebody please help.
>
> Regards
> Irfan.

Well, you could always say

my @a = qw; my $key = 1; my %h = map {
$key++ => $_ } @a;

But why would you want to?  data1 is index 0, data2 is index 1, etc.
Just adjust your numbers by one.  For instance you could print out the
contents of @a above prepended with 1 .. 5 like this

my $i = 1;
for my $item (@a) {
print "$i $tem";
$i++;
}

You could then read a number from the user

my $input = <>;

and then print out the corresponding item

print $a[$input - 1];

There is very little gained by using a hash in this situation.

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




Re: Array modification

2007-08-16 Thread Chas Owens
On 8/16/07, Sayed, Irfan (Irfan) <[EMAIL PROTECTED]> wrote:
> Thanks Chas but my req. is little bit different.
>
> As I said the data in the array will not be fixed so I don't know how
> many elements are present in the array. I don't want to just print the
> contents of the array but to use the contents of the array.
>
> For example if user select 3 option then my programme should pick up the
> third element of the array
snip

If the user selects 3 then subtract one and you get 2 the index of the
third element in the array.  My example code was not limited to 5
values, it was dynamic, but here it is again in whole script form:

#!/usr/bin/perl

use strict;
use warnings;

my @items;
#get a list of 4 to 8 items
push @items, "item $_" for 1 .. 4 + int rand 5;

print "select item:\n";
my $i = 1;
for my $item (@items) {
print "\t" . $i++ . ". $item\n";
}
chomp(my $choice = <>);
die "bad choice" if $choice =~ /\D/ or $choice < 1 or $choice > @items;

print "you chose $items[$choice - 1]\n";

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




RE: array index

2007-09-11 Thread Thomas Bätzler
Pavanvithal Torvi <[EMAIL PROTECTED]> asked:
> While debugging a script I came across a scenario where array 
> access was happening with a negative index.
> (because of a corner case that was not properly handled).
> This resulted in accessing the array in a reverse order. 
[...]
> I wanted to ask others if this is expected behaviour.

This is a Perl feature. See the perldata manpage (probably 
"perldoc perldata" on your system), section "Subscripts":

  "The array indices start with 0. A negative subscript
retrieves its value from the end."

HTH,
Thomas

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




Re: array index

2007-09-11 Thread Jeff Pang
2007/9/11, Pavanvithal Torvi <[EMAIL PROTECTED]>:
>
>
> I wanted to ask others if this is expected behaviour.
Yes.

> If I make use of this feature will it cause compatibility issues with the
> later versions of perl.
>

Please don't use $a and $b as variable names,they are built-in
variables used by `sort` function.see `perldoc -f sort`.

Also your codes could be re-written simply,

my $x = "one/two/three/four/five";
my ($c,$d,$e,$f,$g) = split/\//,$x;

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




Re: array index

2007-09-11 Thread Chas Owens
On 9/10/07, Pavanvithal Torvi <[EMAIL PROTECTED]> wrote:
snip
> If I make use of this feature will it cause compatibility issues with the
> later versions of perl.
snip

Negative indexing has been around at least since Perl 5 (and I think
it goes back much farther than that).  As for compatibility with
future versions of Perl, you should have no problem with the Perl 5
line (e.g. 5.10, the next and possibly last Perl 5 release, although I
believe they are planning Perl 5.12 now).  That specific feature is
changing in Perl 6 (which will be released eventually).  In Perl 6 a
negative index refers to elements before element 0*.  That is this

my @a = 1,2,3,4,5
@a[-1] = 6;

will create a new element before the one holding 1 and put 6 there; so
@a will now hold (6,1,2,3,4,5).  If you wish to set the last element
of a Perl 6 array you must use the whatever** symbol minus the number
of elements you want to go back.  So the Perl 5 code

my @a = 1,2,3,4,5;
$a[-1] = 6;

would be

my @a = 1,2,3,4,5;
@a[*-1] = 6;

The relevant Synopsis is S09: http://dev.perl.org/perl6/doc/design/syn/S09.html

* Pugs appears to be broken in this regard, it still treats indexes
like Perl 5 does and the whatever symbol maps to 0.

** from S02
«Ordinarily a term beginning with * indicates a global function or
type name, but by itself, the * term captures the notion of
"Whatever", which is applied lazily by whatever operator it is an
argument to. Generally it can just be thought of as a "glob" that
gives you everything it can in that argument position.»

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




Re: array index

2007-09-11 Thread Jenda Krynicky
On 11 Sep 2007 at 13:04, Chas Owens wrote:
> Negative indexing has been around at least since Perl 5 (and I think
> it goes back much farther than that).  As for compatibility with
> future versions of Perl, you should have no problem with the Perl 5
> line (e.g. 5.10, the next and possibly last Perl 5 release, although I
> believe they are planning Perl 5.12 now).  That specific feature is
> changing in Perl 6 (which will be released eventually).  In Perl 6 a
> negative index refers to elements before element 0*.  That is this
>
> my @a = 1,2,3,4,5
> @a[-1] = 6;
>
> will create a new element before the one holding 1 and put 6 there; so
> @a will now hold (6,1,2,3,4,5).  If you wish to set the last element
> of a Perl 6 array you must use the whatever** symbol minus the number
> of elements you want to go back.  So the Perl 5 code
>
> my @a = 1,2,3,4,5;
> $a[-1] = 6;
>
> would be
>
> my @a = 1,2,3,4,5;
> @a[*-1] = 6;
>
> The relevant Synopsis is S09:
> http://dev.perl.org/perl6/doc/design/syn/S09.html
>
> * Pugs appears to be broken in this regard, it still treats indexes
> like Perl 5 does and the whatever symbol maps to 0.
>
> ** from S02
> < type name, but by itself, the * term captures the notion of
> "Whatever", which is applied lazily by whatever operator it is an
> argument to. Generally it can just be thought of as a "glob" that
> gives you everything it can in that argument position.>>

OH MY !

Yet another reason to stay away from Perl6.

After reading that part of S09 I can't keep from thinking that Perl6
was designed specifically for golf and obfu. Well *.

Jenda
= [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


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




Re: array index

2007-09-11 Thread Chas Owens
On 9/11/07, Jenda Krynicky <[EMAIL PROTECTED]> wrote:
snip
> OH MY !
>
> Yet another reason to stay away from Perl6.
>
> After reading that part of S09 I can't keep from thinking that Perl6
> was designed specifically for golf and obfu. Well *.
snip

Heh.  Perl 6 is going to meet a lot of resistance, but I think in the
end the advantages will outweigh the (re)learning curve.   For
instance

my @array =1,2,3,4,5;
say @array[3 .. *]; #prints "45\n"

instead of

my @array =1,2,3,4,5;
print @array[3 .. $#array], "\n"; #prints "45\n"

Can you really say that the Perl 5 version is better or more Perlish?
Is the concept of "whatever" any more confusing that a shadow
variable?  Or is it just that you are used to how Perl 5 said things?

As for being designed for golf, I think there may some merit to that
argument, but not in the way you are thinking.  The goal is to remove
redundant and boiler plate code.  This has been a Perl value since the
beginning (which is why it is so easy to golf in Perl 5) so we can
hardly be surprised that Larry and the others are striving to add even
more DWIMery to the language.  If we wanted highly verbose languages
we could stick with C, C++, Java, etc.

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




Re: array index

2007-09-12 Thread Pavanvithal Torvi
Hi All,

Thanks a lot for the advice/help. I think it would be better to avoid using
this feature :)

Regards,
Pavan

On 9/11/07, Jeff Pang <[EMAIL PROTECTED]> wrote:
>
> 2007/9/11, Pavanvithal Torvi <[EMAIL PROTECTED]>:
> >
> >
> > I wanted to ask others if this is expected behaviour.
> Yes.
>
> > If I make use of this feature will it cause compatibility issues with
> the
> > later versions of perl.
> >
>
> Please don't use $a and $b as variable names,they are built-in
> variables used by `sort` function.see `perldoc -f sort`.
>
> Also your codes could be re-written simply,
>
> my $x = "one/two/three/four/five";
> my ($c,$d,$e,$f,$g) = split/\//,$x;
>


Re: array index

2007-09-12 Thread Chas Owens
On 9/11/07, Pavanvithal Torvi <[EMAIL PROTECTED]> wrote:
> Hi All,
>
> Thanks a lot for the advice/help. I think it would be better to avoid using
> this feature :)
snip

It is perfectly safe to use negative indexes.  Perl 6 won't be out for
a while and this feature change is one of the smaller
incompatibilities between Perl 5 and Perl 6 (for instance the
concatenation operator "." is becoming "~", for loops are structured
differently, the member of operator "->" is becoming ".", etc.).

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




Re: Array Manipulation

2007-10-25 Thread Dyana Wu


On 25 Oct 2007, at 4:59 PM, Sayed, Irfan (Irfan) wrote:


Hi All,

I have one array say my @test=(1,2,3,4,5);
if I print this array it will print like this
print "@test\n";
and the output is
1 2 3 4 5

so I mean to say that if I type print "@test1\n";
then output should come as
1
2
3
4
5


Try map:

my @test1 = map($_."\n", @test);
print @test1, "\n";

Note that if you use print "@test1\n", Perl will insert a space after  
each element, this means that you will actually get:


1
 2
 3
 4
 5

Alternatively, if you simply need to print each element of @test with  
a newline after it:


print $_, "\n" foreach @test;
print "\n";

HTH.

--
dwu

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




Re: Array Manipulation

2007-10-25 Thread Gunnar Hjalmarsson

Sayed, Irfan (Irfan) wrote:

I have one array say my @test=(1,2,3,4,5);
if I print this array it will print like this
print "@test\n";
and the output is
1 2 3 4 5
 
now my req. is that I want to store these array values in another array

in such a fashion where I can print like
1
2
3
4
5
 
so I mean to say that if I type print "@test1\n";
then output should come as 
1

2
3
4
5


{
local $" = "\n";
print "@test\n";
}

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

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




RE: Array Manipulation

2007-10-25 Thread Sayed, Irfan (Irfan)
 
Hi,

This is the code I am using which I got from Dyana.

my @test1 = map($_."\n", @test);
print @test1, "\n";

but the issue is that in my array , the values are something like this.

M:\isayed_aic_8.0_int\vob1\34er@@\main\IC_8.0_Integration\7
M:\isayed_aic_8.0_int\vob1\test\New Text
Document(2).txt@@\main\IC_8.0_Integration\4

I want to make these values as follows and store in a separate array

M:\isayed_aic_8.0_int\vob1\34er@@\main\IC_8.0_Integration\7
M:\isayed_aic_8.0_int\vob1\test\New Text
Document(2).txt@@\main\IC_8.0_Integration\4

Actual script is attached for reference.

Please guide.

Thanks in Advance.

Regards
Irfan.


-Original Message-
From: Gunnar Hjalmarsson [mailto:[EMAIL PROTECTED] 
Sent: Thursday, October 25, 2007 5:06 PM
To: beginners@perl.org
Subject: Re: Array Manipulation

Sayed, Irfan (Irfan) wrote:
> I have one array say my @test=(1,2,3,4,5); if I print this array it 
> will print like this print "@test\n"; and the output is
> 1 2 3 4 5
>  
> now my req. is that I want to store these array values in another 
> array in such a fashion where I can print like
> 1
> 2
> 3
> 4
> 5
>  
> so I mean to say that if I type print "@test1\n"; then output should 
> come as
> 1
> 2
> 3
> 4
> 5

{
 local $" = "\n";
 print "@test\n";
}

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

--
To unsubscribe, e-mail: [EMAIL PROTECTED] For additional
commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
{\*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\f0\fs20 # perl script 
to send am e-mail when delivery complets\par
use strict;\par
use warnings;\par
use lib qw(/home/ccvob01/irf/lib);\par
use SendMail 2.09;\par
#my $os=$^O;\par
my @files;\par
print "$ENV\{CLEARCASE_ACTIVITY\}\\n";\par
chomp($ENV\{CLEARCASE_ACTIVITY\});\par
chomp(@files = qx("cleartool desc -fmt %[versions]p 
activity:$ENV\{"CLEARCASE_ACTIVITY"\}"));\par
chomp(@files);\par
#my $temp = 'cleartool desc -fmt "%[versions]p' . "\\n\\"" . 'activity:' . 
$ENV\{"CLEARCASE_ACTIVITY"\};\par
#chomp(@files = qx($temp));\par
my @files1=map($_ . "\\n", @files);\par
print "$files1[0]\\n";\par
sendmail();\par
\par
sub sendmail\par
\{\par
my $sm = new SendMail("301081ANEX1.global.avaya.com");\par
# We set the debug mode "ON".\par
#\par
 $sm->setDebug($sm->ON);\par
 my $CT = "/usr/atria/bin/cleartool";\par
\par
$sm->From("CC VOB Admin -Pune <[EMAIL PROTECTED]>");\par
\par
# We set the subject.\par
#\par
$sm->Subject("Delivery has been completed successfully");\par
\par
\par
# We set the recipient.\par
\par
#$sm->To("$usr");\par
$sm->To("[EMAIL PROTECTED]");\par
#$sm->To("Smita <[EMAIL PROTECTED]>");\par
\par
\par
# We set the content of the mail.\par
#\par
$sm->setMailBody ("This is an auto generated mail.PLease reply to [EMAIL 
PROTECTED],\\n\\n Following is the entire description for the delivery.\\n\\n 
UCM Project: $ENV\{CLEARCASE_PROJECT\}\\n UCM source stream: 
$ENV\{CLEARCASE_SRC_STREAM\}\\nUCM destination 
stream:$ENV\{CLEARCASE_STREAM\}\\nUCM integration activity: 
$ENV\{CLEARCASE_ACTIVITY\}\\nUCM activities delivered: 
$ENV\{CLEARCASE_DLVR_ACTS\} \\n\\n UCM view: $ENV\{CLEARCASE_VIEW_TAG\}\\n\\n 
Following files got delivered in this operation \\n\\n @files1 \\n\\n Thank You 
\\n Pune RDPS SCM Team");\par
#\par
# Attach a testing image.\par
#\par
#$sm->Attach("/tmp/lsco_log");\par
#$sm->Attach("/tmp/lsco_log1");\par
#\par
# Check if the mail sent successfully or not.\par
#\par
if ($sm->sendMail() != 0) \{\par
  print $sm->\{'error'\}."\\n";\par
  exit -1;\par
\}\par
\par
#\par
# Mail sent successfully.\par
#\par
print "Done\\n\\n";\par
#exit 0;\par
\}\par
}
-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Re: Array Manipulation

2007-10-25 Thread Ron Bergin
On Oct 25, 1:59 am, [EMAIL PROTECTED] (Irfan Sayed) wrote:
> Hi All,
>
> I have one array say my @test=(1,2,3,4,5);
> if I print this array it will print like this
> print "@test\n";
> and the output is
> 1 2 3 4 5
>
> now my req. is that I want to store these array values in another array
> in such a fashion where I can print like
> 1
> 2
> 3
> 4
> 5
>
> so I mean to say that if I type print "@test1\n";
> then output should come as
> 1
> 2
> 3
> 4
> 5
>
> I have used push function also but it is not giving expected result.
>
> Please guide.
>
> Regards
> Irfan.

print $_,$/ for @test;


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




Re: Array Manipulation

2007-10-25 Thread [EMAIL PROTECTED]
On Oct 25, 4:21 pm, [EMAIL PROTECTED] (Ron Bergin) wrote:
>
> print $_,$/ for @test;

Nothing wrong with that, but I usually write:

print "$_\n" for @test;

TMTOWTDI!


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




Re: Array Manipulation

2007-10-25 Thread asmith9983

Hi
This will do what you want:-

perl -le '@test=(1,2,3,4,5);print join "\n",@test;'

The -l option ensures a final newline after the last element of the array is 
printed.  The order of the options is important as changing it to "el" 
wouldn't 
work.


--
Andrew
Edinburgh,Scotland

On Thu, 25 Oct 2007, Ron Bergin wrote:


On Oct 25, 1:59 am, [EMAIL PROTECTED] (Irfan Sayed) wrote:

Hi All,

I have one array say my @test=(1,2,3,4,5);
if I print this array it will print like this
print "@test\n";
and the output is
1 2 3 4 5

now my req. is that I want to store these array values in another array
in such a fashion where I can print like
1
2
3
4
5

so I mean to say that if I type print "@test1\n";
then output should come as
1
2
3
4
5

I have used push function also but it is not giving expected result.

Please guide.

Regards
Irfan.


print $_,$/ for @test;





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




Re: Array Manipulation

2007-10-25 Thread Greg
Similar issue here, but with a twist.
I have an input file that I'm reading in that is pipe delimited.  (HL7
actually)
So far I have
my @record = split (/\|/,$_);
I want to take $record[16] and replace it with $record[16] /
$record[7] ONLY if $record[7] is not empty.
I have this accomplished by
$converted = $record[16];
$converted = ($record[16] / $record[7]) unless ($record[7] eq "");
$record[16]="$converted";

At this point I print out @record and it gives correct values..
without the |'s.
To get it back into the line of the input file in memory can I just do
a
print $_, "|" foreach @record;  ?
or how would I do that exactly?


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




Re: Array Manipulation

2007-10-27 Thread John W . Krahn
On Thursday 25 October 2007 11:03, [EMAIL PROTECTED] wrote:
> Hi

Hello,  Please do not top-post,  TIA.

> This will do what you want:-
>
> perl -le '@test=(1,2,3,4,5);print join "\n",@test;'
>
> The -l option ensures a final newline after the last element of the
> array is printed.  The order of the options is important as changing
> it to "el" wouldn't work.

If you are going to use the -l switch then just do:

print for @test


:-)

John
-- 
use Perl;
program
fulfillment


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




<    1   2   3   4   5   6   7   8   >