Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to push to an array and add a delimiter 1

Status
Not open for further replies.
Jun 3, 2007
84
0
0
US
Hello, I was wondering how to push data into an array but at the same time when pushing to an array add a '.'

for example. Variable which holds numbers

The reason I am splitting the original number when entered is because I will be running tests/other code against each one of the numbers, but once I am done with each set/group of numbers...etc I want to print the results out in an dotted notation instead of 1 group of numbers per line.

Thanks,

Code:
my $INPUT = $ARGV[0];
@NUM = split(/\./,$INPUT);


foreach $DAT ( @NUM ) {
    print "$DAT, "\n";
} 
 __RESULTS__
this is the printed result 
232
226
658
3

What I would like to do is push/join each number back into an array but dotted(with .) so that the results are 232.226.658.3

thanks for the help in advance!!!

 
>What I would like to do is push/join each number back into an array but dotted(with .) so that the results are 232.226.658.3
Then use join()

Code:
[COLOR=#804040][b]use strict[/b][/color];

[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$INPUT[/color] = [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]232.226.658.3[/color][COLOR=#ff00ff]"[/color];
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]INPUT='[/color][COLOR=#008080]$INPUT[/color][COLOR=#ff00ff]'[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];

[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]@NUM[/color] = [COLOR=#804040][b]split[/b][/color]([COLOR=#804040][b]/[/b][/color][COLOR=#6a5acd]\.[/color][COLOR=#804040][b]/[/b][/color],[COLOR=#008080]$INPUT[/color]);
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\@[/color][COLOR=#ff00ff]NUM=([/color][COLOR=#008080]@NUM[/color][COLOR=#ff00ff])[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];


[COLOR=#804040][b]foreach[/b][/color] [COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$DAT[/color] ( [COLOR=#008080]@NUM[/color] ) {
    [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#008080]$DAT[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]
}

[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$final_string[/color] = [COLOR=#804040][b]join[/b][/color]([COLOR=#ff00ff]'[/color][COLOR=#ff00ff].[/color][COLOR=#ff00ff]'[/color],[COLOR=#008080]@NUM[/color]);
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]final_string='[/color][COLOR=#008080]$final_string[/color][COLOR=#ff00ff]'[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
which does this
Code:
$INPUT='232.226.658.3'
@NUM=(232 226 658 3)
232
226
658
3
$final_string='232.226.658.3'
 
$" is perl predefined variable that tells perl what character to use when you print an array in scalar context. By default it is a space but you can change it to whatver you want.

Code:
my $INPUT = '232.226.658.3';
my @NUM = split(/\./,$INPUT);
# your tests here
{
   local $" = '.';
   print "@NUM\n";
}

I localized it in a block so it does not affect code that comes later in the script.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
mikrom,

in case you are interested I have a tool that marks up perl code into TGML code. You're welcome to use it if you wanted to:





------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi KevinADC,

Thank you for that link to TGML-converter.

Some weeks ago I created a little script as an example for using regular expressions in ooREXX which I posted here in REXX-forum:
But before coding that in REXX I first tried that regular expressions quickly with a Perl-sript, which I posted on the same page as a comparition.
:)
 
Cool. I've never even heard of REXX before, looks killer though.

I can't post the code to the convertor script I have, its long (around 5,000 lines of code total) and consists of a frontend I wrote and a CPAN module that I have heavily modified.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin,

REXX is probably one of the first scripting languages - mainly used on mainframes and similar platforms.
I use it mainly at my work on IBM iSeries (aka AS/400). It's not so powerful as Perl but still usable.

The difference between my script and yours is that your script works probably directly with Perl source, but my only translates the HTML-output produced by the Vim editor into TGML.
Therefore my is very simple.
Your script is probably specialized for Perl only. My doesn't know if the source is written in Perl, Python, REXX, COBOL, Fortran, LISP,...etc. It only translates what Vim produces and that editor support syntax highlighting for dozen of languages.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top