Removing characters from Actionscript 3 Strings - arrays

I have a text file that I read using the usual URLRequest and URLloader functions. It consists of a series of names, each separated by \r\d. I want to create an array of those names, but I want to eliminate both the \r and the \d. This code does a great job at splitting the names into arrays, but it leaves the carriage return in the string.
names = testfile.split(String.fromCharCode(10));
And this leaves the new line:
names = testfile.split(String.fromCharCode(13));
I'm mainly a C/C++/assembly programmer, AS3 has some things that seem rather odd to me. Is there a way to do this? I've tried searching the resulting string array members but I get errors from the compiler. Very easy to do in C/C++/assembly, but I haven't quite figured AS3 out yet.

You should be able to use a RegExp to do this. Something like:
var noLines:String = withLines.replace( /[\r\n]/g, "" );
That'll remove all new lines from your string; whether you want to do that before or after splitting it up to you.
If your string is in the form:
name1
name2
name3
Then you might even be able to get away with splitting using a RegExp:
var names:Array = withLines.split( /[\r\n]/ );
You can test out the RegExp provided here: http://regexr.com?38dmk (click on the replace tab and clear the replace input)

Related

Why can't I use the Match method on an array in ruby?

I need to iterate and parse 108 lines from a file and then sort them into 3 different hashes. (In one iterator.) (In Ruby)
I have the file loaded into the program and into the array I need to parse. When I try to make the iterator anyway I try to use the Regex Match command I get an error abut the unknown method. Is as simple that I can't use that method on a array?
lines = File.readlines('access_log')
lines.each.match(/^([:\d\.]+) .*\[.*\].*\"[A-Z]+ *(.+) HTTP/)
This and every other way I have tried to use the match method it hasn't worked.
This also doesn't anything for the hash, as I haven't done that yet.
/^([:\d\.]+) .*\[.*\].*\"[A-Z]+ *(.+) HTTP/.match(lines)
Have also tried this, but the error output appears that you cant run it on only the array. I beilive this is where I would need to tie the iterator in, but I'm stumped.
So, what's happening is that what readlines does is it slurps the entire text file.
So you have an array with the content of the textfile separated by a newline(and the newline is kept in every string in the array).
After that, you're doing lines.each, which brings out an enumerator. Then you're calling .match on the enumerator instead of the string itself
The proper way to do this would be
lines.each { |line| line.match(/^([:\d\.]+) .*\[.*\].*\"[A-Z]+ *(.+) HTTP/) }
However, the above actually won't do anything because all you're doing is iterating against each element and checking if it matches the REGEX.If you want it to actually do something, try...
matches = lines.map { |line| line.match(/^([:\d\.]+) .*\[.*\].*\"[A-Z]+ *(.+) HTTP/) }
Remember that the match method only works on strings. If match matches something, it returns an object of the class MatchData, else if it doesn't match anything-- nil.

Apache VTL - How to find an item in an array

I am trying to use VTL for the first time and cannot see to get arrays to grab an inputted String.
I have tried "in" (but I do not believe this is a key word in this language), as well .contains() below (but it does not find the string inside the created array)
Code Below for Use Case Reference:
#set($MSpanish = ["Argentina", "Belize"])
#elseif($MSpanish.contains(${lead.Country}))
#set($subject = "hellow world")
What keyword would I use instead of contains? Is there any keyword that would save the time of many if statements with == in VTL?

Perl: Reading from file and saving into an array

I had got a script in Perl and my task is to do some changes in it. This of course means to understand which part does exactly what. I am not familiar with Perl language but I tried to read through some tutorials, but still some things are confusing me. And I got stuck in following part:
while (<KOEFICIENTYfile>) {
#_=(split(/\s+/, $_));
push(#ZAID,shift(#_));
$KOEFICIENTY{$ZAID[-1]}=[#_];
}
As I understands this part then it:
Reads line from filehandle KOEFICIENTYfile
Separates them by spaces (one or more)
Loads first item from this separated array into array ZAID (and in the process, removes it from #_)
??? Adds a rest of a separated array into array KOEFICIENTY? I am confused by curly brackets part and by square brackets after equals sign.
I think that I understood the meaning of #, $, #_ or negative indexing but this is beyond me. Can you please advice me on meaning of this?
[-1] indexing is just a shortcut way to say "last element of the array".
KOEFICIENTY is actually a hash (you can tell this because it is using curly braces, instead of square ones, around the index), so you're putting the rest of the array #_ into a hash called KOEFICIENTY with a key of the last element of the array.
If you include:
use Data::Dumper
at the top of the script and do
print Dumper(%KOEFICIENTY)
it will nicely format the output for you, which may help
The original coder was trying to be too clever using the negative offset. It would have been much more obvious if the code had been written with a simple temporary variable thus:
while (<KOEFICIENTYfile>) {
#_ = (split(/\s+/, $_));
my $key = shift(#_);
push(#ZAID, $key);
$KOEFICIENTY{$key} = [#_];
}
The braces on $KOEFICIENTY show that this is a "hash" of key/value pairs named %KOEFICIENTY, and not a normal array.
If you don't actually need to preserve the sort order of the keys you could just use keys %KOEFICIENTY to retrieve them instead of storing them in #ZAID.
#zaid is a list, into which the first part of the split is added.
%KOEFICIENTY is a hash, in which a reference to the rest of the split is stored as a list reference under the key of the first part.
So if the line is a b c, #zaid will get a and %KOEFICIENTY{'a'} will hold a reference to a list containing b and c.

Easy way to parse file for directives in C?

Is there an easy way to parse a file for a directive (as I can't think of a better word for it)?
I need to scan a file for <!--#directive parameter=value -->, copy the value, find the location and length where this directive was in the file, so it can be replaced with whatever.
I come from microcontrollers, and don't have a lot of experience with extra / full libraries.
Is there a better way to implement this than manually scanning it line by line? (I guess with ftell for position, fgets, and then parse with sscanf, fseek back to last position if it was a match).
Here is a regular expression which can help:
<!--\s*#.+=(\S+)\s?-->
Group with index 1 in each match is your value.
You can test them here: https://regex101.com/
Also consider using high-level language for this. Here is a snippet in C# which prints all values from a text file:
var inputText = File.ReadAllText("D:\\myTextFile.txt");
var regex = new Regex("<!--\\s*#.+=(\\S+)\\s?-->");
var matches = regex.Matches(inputText);
foreach (var g in matches.Cast<Match>().Select(match => match.Groups[1]))
Console.WriteLine(g.ToString());

Regular expression to replace (if|then)

I have some verse references in articles that I want to link to the adjacent verses file.
Example:
some text (Gen 2:15, 16), other text (Ex 4:12, 13) more.. etc.
I could replace the first one with the following regex:
\(Gen \1: \2, \3\)
Here I fixed the "1" (book=) and the "Gen"
But I couldn't figure out how to use if|then so that I could give it all arrays of (Gen|Ex|Lev.. etc.), so that it replaces Gen with book number "1", Ex "2".. etc.
You need to somewhere define what all the book orders are. And you'll need to use some sort of scripting language, not just a plain old regex. For example, you could do something along the lines of:
books = ["Gen", "Ex", ..., "Rev"]
...and then replace book_name with books.index(book_name)+1
The exact code/syntax obviously depends on which language you choose to use.
With notepad++ you won't be able to get the order numbers.
But everything else is possible. You need to put each book on a new line:
find \), and replace by \n
Then use this pattern:
[a-z\s]+\(([a-z]+)\s+([0-9:]+)\,\s+([0-9]+)\)
and replace by:
\1: \2, \3
you'll get the list of urls. Which then you can merge back to one line if needed.
The only problem is the book number.
Demo is here: https://regex101.com/r/qN8mO7/2

Resources