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.
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
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
2. Programming
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
3. Shell Programming and Scripting
@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
4. Shell Programming and Scripting
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
5. Shell Programming and Scripting
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
6. Shell Programming and Scripting
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
7. Shell Programming and Scripting
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
8. Shell Programming and Scripting
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
9. Shell Programming and Scripting
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
10. Shell Programming and Scripting
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
LEARN ABOUT PHP
mb_ereg_replace_callback
MB_EREG_REPLACE_CALLBACK(3) 1 MB_EREG_REPLACE_CALLBACK(3)
mb_ereg_replace_callback - Perform a regular expresssion seach and replace with multibyte support using a callback
SYNOPSIS
string mb_ereg_replace_callback (string $pattern, callable $callback, string $string, [string $option = "msr"])
DESCRIPTION
Scans $string for matches to $pattern, then replaces the matched text with the output of $callback function.
The behavior of this function is almost identical to mb_ereg_replace(3), except for the fact that instead of $replacement parameter, one
should specify a $callback.
PARAMETERS
o $pattern
- The regular expression pattern. Multibyte characters may be used in $pattern.
o $callback
- A callback that will be called and passed an array of matched elements in the $subject string. The callback should return the
replacement string. You'll often need the $callback function for a mb_ereg_replace_callback(3) in just one place. In this case
you can use an anonymous function to declare the callback within the call to mb_ereg_replace_callback(3). By doing it this way you
have all information for the call in one place and do not clutter the function namespace with a callback function's name not used
anywhere else.
o $string
- The string being checked.
o $option
- Matching condition can be set by $option parameter. If i is specified for this parameter, the case will be ignored. If x is
specified, white space will be ignored. If m is specified, match will be executed in multiline mode and line break will be
included in '.'. If p is specified, match will be executed in POSIX mode, line break will be considered as normal character. Note
that e cannot be used for mb_ereg_replace_callback(3).
RETURN VALUES
The resultant string on success, or FALSE on error.
NOTES
Note
The internal encoding or the character encoding specified by mb_regex_encoding(3) will be used as the character encoding for this
function.
EXAMPLES
Example #1
mb_ereg_replace_callback(3) example
<?php
// this text was used in 2002
// we want to get this up to date for 2003
$text = "April fools day is 04/01/2002
";
$text.= "Last christmas was 12/24/2001
";
// the callback function
function next_year($matches)
{
// as usual: $matches[0] is the complete match
// $matches[1] the match for the first subpattern
// enclosed in '(...)' and so on
return $matches[1].($matches[2]+1);
}
echo mb_ereg_replace_callback(
"(d{2}/d{2}/)(d{4})",
"next_year",
$text);
?>
The above example will output:
April fools day is 04/01/2003
Last christmas was 12/24/2002
Example #2
mb_ereg_replace_callback(3) using anonymous function supported in PHP 5.3.0 or later
<?php
// this text was used in 2002
// we want to get this up to date for 2003
$text = "April fools day is 04/01/2002
";
$text.= "Last christmas was 12/24/2001
";
echo mb_ereg_replace_callback(
"(d{2}/d{2}/)(d{4})",
function ($matches) {
return $matches[1].($matches[2]+1);
},
$text);
?>
SEE ALSO
mb_regex_encoding(3), mb_ereg_replace(3), Anonymous functions, information about the callback type.
PHP Documentation Group MB_EREG_REPLACE_CALLBACK(3)