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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

backslash mayhem, arrays, strings, files

Status
Not open for further replies.

goliath

MIS
Aug 18, 1999
62
US
Hello,

I'm using Perl on Windows to read in a file as such:
APPLS\UTILITY\NOV2WIN mtuser:RXL

I read in the entire file full of these and put them into an array (@array).

if I do a "Print @array\n"; all of the backslashes that were in the text file are intact.

I however cannot seem to find a way to get them out of the array and intact for manipulation, printing, etc.

Any help appreciated *scratching head* I've thought about a global search replace on backslashes or something but I can't help but think I'm missing something obvious.

Script snipit below.

# Special Permissions Perl Script

$server = $ARGV[0];
$domain = $ARGV[1];

@parts = split(/\./,$domain);
$site = $parts[0];
$root = $parts[1];
$ext = $parts[2];


# MAIN

open (MODIN,"mods.txt");

while ($linein = <MODIN>){
chomp $linein;
print &quot;$linein\n&quot;; # Still has backslashes!
push (@array, $linein);
}

print &quot;@array\n&quot;; # hey, its got backslashes!

while ($line = <@array>){
$line= &quot;\\\\$server\\&quot;.$line;
print &quot;$line \n&quot;; # Backslashes gone!
}

close (MODIN);


-------------

mod.txt:
APPLS\UTILITY\NOV2WIN mtuser:RXL
APPLS\UTILITY
APPLS\UTILITY\GENERAL\PCTRACKER
APPLS\SWITCH_H
APPLS\NADMIN
 
[tt]while ($line = <@array>){[/tt]

I'm unsure exactly what you're trying to do here, but I suspect you'll get much better results with:

[tt]foreach my $line (@array) {[/tt]

if you're trying to set [tt]$line[/tt] to each of the elements of [tt]@array[/tt]
 
You could change the line that reads:
Code:
while ($line = <@array>){
to:
Code:
foreach $line (@array){
and it'll perform the function you're after, retaining slashes. I'll be honest, I've never seen the < > operator used with arrays (or anything other than filehandles, pipes, etc), what is it supposed to be doing here? ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
I guess I feel kinda silly now.

My intent of the while was the same as the resutl of foreach. I've mis-used while before and had troubles as well.

That seems to take care of my problem =) Thank you very much (back to the books ;) heheh.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top