Perl: varible-sized arrays?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: varible-sized arrays?
# 1  
Old 07-27-2009
Perl: varible-sized arrays?

How do you store strings in a variable-sized array?

Background:
I wrote a program earlier today to work with a very large text file. I chose Perl because it lets me do some nice formatting on the text I grab, instead of just using a shell script to con'cat'enate egrep results.

The program worked pretty nicely, outputting html that is displayed in the browser. (This is useful, not just pretty: the generated web page has many links, and it's much more convenient to click than copy/paste/modify.) But I wanted to modify the results based on whether there is a match or not for each section. If there are 1 or more matches, write the header, the formatted matches, and the footer; otherwise write nothing.


I have a large @all array which will contain a few (maybe 0-100) matches for a particular regular expression. I'd like to store the matches -- actually, formatted text based on those matches -- in an array so I can work with them later. What's the best way to do this? I'd like to get better with perl rather than just emulate C in perl, so I thought it might be a good idea to post here rather than just butt my head against the language a bit longer.
# 2  
Old 07-27-2009
Your question is a bit vague, but to add new data to the end of an array you can the push() function:

@array = ('foo');
push @array,'bar';

now the array has ('foo', 'bar')

You can just keep adding to an array like this until you have used all the available memory, although in practice you don't want to do that. Its just that perl puts no limitations on the size of an array.
# 3  
Old 07-27-2009
Quote:
Originally Posted by CRGreathouse
...
I have a large @all array which will contain a few (maybe 0-100) matches for a particular regular expression. I'd like to store the matches -- actually, formatted text based on those matches -- in an array so I can work with them later.
...
Here's a short script that should give you a few ideas:

Code:
$ 
$ perl -e '$_="ant bat cat dog eel";
>          while (/(\w+)/g) {  # match a word repeatedly in $_
>            push @x, $1;      # push the matched word into the array @x
>          }                   # array @x now has 5 elements - ant, bat, cat, dog, eel
>          foreach $i (@x) {   # loop through the elements of array @x
>            print $i, "\n";   # print the array element
>          }'
ant
bat
cat
dog
eel
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

perl: compare two arrays

Hi friends, I want to compare two arrays and find matched one using perl? Also, I want to delete unmatched one. Plz suggest me solution (1 Reply)
Discussion started by: Renesh
1 Replies

2. Shell Programming and Scripting

Arrays in perl

Hi all, I have a log file which has logs. I am reading logs line by line into perl arrays. I want to print all the arrays elements starting from 8(word) to end of the line. print array......array to a new file. and I have to do it in perl as res of the program in perl. Please help me on... (9 Replies)
Discussion started by: firestar
9 Replies

3. Shell Programming and Scripting

Difference between 2 arrays in perl

Hi, I have 2 arrays: @a=qw(19190289 18381856 12780546 10626296 9337410 8850557 7740161 8101063); @b=qw(18309897 17612870 10626296 16871843 7740161 19947571 18062861); $len=@a; print "<br> length of array1: $len<br>"; $len1=@b; print "<br> length of array2: $len1<br>"; The output... (3 Replies)
Discussion started by: vanitham
3 Replies

4. Shell Programming and Scripting

Perl hash containing arrays

Hi, I am not that good at Perl. But here's what I wanna do. I want to create a hash where the keys would point to different arrays. This is what I have tried to do but in vain :( @arr=(1,2,3); @arr1=(3,2,1); %hashOfLists=(); $hashOfLists{Key1}=@arr."\n"; $hashOfLists{Key2}=@arr1."\n";... (3 Replies)
Discussion started by: King Nothing
3 Replies

5. Shell Programming and Scripting

Comparing arrays in perl

Hi all, I am trying to compare two arrays in perl using the following code. foreach $item (@arrayA){ push(@arrayC, $item) unless grep(/$item/, @arrayB); ... (1 Reply)
Discussion started by: chriss_58
1 Replies

6. Shell Programming and Scripting

perl arrays

Hi I need some help using arrays in perl. I have an array say var and a variable var1. I want to check if the var1 is present in the array. How do I check that ? my @var = 1...10; my $var1 =5; if ( $var1 in @var ) { ....... } else { ....... } Something like above. Can some... (2 Replies)
Discussion started by: ammu
2 Replies

7. Shell Programming and Scripting

Perl array of arrays

Hi, I am trying to assign an array as a value to one of the array element, I mean I have an array @KS and array @kr. I want array @KS to hold @kr as an element. So I am doin this $KS=@kr; But the value stored is number of elements in the @kr array. Can... (2 Replies)
Discussion started by: eamani_sun
2 Replies

8. Shell Programming and Scripting

Perl Arrays and Substituion

@xray =~ s/^ *//g; @xray =~ s/ *$//g; @xray =~ s/\s+/ /g; Guess I have a two part question ... First Is there a way to make substitutions, remove leading spaces, trailing spaces, and crunch multiple spaces into a single space, to the entire array, or must the substitutions be done on on... (1 Reply)
Discussion started by: popeye
1 Replies

9. Programming

perl arrays

hello ppl, i'm coding a perl script and i have the following situation: @array1 = ("test1", "test2", "test3"); @array2 = ("something1", "something2", "something1"); $var1 = "with_one_of_the_array1_values"; $var2 = "with_one_of_the_array2_values"; what i want to do is to compare $var1... (2 Replies)
Discussion started by: crashnburn
2 Replies

10. Shell Programming and Scripting

Using varible/varible substitution in Perl/sed Search & Replace

Hi, I have a program that searches for a particular string patten. I am however having difficulty passing the varible $i (in a loop) as the string pattern to replace. Using either perl or sed search and replace statements, I get the same kinda result. For example, using the perl: for i in... (3 Replies)
Discussion started by: Breen
3 Replies
Login or Register to Ask a Question