Perl and string array problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl and string array problem
# 1  
Old 07-01-2010
Perl and string array problem

Code:
#!/usr/bin/perl
my @arr=("hello", "how", "are", "you");
$l=length(@arr);
print $l;

This print 1.Why?
How can i print the array size = 4?
I want to store these in an array.
Code:
hello
how 
are
you

And then i want to access these element through indexing.
How can i do this?
# 2  
Old 07-01-2010
Code:
print $#arr+1;

$#arr contains index of array's last element.
This User Gave Thanks to bartus11 For This Post:
# 3  
Old 07-01-2010
Quote:
% perldoc -f length
length EXPR
length Returns the length in characters of the value of EXPR. If EXPR
is omitted, returns length of $_. Note that this cannot be
used on an entire array or hash to find out how many elements
these have. For that, use "scalar @array" and "scalar keys
%hash" respectively.
Code:
% perl -le'
  my @arr = qw(hello how are you);
  $l = @arr;
  print $l;
  '
4

This User Gave Thanks to radoulov For This Post:
# 4  
Old 07-01-2010
Quote:
Originally Posted by cola
And then i want to access these element through indexing.
How can i do this?
Code:
print $arr[1]; #prints how
print $arr[3]; #prints you

# 5  
Old 07-01-2010
Quote:
Originally Posted by radoulov
Code:
% perl -le'
  my @arr = qw(hello how are you);
  $l = @arr;
  print $l;
  '
4

I just knew about
Code:
$k=$#(@arr);

But $l=@arr even looks easier.

Done.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl string formation from array

HI I ma using perl programming my perl is like this $InputFile = $ENV{UDE_TMP} . "/" ."cre_fmr_gen.temp_data_file_gen.dat"; @duplicates = `cat $InputFile | cut -d "|" -f 1,1 | sort | uniq -c | awk '{ if(\$1>1) {print \$2;}}'`; my $cusiplist ; foreach $cusip (@duplicates) {... (1 Reply)
Discussion started by: ptappeta
1 Replies

2. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

3. Shell Programming and Scripting

[Perl] Sorting an String-Array

Hi, i have a txtfile with the format <Nr>tab<word>tab<other stuff>new line and i want to sort the <word>-colum with a perl script. My textfile: <Nr>tab<word>tab<other stuff>new line 6807 die ART.Acc.Sg.Fem 6426 der ART.Gen.Sg.Fem 2 die ART.Nom.Sg.Fem 87 auf APPR.-- 486 nicht PTKNEG.--... (1 Reply)
Discussion started by: buckelede
1 Replies

4. Shell Programming and Scripting

[Perl] Strange problem with array

Hi, I have a strange problem with arrays in Perl. That is to say, for me it is strange and perhaps there is a special reason for it that I do not know of. Not a real Perl Ace. This is the program, as an example: #!/usr/bin/perl -w #-d use strict; my $pu; my $pu_list_cmd; my... (2 Replies)
Discussion started by: ejdv
2 Replies

5. Shell Programming and Scripting

Perl nested array problem

I have a array reference which has some number of array references inside it.The nested array references also contains the array references. my $Filename = "sample.xml"; my $Parser = new XML::Parser( Style => 'tree' ); my $Tree = $Parser->parsefile( $Filename ); Here the $Tree is the... (6 Replies)
Discussion started by: karthigayan
6 Replies

6. Shell Programming and Scripting

search of string from an array in Perl

Hi All I want to search a string from an array in Perl. If a match occurs, assign that string to a variable else assign 'No match'. I tried writing the script as follows but it's in vain. Please help me.. #!/usr/bin/perl use strict; my $NER; my @text=("ORG","PER"); ... (4 Replies)
Discussion started by: my_Perl
4 Replies

7. Shell Programming and Scripting

perl string matching problem (two backslash)

I have a few .tex files generated with html2latex. They have some extra \\ that generate error with pdflatex, so I would like to get rid of them. This perl -p -i -e s/\\\\//g myfile.tex with or without simple or double quote remove all of the backslashes, also the single ones needed by tex. How... (2 Replies)
Discussion started by: ahsog
2 Replies

8. Shell Programming and Scripting

PERL, push to hash of array problem

$key = "a"; $value = "hello"; %myhash = {} ; push @{ myHash{$key} }, $hello; print $myHash{$key}."\n"; this script prints "hello" but has following error message. Reference found where even-sized list expected at ./test line 5. can any one help me to fix this problem?? (3 Replies)
Discussion started by: bonosungho
3 Replies

9. Shell Programming and Scripting

Problem when assign the array with the string index

I come across the problems when assigning the array in the script below . How to use the array with the 'string index' correctly ? When I assign a new string index , the array elements that are previously assigned are all changed .:eek::eek::eek: $ array=211 $ echo ${array} 211 $... (4 Replies)
Discussion started by: youareapkman
4 Replies

10. Shell Programming and Scripting

PERL: Searching for a string in a text file problem

Looking for a bit of help. I need to search for a string of words, but unfortunately these words are located on separate lines. for example the text output is: United Chanmpions Ronaldo Liverpool Losers Torres and my script code is print("DEBUG - checking file message"); while... (15 Replies)
Discussion started by: meevagh
15 Replies
Login or Register to Ask a Question