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

Guarantee text file at the beginning of the array?

Status
Not open for further replies.

HughbertD

Technical User
Apr 30, 2007
42
GB
Hi

I have /another/ question - sorry, you have all been so good with my queries - thanks.

I am zipping up a bunch of files within directories - and the aim is to have the files but not the directories they are in : however, there is a file "BugList.txt" that exists in $WORKING/ area - I want this in the zip but it seems to mess up the zipping if it is in the array.

I have used a SHIFT which seems to be working - but is there any guarantee the buglist.txt will always be first in the array?

My code :

@list = glob("*");
shift(@list);
foreach $lang (@list)
{
print "Zipping $lang files\n";
chdir $lang;
system("zip -r $WORKING/$kitname *");
chdir $WORKING;
}
chdir $WORKING;
system("zip $WORKING/$kitname Buglist.txt");


Thanks for any help
 
Hi,
Why not made it programatically if it is not?
Something like,
Code:
if ( $list[0] ne "BugList.txt" )
{
  my $cnt = 1 ;
  foreach ( @list ) 
  {
    if ( $list[$i] eq "BugList.txt" )
    {
      my $temp = $list[0] ;
      $list[0] = $list[$i] ;
      $list[$i] = $temp ;
    }
    $i++ ;
  }
}

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Typo
Code:
if ( $list[0] ne "BugList.txt" )
{
  my $cnt = 1 ;
  foreach ( @list ) 
  {
    if ( $list[[b]$cnt[/b]] eq "BugList.txt" )
    {
      my $temp = $list[0] ;
      $list[0] = $list[$cnt] ;
      $list[$cnt] = $temp ;
    }
    $cnt++ ;
  }
}

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
If all you want is a list of files with Buglist.txt removed, then wouldn't
Code:
@list = grep(!/Buglist.txt/, @list);
be simpler?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Oops. Need to escape the dot...
Code:
@list = grep(!/Buglist[red]\[/red].txt/, @list);
My bad.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
I have used a SHIFT which seems to be working - but is there any guarantee the buglist.txt will always be first in the array?

You want to use unshift:


Code:
@list = glob("*");
unshift(@list,'BugList.txt');


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I'd assumed that he was using shift to remove the Buglist.txt from the front of the array as it gets binned in the first example. I thought that the problem was removing it if Buglist.txt wasn't the first entry...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
No, there is no way to guarantee that Buglist.txt will be the first file in the array. If there is a folder called 'aardvark' for instance, that will appear before buglist.txt

The problem is that glob() is returning a list of files and folders but no information as to which is which. Your code assumed that everything returned is a folder. It fell over trying to change directory to a file that isn't a directory.

What you need to do is to test if the name returned by the glob() call represents a file or a folder, for which you can use the '-d' file test operator:

Code:
[url=http://perldoc.perl.org/functions/system.html][black][b]system[/b][/black][/url][red]([/red][red]"[/red][purple]zip [blue]$WORKING[/blue]/[blue]$kitname[/blue] Buglist.txt[/purple][red]"[/red][red])[/red][red];[/red]
[olive][b]foreach[/b][/olive] [url=http://perldoc.perl.org/functions/glob.html][black][b]glob[/b][/black][/url][red]([/red][red]"[/red][purple]*[/purple][red]"[/red][red])[/red] [red]{[/red]
	[olive][b]if[/b][/olive][red]([/red] [url=http://perldoc.perl.org/functions/-X.html][black][b]-d[/b][/black][/url] [red])[/red] [red]{[/red]
    [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Zipping [blue]$_[/blue] files[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
    [url=http://perldoc.perl.org/functions/chdir.html][black][b]chdir[/b][/black][/url] [blue]$_[/blue][red];[/red]
    [black][b]system[/b][/black][red]([/red][red]"[/red][purple]zip -r [blue]$WORKING[/blue]/[blue]$kitname[/blue] *[/purple][red]"[/red][red])[/red][red];[/red]
    [black][b]chdir[/b][/black] [blue]$WORKING[/blue][red];[/red]
	[red]}[/red]
[red]}[/red]
 
You might even want to put the zipping of Buglist.txt inside another file test operator ( -e : exists):
Code:
system("zip $WORKING/$kitname Buglist.txt") if ( -e 'Buglist.txt');
 
'd assumed that he was using shift to remove the Buglist.txt from the front of the array as it gets binned in the first example. I thought that the problem was removing it if Buglist.txt wasn't the first entry...

I misunderstood the OPs original post. I thought they wanted to guarantee "BugList" was always at the beginning and then to process the array.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top